Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:05:11+00:00 2026-05-27T05:05:11+00:00

I am creating a simple Rock Paper Scissors game, but changing the traditional way

  • 0

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;
     }
    }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:05:12+00:00Added an answer on May 27, 2026 at 5:05 am

    As Hot Licks suggests, that’s your problem: you’re using == to check for String equivalence rather than use either the equals(...) or the equalsIgnoreCase(...) 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 of

    if (fu == "bar") {
      // do something
    }
    

    do,

    if (fu.equals("bar")) {
      // do something
    }
    

    or,

    if (fu.equalsIgnoreCase("bar")) {
      // do something
    }
    

    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:

    import java.util.Comparator;
    
    public enum HandGameChoice  {
       ROCK,
       PAPER,
       SCISSORS,
       LIZARD,
       SPOCK;
    
       private static MyComparator myComparator = new MyComparator();
    
       public static int compare(HandGameChoice o1, HandGameChoice o2) {
          return myComparator.compare(o1, o2);
       }
    
       private static class MyComparator implements Comparator<HandGameChoice> {
          private int[][] winMatrix = {
                { 0, -1,  1,  1, -1},
                { 1,  0, -1, -1,  1},
                {-1,  1,  0,  1, -1},
                {-1,  1, -1,  0,  1},
                { 1, -1,  1, -1,  0}
          };
    
          @Override
          public int compare(HandGameChoice o1, HandGameChoice o2) {
             return winMatrix[o1.ordinal()][o2.ordinal()];
          }
       }
    }
    

    A class to test this enum could look like so:

    public class TestHandGameChoices {
       public static void main(String[] args) {
          for (HandGameChoice choice1 : HandGameChoice.values()) {
             for (HandGameChoice choice2 : HandGameChoice.values()) {
                int value = HandGameChoice.compare(choice1, choice2);
                String result = "";
                if (value > 0) {
                   result = "win";
                } else if (value < 0) {
                   result = "lose";
                } else {
                   result = "tie";
                }
                System.out.printf("%-8s vs %-8s: %s%n", choice1, choice2, result);
             }
          }
       }
    }
    

    The output of the test class shows:

    ROCK     vs ROCK    : tie
    ROCK     vs PAPER   : lose
    ROCK     vs SCISSORS: win
    ROCK     vs LIZARD  : win
    ROCK     vs SPOCK   : lose
    PAPER    vs ROCK    : win
    PAPER    vs PAPER   : tie
    PAPER    vs SCISSORS: lose
    PAPER    vs LIZARD  : lose
    PAPER    vs SPOCK   : win
    SCISSORS vs ROCK    : lose
    SCISSORS vs PAPER   : win
    SCISSORS vs SCISSORS: tie
    SCISSORS vs LIZARD  : win
    SCISSORS vs SPOCK   : lose
    LIZARD   vs ROCK    : lose
    LIZARD   vs PAPER   : win
    LIZARD   vs SCISSORS: lose
    LIZARD   vs LIZARD  : tie
    LIZARD   vs SPOCK   : win
    SPOCK    vs ROCK    : win
    SPOCK    vs PAPER   : lose
    SPOCK    vs SCISSORS: win
    SPOCK    vs LIZARD  : lose
    SPOCK    vs SPOCK   : tie
    

    Then the GUI would use it like so:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class HandGameGui extends JPanel {
       private JTextArea tArea = new JTextArea(13, 40);
    
       public HandGameGui() {
          ButtonListener btnListener = new ButtonListener();
          JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
          for (HandGameChoice hgChoice : HandGameChoice.values()) {
             String choiceString = hgChoice.name();
             String initCapChoiceString = choiceString.substring(0, 1)
                   + choiceString.substring(1, choiceString.length()).toLowerCase();
    
             JButton button = new JButton(initCapChoiceString);
             button.setActionCommand(choiceString);
             button.addActionListener(btnListener);
             btnPanel.add(button);
          }
    
          setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
          setLayout(new BorderLayout(5, 5));
          add(new JScrollPane(tArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
          add(btnPanel, BorderLayout.PAGE_END);
       }
    
       private class ButtonListener implements ActionListener {
          private Random random = new Random();
          @Override
          public void actionPerformed(ActionEvent e) {
             String actionCommand = e.getActionCommand();
             HandGameChoice userChoice = HandGameChoice.valueOf(actionCommand);
             int randomInt = random.nextInt(HandGameChoice.values().length);
             HandGameChoice aiChoice = HandGameChoice.values()[randomInt];
    
             int gameResult = HandGameChoice.compare(userChoice, aiChoice);
             String resultStr = "";
             if (gameResult > 0) {
                resultStr = "win";
             } else if (gameResult < 0) {
                resultStr = "lose";
             } else {
                resultStr = "tie";
             }
    
             String output = String.format("You chose %s, and the computer chose %s; you %s%n", 
                   userChoice, aiChoice, resultStr);
             tArea.append(output);
          }
       }
    
       private static void createAndShowGui() {
          HandGameGui mainPanel = new HandGameGui();
    
          JFrame frame = new JFrame("Rock Paper Scissors Lizard Spock");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating a simple RPG game, first time using XNA. Trying to get my character
I am fairly familiar with creating simple custom controls, but I haven't had this
I'm creating simple image viewer, but I want to make a sorting of pictures
I am creating simple chess board game in java, and they are running smoothly,
Creating a simple TCP server based on examples but still do not get how
I'm creating a simple matching game for kids for iPad. All images are drawn
I'm creating a simple CRUD for adding links to a category. Each category has
I'm creating simple proxy server but I faced a strange situation, I've following code
I am creating simple application for Notification. It is showing notification properly But my
I'm creating simple example on JSF. It's small Login application. My problem is duplicate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.