I am creating a simple Rock Paper Scissors game, but changing the traditional way it is played by adding two more choices, “Lizard” and “Spock”. The Point of this game is to make it more complex then the traditional way of playing it. Not sure if I am overloading the Action Event, but I get this weird bug in the code. there is suppose to be 5 buttons containing the Rock Paper Scissors etc., and 1 JTextArea. The problem is whenever I click the button “Spock” and the String AI gets “Spock”/choices[4] as well, It appear to be ignoring the following code below that I want it to respond to and instead goes with “You Lost… Haha” Option,
if ((e.getSource() == b5) && (AI == choices[4])) {
text.append("It's a Tie!\n\n");
return;
}
Also when clicking the “Lizard” Button, it also ignores the following code below and goes with the “You Won!” Option instead
if ((e.getSource() == b4) && (AI == choices[3])) {
text.append("It's a Tie!\n\n");
return;
}
rules to play
Scissors cut paper
Paper covers rock
Rock crushes lizard
Lizard poisons Spock
Spock smashes scissors
Scissors decapitate lizard
Lizard eats paper
Paper disproves Spock
Spock vaporizes rock
Rock crushes scissors
Complete Code for those that are Curious.
http://www.mediafire.com/?x4853rq3a4fp4ah
*No Viruses. 😉
the code:
import java.awt.event.*;
import javax.swing.*;
class Main2 implements ActionListener
{
JTextArea text;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JButton b5;
String[] choices = { "Rock", "Paper", "Scissors", "Lizard", "Spock" };
int l = this.choices.length;
public static void main(String[] args)
{
Main2 gui = new Main2();
gui.go();
}
public void go() {
JFrame frame = new JFrame("Rock Paper Scissors");
this.text = new JTextArea(13,40);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
b1 = new JButton(choices[0]);
b2 = new JButton(choices[1]);
b3 = new JButton(choices[2]);
b4 = new JButton(choices[3]);
b5 = new JButton(choices[4]);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
text.setEditable(false);
JScrollPane scroller = new JScrollPane(text);
scroller.setVerticalScrollBarPolicy(22);
panel1.add(scroller);
panel2.add(this.b1);
panel2.add(this.b2);
panel2.add(this.b3);
panel2.add(this.b4);
panel2.add(this.b5);
frame.getContentPane().add("Center", panel1);
frame.getContentPane().add("South", panel2);
frame.setSize(500, 300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int nr = (int)(Math.random() *l);
String AI = choices[nr];
if (e.getSource() == b1) {
text.append("Your choice was " + choices[0] + "\nComputer's choice was " + AI + "\n");
}
if (e.getSource() == b2) {
text.append("Your choice was " + choices[1] + "\nComputer's choice was" + AI + "\n");
}
if (e.getSource() == b3) {
text.append("Your choice was " + choices[2] + "\nComputer's choice was " + AI + "\n");
}
if (e.getSource() == this.b4) {
text.append("Your choice was " + choices[3] + "\nComputer's choice was " + AI + "\n");
}
if (e.getSource() == this.b5) {
text.append("Your choice was " + choices[4] + "\nComputer's choice was " + AI + "\n");
}
if (((e.getSource() ==b1) && (AI == choices[2])) || (AI == choices[3])) {
text.append("You won!\n\n");
return;
}
if ((e.getSource() == b1) && (AI == choices[0])) {
text.append("It's a Tie!\n\n");
return;
}
if (((e.getSource() == b1) && (AI == choices[1])) || (AI == choices[4])) {
text.append("You Lost... Haha!\n\n");
return;
}
if (((e.getSource() == b2) && (AI == choices[0])) || (AI == choices[4])) {
text.append("You won!\n\n");
return;
}
if ((e.getSource() == b2) && (AI == choices[1])) {
text.append("It's a Tie!\n\n");
return;
}
if (((e.getSource() == b2) && (AI == choices[2])) || (AI == choices[3])) {
text.append("You Lost... Haha!\n\n");
return;
}
if (((e.getSource() == b3) && (AI == choices[1])) || (AI == choices[3])) {
text.append("You won!\n\n");
return;
}
if ((e.getSource() == b3) && (AI == choices[2])) {
text.append("It's a Tie!\n\n");
return;
}
if (((e.getSource() == b3) && (AI == choices[0])) || (AI == choices[4])) {
text.append("You Lost... Haha!\n\n");
return;
}
if (((e.getSource() == b4) && (AI == choices[1])) || (AI == choices[4])) {
text.append("You won!\n\n");
return;
}
if ((e.getSource() == b4) && (AI == choices[3])) {
text.append("It's a Tie!\n\n");
return;
}
if (((e.getSource() == b4) && (AI == choices[0])) || (AI == choices[2])) {
text.append("You Lost... Haha!\n\n");
return;
}
if (((e.getSource() == b5) && (AI == choices[0])) || (AI == choices[2])) {
text.append("You won!\n\n");
return;
}
if ((e.getSource() == b5) && (AI == choices[4])) {
text.append("It's a Tie!\n\n");
return;
}
if (((e.getSource() == b5) && (AI == choices[1])) || (AI == choices[3])) {
text.append("You Lost... Haha!\n\n");
return;
}
}
}
As Hot Licks suggests, that’s your problem: you’re using
==to check for String equivalence rather than use either theequals(...)or theequalsIgnoreCase(...)method. Understand that==checks if the two objects are the same which is not what you’re interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that’s what matters here. So instead ofdo,
or,
Another issue, rather than having a chit-load of if blocks, why not use a win-matrix to simply, easily and briefly check to see if one choice beats another. Enums would be useful for this, something like this could work:
A class to test this enum could look like so:
The output of the test class shows:
Then the GUI would use it like so: