Jump to content
xisto Community

Rock, Paper, Scissors Some bugs?

Recommended Posts

I am currently taking a simple Java course and I made a program that allows you to play the simple game of Rock, Paper, Scissors. I have tested and it seems to work but others seem to have trouble with the program and say there are some bugs. I would really appreciate the help! Thank you in advance.

import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Rps extends JFrame    implements ActionListener{  private final char moves[] = {'R', 'P', 'S'};  private JRadioButton rock, paper, scissors;  private JTextField display;   public Rps()  {    super("Rock, paper, Scissors");    rock = new JRadioButton("   Rock   ", true);    paper = new JRadioButton("   Paper  ");    scissors = new JRadioButton(" Scissors ");    ButtonGroup rpsButtons = new ButtonGroup();    rpsButtons.add(rock);    rpsButtons.add(paper);    rpsButtons.add(scissors);    JButton go = new JButton("         Go         ");    go.addActionListener(this);    display = new JTextField(25);    display.setEditable(false);    display.setBackground(Color.yellow);    Container c = getContentPane();    c.setLayout(new FlowLayout());    c.add(rock);    c.add(paper);    c.add(scissors);    c.add(go);    c.add(display);  }  /**   *  returns -1 if the player wins,   *  0 if it's a tie, and 1 if the computer wins   */  private int nextPlay(char computerMove, char playerMove)  {    int result = 0;switch(computerMove)   {    case 'R':	switch (playerMove)	{            case 'R':		result = 0;                break;	    case 'P':		result = -1;		break;	    case 'S':		result = 1;		break;	}	break;    case 'P':	switch (playerMove)	{	    case 'R':		result = 1;		break;	    case 'P':		result = 0;		break;	    case 'S':		result = -1;		break;	}	break;	    case 'S':	switch (playerMove)	{	    case 'R':		result = -1;		break;  	    case 'P':		result = 1;		break;	    case 'S':		result = 0;		break;	}	break;   }	return result;  }  public void actionPerformed(ActionEvent e)  {    char playerMove, computerMove;    if (rock.isSelected())      playerMove = 'R';    else if (paper.isSelected())      playerMove = 'P';    else // if (scissors.isSelected())      playerMove = 'S';    int k = (int)(Math.random() * 3);    computerMove = moves[k];    int result = nextPlay(computerMove, playerMove);    String msg = "  You said " + makeWord(playerMove) + ", I said " +                 makeWord(computerMove);    if (result < 0)      msg += " -- you win.";    else if (result == 0)      msg += " -- tie.";    else // if (result > 0)      msg += " -- I win.";    display.setText(msg);   }  private String makeWord(char move)  {    String word = "";    switch (move)    {      case 'R': word = "ROCK"; break;      case 'P': word = "PAPER"; break;      case 'S': word = "SCISSORS"; break;    }    return word;  }  public static void main(String[] args)  {    Rps window = new Rps();    window.setBounds(300, 300, 300, 140);    window.setDefaultCloseOperation(EXIT_ON_CLOSE);    window.setVisible(true);  }}

Share this post

Link to post
Share on other sites

What are the bugs other people are finding? I tried your code and it works fine, only there were warning during compilation:


warning: [serial] serializable class Rps has no definition of serialVersionUID

public class Rps extends JFrame


1 warning


which, I've read, can be fixed by adding any static, final and long field named "serialVersionUID" in your class:


<ANY-ACCESS-MODIFIER> static final long serialVersionUID = 42L;


Basically, that's all the "problem" I ever encountered; is this it or was there, altogether, something else? :o

Share this post

Link to post
Share on other sites

Ah, no I actually talked to those people and they said that they actually changed some stuff around that they THOUGHT were errors. Some also said that when the played the game itself it kind of messed up, or didn't seem right. But then again they are more of a perfectionist than I am haha. Thank you for your input :o

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.