Created
November 2, 2012 07:11
-
-
Save TabsPH/3999188 to your computer and use it in GitHub Desktop.
A Simple Jack And Poy Program by Alvin Tabontabon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import javax.swing.*; | |
import java.awt.event.*; | |
import java.util.Random; | |
public class JackNPoy extends JFrame implements ActionListener, ItemListener{ | |
JLabel lblHeaders = new JLabel("JACK N POY"); | |
JLabel lblPSubHeaders = new JLabel(); | |
JLabel lblCSubHeaders = new JLabel("Computer"); | |
JLabel lblPScores = new JLabel("Player pts: "); | |
JLabel lblCScores = new JLabel("Computer pts: "); | |
Font fHead = new Font("Courier", Font.BOLD, 30); | |
Font fPSubHead = new Font("Courier", Font.PLAIN, 15); | |
Font fScores = new Font("Courier", Font.PLAIN, 11); | |
JButton bGo = new JButton("Roll"); | |
JButton bAgain = new JButton("Re-Start"); | |
JRadioButton[] rdoPTurn = new JRadioButton[3]; | |
JRadioButton[] rdoCTurn = new JRadioButton[3]; | |
ButtonGroup bgRdoP = new ButtonGroup(); | |
ButtonGroup bgRdoC = new ButtonGroup(); | |
String[] strTurn = {"Rock", "Paper", "Scissor"}; | |
Integer[] turnLoc = {80,110,140}; | |
String playerPick; | |
String computerPick; | |
String strName; | |
int p = 0; | |
int c = 0; | |
Random rnd = new Random(); | |
public JackNPoy(){ | |
setPlayer(); | |
setJLabel(); | |
setJRadio(); | |
setJButton(); | |
setJPane(); | |
setJFrame(); | |
} | |
private void setJLabel(){ | |
lblHeaders.setSize(200,50); | |
lblHeaders.setLocation(50,0); | |
lblHeaders.setFont(fHead); | |
lblPSubHeaders.setSize(100,30); | |
lblPSubHeaders.setLocation(30,50); | |
lblPSubHeaders.setFont(fPSubHead); | |
lblCSubHeaders.setSize(100,30); | |
lblCSubHeaders.setLocation(185,50); | |
lblCSubHeaders.setFont(fPSubHead); | |
lblPScores.setSize(150,30); | |
lblPScores.setLocation(15,180); | |
lblPScores.setFont(fScores); | |
lblPScores.setText(lblPScores.getText() + p); | |
lblCScores.setSize(150,30); | |
lblCScores.setLocation(170,180); | |
lblCScores.setFont(fScores); | |
lblCScores.setText(lblCScores.getText() + c); | |
} | |
private void setJRadio(){ | |
for(int i=0; i<strTurn.length; i++){ | |
rdoPTurn[i] = new JRadioButton(strTurn[i], false); | |
rdoPTurn[i].setSize(80,30); | |
rdoPTurn[i].setLocation(20, turnLoc[i]); | |
rdoPTurn[i].addItemListener(this); | |
rdoCTurn[i] = new JRadioButton(strTurn[i], false); | |
rdoCTurn[i].setSize(80,30); | |
rdoCTurn[i].setLocation(185, turnLoc[i]); | |
rdoCTurn[i].setEnabled(false); | |
bgRdoP.add(rdoPTurn[i]); | |
bgRdoC.add(rdoCTurn[i]); | |
} | |
} | |
private void setJButton(){ | |
bGo.setSize(100,20); | |
bGo.setLocation(40,230); | |
bGo.setEnabled(false); | |
bAgain.setSize(100,20); | |
bAgain.setLocation(150,230); | |
bGo.addActionListener(this); | |
bAgain.addActionListener(this); | |
} | |
private void setJFrame(){ | |
setTitle("Jack N Poy by Alvin T."); | |
setSize(300,300); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
setResizable(false); | |
setVisible(true); | |
} | |
private void setJPane(){ | |
getContentPane().setLayout(null); | |
getContentPane().add(lblHeaders); | |
getContentPane().add(lblPSubHeaders); | |
getContentPane().add(lblCSubHeaders); | |
getContentPane().add(lblPScores); | |
getContentPane().add(lblCScores); | |
getContentPane().add(bGo); | |
getContentPane().add(bAgain); | |
for(int i=0; i<strTurn.length; i++){ | |
getContentPane().add(rdoPTurn[i]); | |
getContentPane().add(rdoCTurn[i]); | |
} | |
} | |
public static void main(String[] args) { | |
new JackNPoy(); | |
} | |
private void setPlayer(){ | |
strName = JOptionPane.showInputDialog("Enter your name:"); | |
strName = "" + strName.toUpperCase().charAt(0) + strName.substring(1).toLowerCase(); | |
lblPSubHeaders.setText(strName); | |
JOptionPane.showMessageDialog(JackNPoy.this, "First player who get 10pts will be the winner!"); | |
} | |
public void clear(){ | |
bgRdoP.clearSelection(); | |
bgRdoC.clearSelection(); | |
bGo.setEnabled(false); | |
} | |
private void setWinner(){ | |
JOptionPane.showMessageDialog(JackNPoy.this, "JACK N POY !"); | |
if(playerPick.equals("Rock") && computerPick.equals("Paper")){ | |
lblCScores.setText("Computer pts: " + (c+=1)); | |
JOptionPane.showMessageDialog(null, "Points goes to Computer!"); | |
} | |
else if(playerPick.equals("Rock") && computerPick.equals("Scissor")){ | |
lblPScores.setText("Player pts: " + (p+=1)); | |
JOptionPane.showMessageDialog(null, "Points goes to " + strName + "!"); | |
} | |
else if(playerPick.equals("Paper") && computerPick.equals("Rock")){ | |
lblPScores.setText("Player pts: " + (p+=1)); | |
JOptionPane.showMessageDialog(null, "Points goes to " + strName + "!"); | |
} | |
else if(playerPick.equals("Paper") && computerPick.equals("Scissor")){ | |
lblCScores.setText("Computer pts: " + (c+=1)); | |
JOptionPane.showMessageDialog(null, "Points goes to Computer!"); | |
} | |
else if(playerPick.equals("Scissor") && computerPick.equals("Paper")){ | |
lblPScores.setText("Player pts: " + (p+=1)); | |
JOptionPane.showMessageDialog(null, "Points goes to " + strName + "!"); | |
} | |
else if(playerPick.equals("Scissor") && computerPick.equals("Rock")){ | |
lblCScores.setText("Computer pts: " + (c+=1)); | |
JOptionPane.showMessageDialog(null, "Points goes to Computer!"); | |
} | |
else{ | |
JOptionPane.showMessageDialog(null, "DRAW!"); | |
} | |
if(c == 10){ | |
bAgain.setEnabled(true); | |
for(int i=0;i<strTurn.length; i++) | |
rdoPTurn[i].setEnabled(false); | |
JOptionPane.showMessageDialog(null, "Your such a looser, " + strName + "!!!"); | |
} | |
else if(p == 10){ | |
bAgain.setEnabled(true); | |
for(int i=0;i<strTurn.length; i++) | |
rdoPTurn[i].setEnabled(false); | |
JOptionPane.showMessageDialog(null, "Congratulations " + strName + "!"); | |
} | |
clear(); | |
} | |
public void actionPerformed(ActionEvent ae){ | |
if(ae.getSource() == bGo){ | |
int i = rnd.nextInt(3); | |
rdoCTurn[i].setSelected(true); | |
computerPick = rdoCTurn[i].getActionCommand(); | |
setWinner(); | |
} | |
else{ | |
clear(); | |
p = c = 0; | |
for(int i=0;i<strTurn.length; i++) | |
rdoPTurn[i].setEnabled(true); | |
lblCScores.setText("Computer pts: " + c); | |
lblPScores.setText("Player pts: " + p); | |
} | |
} | |
public void itemStateChanged(ItemEvent ie){ | |
for(int i=0; i<strTurn.length; i++){ | |
if(rdoPTurn[i].isSelected()){ | |
playerPick = rdoPTurn[i].getActionCommand(); | |
bGo.setEnabled(true); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can i get this in python script? really need help for my project please.