1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Random;
public class Game extends JFrame
{
private JButton fireButton;
private JButton waterButton;
private JButton snowButton;
private JLabel score;
private JLabel choice;
private Random rand;
private int cpuChoice = 0;
private String cpuChoiceString;
private int userChoice = 0;
private String userChoiceString;
private int scoreUser = 0;
private int scoreCPU = 0;
private static int winningScore = 0;
public Game()
{
setLayout(new FlowLayout());
fireButton = new JButton("Fire");
waterButton = new JButton("Water");
snowButton = new JButton("Snow");
score = new JLabel("You: 0 -- CPU: 0");
choice = new JLabel("Your choice: NULL -- CPU choice: NULL");
rand = new Random();
add(score);
add(choice);
add(fireButton);
add(waterButton);
add(snowButton);
actionL aL = new actionL();
fireButton.addActionListener(aL);
waterButton.addActionListener(aL);
snowButton.addActionListener(aL);
}
public class actionL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
cpuChoice = rand.nextInt(3);
if(cpuChoice == 0)
cpuChoiceString = "Fire";
else if(cpuChoice == 1)
cpuChoiceString = "Water";
else
cpuChoiceString = "Snow";
if(e.getSource() == fireButton)
{
userChoice = 0;
userChoiceString = "Fire";
}
else if(e.getSource() == waterButton)
{
userChoice = 1;
userChoiceString = "Water";
}
else if(e.getSource() == snowButton)
{
userChoice = 2;
userChoiceString = "Snow";
}
WinnerChecker wc = new WinnerChecker();
wc.getWin();
}
}
public class WinnerChecker
{
public void getWin()
{
if(userChoice == cpuChoice) //Case same
choice.setText("Your choice: "+userChoiceString+" -- CPU choice: "+cpuChoiceString);
else if(userChoice == 0 && cpuChoice == 1) //Case Fire - Water
{
scoreCPU++;
score.setText("You: "+scoreUser+" -- CPU: "+scoreCPU);
choice.setText("Your choice: "+userChoiceString+" -- CPU choice: "+cpuChoiceString);
}
else if(userChoice == 0 && cpuChoice == 2) //Case Fire - Snow
{
scoreUser++;
score.setText("You: "+scoreUser+" -- CPU: "+scoreCPU);
choice.setText("Your choice: "+userChoiceString+" -- CPU choice: "+cpuChoiceString);
}
else if(userChoice == 1 && cpuChoice == 0) //Case Water - Fire
{
scoreUser++;
score.setText("You: "+scoreUser+" -- CPU: "+scoreCPU);
choice.setText("Your choice: "+userChoiceString+" -- CPU choice: "+cpuChoiceString);
}
else if(userChoice == 1 && cpuChoice == 2) //Case Water - Snow
{
scoreCPU++;
score.setText("You: "+scoreUser+" -- CPU: "+scoreCPU);
choice.setText("Your choice: "+userChoiceString+" -- CPU choice: "+cpuChoiceString);
}
else if(userChoice == 2 && cpuChoice == 0) //Case Snow - Fire
{
scoreCPU++;
score.setText("You: "+scoreUser+" -- CPU: "+scoreCPU);
choice.setText("Your choice: "+userChoiceString+" -- CPU choice: "+cpuChoiceString);
}
else if(userChoice == 2 && cpuChoice == 1) //Case Snow - Water
{
scoreUser++;
score.setText("You: "+scoreUser+" -- CPU: "+scoreCPU);
choice.setText("Your choice: "+userChoiceString+" -- CPU choice: "+cpuChoiceString);
}
if(scoreCPU == winningScore || scoreUser == winningScore)
{
if(scoreCPU == winningScore)
{
JOptionPane.showMessageDialog(null, "CPU wins the game!", "Winner", JOptionPane.INFORMATION_MESSAGE);
fireButton.setEnabled(false);
waterButton.setEnabled(false);
snowButton.setEnabled(false);
}
else if(scoreUser == winningScore)
{
JOptionPane.showMessageDialog(null, "You win the game!", "Winner", JOptionPane.INFORMATION_MESSAGE);
fireButton.setEnabled(false);
waterButton.setEnabled(false);
snowButton.setEnabled(false);
}
}
}
}
public static void main(String[] args)
{
Game game = new Game();
String winScore = JOptionPane.showInputDialog("Enter winning score: ");
try
{
winningScore = Integer.parseInt(winScore);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "INVALID WINNING SCORE!", "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "Setting a default score of 5!", "Info", JOptionPane.INFORMATION_MESSAGE);
winningScore = 5;
}
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
game.setLocation(dim.width/2-game.getSize().width/2, dim.height/2-game.getSize().height/2);
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setTitle("Fire Water Snow");
game.setSize(250, 120);
game.setResizable(false);
game.setVisible(true);
}
}
|