Results 1 to 7 of 7
  1. #1
    Can
    Can is offline Member
    Join Date
    Sep 2016
    Posts
    4
    Rep Power
    0

    Default Logical Problems - ROCK PAPER SCISSORS LIZARD SPOCK

    I am quite new to Java and programming in general. I very recently learned about the basics of GUIs and decided to put my newly acquired skills to a test and wrote this code in Eclipse:

    Java Code:
    public class RPSLSgui extends JFrame{
    	private JButton rock, paper, scissors, lizard, spock;
    	
    	public RPSLSgui(){
    		super("Rock Paper Scissors Lizard Spock");
    		setLayout(new FlowLayout());
    		
    		Icon r = new ImageIcon(getClass().getResource("rock.png"));
    		Icon p = new ImageIcon(getClass().getResource("paper.png"));
    		Icon s = new ImageIcon(getClass().getResource("scissors.png"));
    		Icon l = new ImageIcon(getClass().getResource("lizard.png"));
    		Icon sp = new ImageIcon(getClass().getResource("spock.png"));
    		
    		rock = new JButton("ROCK", r);
    		add(rock);
    		
    		paper = new JButton("PAPER", p);
    		add(paper);
    		
    		scissors = new JButton("SCISSORS", s);
    		add(scissors);
    		
    		lizard = new JButton("LIZARD", l);
    		add(lizard);
    		
    		spock = new JButton("SPOCK", sp);
    		add(spock);
    		
    		theHandler handler = new theHandler();
    		rock.addActionListener(handler);
    		paper.addActionListener(handler);
    		scissors.addActionListener(handler);
    		lizard.addActionListener(handler);
    		spock.addActionListener(handler);		
    	}
    	
    	public String compMove(){
    		Random rand = new Random();
    		int index = rand.nextInt(5);
    		
    		if (index == 0){
    			return "ROCK";
    		} else if (index == 1) {
    			return "PAPER";
    		} else if (index == 2) {
    			return "SCISSORS";
    		} else if (index == 3) {
    			return "LIZARD";
    		} else if (index == 4) {
    			return "SPOCK";
    		} else {
    			return "";
    		}
    		
    	}
    	
    	public class theHandler implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			if (compMove().equals(event.getActionCommand())){
    				String draw = String.format("Computer: %s\n\nDRAW! %s", compMove(), event.getActionCommand());
    				
    				JOptionPane.showMessageDialog(null, draw);
    			} else if ((compMove().equals("ROCK") && event.getActionCommand().equals("SCISSORS")) || (compMove().equals("ROCK") && event.getActionCommand().equals("LIZARD")) || (compMove().equals("PAPER") && event.getActionCommand().equals("ROCK")) || (compMove().equals("PAPER") && event.getActionCommand().equals("SPOCK")) || (compMove().equals("SCISSORS") && event.getActionCommand().equals("PAPER")) || (compMove().equals("SCISSORS") && event.getActionCommand().equals("LIZARD")) || (compMove().equals("LIZARD") && event.getActionCommand().equals("PAPER")) || (compMove().equals("LIZARD") && event.getActionCommand().equals("SPOCK")) || (compMove().equals("SPOCK") && event.getActionCommand().equals("ROCK")) || (compMove().equals("SPOCK") && event.getActionCommand().equals("SCISSORS"))) {
    				String loss = String.format("Computer:  %s\n\nYOU LOSE! %s", compMove(), event.getActionCommand());
    				
    				JOptionPane.showMessageDialog(null, loss);
    			} else {
    				String win = String.format("Computer:  %s\n\nYOU WIN! %s", compMove(), event.getActionCommand());
    				
    				JOptionPane.showMessageDialog(null, win);
    			}
    		}
    	}
    }
    I imported everything necessary and have a "main" class in which I finish up and run this program. The GUI part of the code runs smoothly, the window, the buttons and the dialog box work fine but although the program sets and outputs the values of compMove and event.getActionCommand() without any errors, the wins, losses and draws appear to be random, the do not fit the rules of the game. For example, although it every now and then outputs an accurate result, in a situation where compMove and event.getActionCommand() are the same, it can also say "YOU WIN!", or "YOU LOSE!" randomly, event though it prints out compMove and the user's move(event.getActionCommand()) accurately. I have absolutely no idea where I made a mistake and it is driving me crazy. Any help would be incredibly appreciated. Thank you in advance for your time.
    Last edited by Can; 09-25-2016 at 03:30 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Logical Problems - ROCK PAPER SCISSORS LIZARD SPOCK

    How are you trying to debug the code? Have you tried printing out the contents of the variables used to make decisions so you can see what the computer sees when the code is executed?
    For example what is in compMove() and event.getActionCommand() just before the if statement?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Can
    Can is offline Member
    Join Date
    Sep 2016
    Posts
    4
    Rep Power
    0

    Default Re: Logical Problems - ROCK PAPER SCISSORS LIZARD SPOCK

    Thank you so much for the advice. When I print out the values in the actionPerformed() method, right before the if statement, compMove() has a different value from the compMove() printed at the end. But not even the first compMove() that I print out is in sync with the end result. Do you know why this is happening? How can I fix this?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Logical Problems - ROCK PAPER SCISSORS LIZARD SPOCK

    compMove() has a different value from the compMove() printed at the end
    Look at the compMove() method. What does it return when it is called?
    Java Code:
       public String compMove(){
            Random rand = new Random();
            int index = rand.nextInt(5);  //<<<< gets new value every call!!!
    It should be called one time and the value saved for the tests in the if statement.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Can
    Can is offline Member
    Join Date
    Sep 2016
    Posts
    4
    Rep Power
    0

    Default Re: Logical Problems - ROCK PAPER SCISSORS LIZARD SPOCK

    Oh... I think I get it, is it because everytime I write compMove(), it reassigns a value to the method?

  6. #6
    Can
    Can is offline Member
    Join Date
    Sep 2016
    Posts
    4
    Rep Power
    0

    Default Re: Logical Problems - ROCK PAPER SCISSORS LIZARD SPOCK

    Thank you so much once more, and please ignore my last reply, I think we posted at the same time and I failed in deleting it. Have a great day.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Logical Problems - ROCK PAPER SCISSORS LIZARD SPOCK

    Print out the actionCommands and see if they are what you expected.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Rock paper scissors.
    By katie_gsu in forum New To Java
    Replies: 7
    Last Post: 11-29-2011, 02:22 AM
  2. Replies: 9
    Last Post: 10-16-2011, 10:55 PM
  3. Rock Paper Scissors Spock Lizard Player Problem
    By flyingcurry in forum New To Java
    Replies: 1
    Last Post: 03-03-2011, 12:19 AM
  4. Rock, paper, scissors
    By StevenF in forum New To Java
    Replies: 3
    Last Post: 01-30-2011, 11:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •