May 12, 2008

Rock, Paper, Scissors - Some bugs?

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

free web hosting

Rock, Paper, Scissors - Some bugs?

thejestergl
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.

CODE
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);
}
}

 

 

 


Reply

salamangkero
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? happy.gif

Reply

thejestergl
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 smile.gif

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. java paper scissors rock game simulation - 1241.65 hr back.
Similar Topics

Keywords : scissors bugs


    Looking for rock, paper, scissors, bugs,

Searching Video's for rock, paper, scissors, bugs,
advertisement



Rock, Paper, Scissors - Some bugs?



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE