This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Rock Paper Scissors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rock Paper Scissors" Watch "Rock Paper Scissors" New topic
Author

Rock Paper Scissors

Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Ok So i have this rock paper scissors game. I have it so it will compile and work, but i would like to be able to loop the whole game so i can keep playing over and over until i enter an invalid entry which will end the game. Also I would like to keep track of wins and loses. Can someone point me in the right direction?
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
That's too much of a code in a method without abstraction.

Also you could use simple arrays to eliminate a lot of sysouts.


Experience and talent are independent of age
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

1) Put the gameplay in a loop.
2) You already know when you've won or lost--increment a counter.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
How about



JDBCSupport - An easy to use, light-weight JDBC framework -
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
So to loop the whole game what kind of loop do I want to use? It is an event controlled loop and I tried using a while loop:

or I've even tried an if loop in the same way as above.... But you can't use the || operator in a boolean expression...
help please...
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
what i am trying to do is to test whether personPlay equals "R","P",or "S". before excuting the game... if input does not equal those values then its game over. And where would i place the loop so that the games keeps running as long as the user inputs valid input each round?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

But you can't use the || operator in a boolean expression

I'm pretty sure you can. Do you have a Java book or access to the web?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
I gave you an example earlier.

What you want to do is check the input immediately after it has been read from the user, then break the loop if the input was wrong.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
well thats what I tried and when i went to compile I got an error saying you can not use || in a boolean expression
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You could post your code...
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76



The error i get is: operator || cannot be applied to boolean,java.lang.String
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
First of all, do not use == when you compare Strings, == is an instance level operator which only returns true if 2 objects refer to one and the same object instance.
Use equals() instead.

What you want to do is

Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
sweet thanks sebastian. That worked.. but how do i get the game to keep looping through. For example.. I run the game I choose "R". I win or lose or tie.. whatever the case may be.. but after that game I want it to loop back up and start over again prompting for my input.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Check my very first post ... The do ... while loop.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
could you please show me where in the code to insert that stuff. I'm really new to loops and all of this really. And the do while stuff you posted does not make sense to me.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Whatever is inside the curly braces of a loop will be part of the interating cycle.

So what are the requirements.

1. You want to read the user input
2. Evaluate the user input
3. Display the message whether or not he won

And that over and over again.

Hence, all code from personPlay = Keyboard.nextLine(); up until all the if else statements need to be in the loop.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
I'm am super confused... could you please just show me what you mean? I realize this might be a pain for you but I really don't understand the do while loop
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You need to understand the idea of iterations before I can post ready baked code .

Check this link: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html

It explains the while loop pretty well. Also check the for loop.

If you then still have questions, ask ..
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
ok that works thanks very much. And now just one more question.. You are going to get sick of me soon. I'm trying to incorporate a counter to keep track of wins, loses, and ties.
when i try to compile this I get a bunch of else without if errors at all my else if statements..... thoughts?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183


this is dangerous.

Any statement that you use without braces only works on the next command only.

So, the System.out will be part of the else if, and then the increment stands in between the previous and the next if and breaks the syntax.

Always use curly braces.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Ok that works... but now when i go to print out the countwin, countlose, counttie it just keeps printint out zeros no matter how many rounds i play.


Should they be static variables???
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
That is because you increment wrong.



first evaluates the value and then increments. Hence prints 0;



first increments and then evaluates, hence prints 1.

So,...



i remains 0, because the evaluation happens first, is assigned to i, and then the incrementation happens which has no effect.

Simply use i++.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
Bryan PeachShould they be static variables???[/quote wrote:Don't know. But if you haven't got a good reason for making something static, it probably shouldn't be.

You need to go through your loop and find where those variables are incremented. Also find whether you have managed to declare them twice.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18753
    
  40

Bryan Peach wrote:I'm am super confused... could you please just show me what you mean? I realize this might be a pain for you but I really don't understand the do while loop


It's not that its a pain... but generally people learn better when they try it out for themselves -- especially when they try it out and fail initially. There is no better way to burn something into the brain than frustration. So... I recommend that you just try it. And who knows? You may figure it out on the first attempt.


BTW, as a side note. I don't really know why, but the do-while loop seems to be the least popular among the loops. The for loop (both variants) and the while loop seems to be used more often.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Sweet finally got it working right... Thank you so much for the help sebastian... I guess that means i owe you a beer or something
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rock Paper Scissors