Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Rock, Paper, Scissors, Some bugs?
thejestergl
post Jan 30 2008, 05:37 PM
Post #1


Super Member
*********

Group: Members
Posts: 231
Joined: 21-August 06
Member No.: 28,673



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

Go to the top of the page
 
+Quote Post
salamangkero
post Jan 31 2008, 05:20 AM
Post #2


Super Member
*********

Group: [HOSTED]
Posts: 459
Joined: 15-August 06
From: Philippines
Member No.: 28,387



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
Go to the top of the page
 
+Quote Post
thejestergl
post Jan 31 2008, 03:59 PM
Post #3


Super Member
*********

Group: Members
Posts: 231
Joined: 21-August 06
Member No.: 28,673



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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. The best Rock band(69)
  2. Muse(11)
  3. Emotional Rock Song(5)
  4. Fave Type Of Music?(27)
  5. Slow Rock(5)
  6. Rock, Paper, Scissors(14)
  7. Before The Toilet Paper(17)
  8. Data Can Be Stored On Paper(31)
  9. Paper Mario(5)
  10. Rock Cakes(5)
  11. Gmail Paper(4)
  12. Research Paper Titled: How Have The Negative Effects Of Computers Changed American Society?(4)
  13. A Paper On A Book - Tewwg(0)
  14. Hearts Vector Wall Paper(2)
  15. Google Adsense Bugs(4)
  1. Wall Paper(4)
  2. A Symphonic Rock/orchestra Song I Wrote(4)
  3. 'guitar Hero Iii: Legends Of Rock' (some Interesting News)(0)
  4. Rock Band(2)
  5. What Pests Do You Have?(16)
  6. Amazing Origami Photos - Art Of Paper Folding(15)
  7. From Paper To Film(1)
  8. Beryl/compiz Bugs(3)
  9. Why Do People Hate Australias Because Of Spiders?(5)
  10. Marijuana(3)
  11. White Paper: Security Threat Report: 2008(0)
  12. I Hate Rap (mostly As A Whole) And I Love Rock (again Not All Of It)(10)
  13. Unix Has Bad Bugs(4)


 



- Lo-Fi Version Time is now: 27th July 2008 - 02:29 AM