• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simulate a game of Rock, Paper, Scissors

 
Todd Smith Todd
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code for the game on the Java Ranch web site has this code

if(p1Choice==p2Choice)
{
draw++;
System.out.println("\n\t\t\t Draw \n");
}

I'm not sure if I understand or not. In effect with the "==" you are comparing the address at which the strings are stored. Unless they are exactly the same string (that is, at the same address, not that they have the same content) this will always fail.

I am trying to change the program and use p1Choice = (inData.readLine()); and p1Choice = (inData.readLine()); I enter the same for both and they never match. I am just trying to understand. Sorry I am new to Java and I just would like to understand
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that p1Choice and p2Choice are String objects (or any type of object for that matter), you are exactly right. You should use p1Choice.equals(p2Choice) instead.

Layne
[ October 27, 2004: Message edited by: Layne Lund ]
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haven't looked at the specific code but could be something like this.

You have three constants
ROCK
PAPER
SCISSORS

A player chooses one of these constants, if both players choose the same constant then in fact they are pointing to the same object and the == works perfectly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic