Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rock Paper Scissors

 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's too much of a code in a method without abstraction.

Also you could use simple arrays to eliminate a lot of sysouts.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Put the gameplay in a loop.
2) You already know when you've won or lost--increment a counter.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about

 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could post your code...
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The error i get is: operator || cannot be applied to boolean,java.lang.String
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check my very first post ... The do ... while loop.
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic