Use some loops so that a user can reliably play "best 2 out of 3" in Rock, Paper, Scissors.
Concepts: Loops (review)
Textbook: Chapter 6.
First, make sure your RockPaperScissors game from A09 works well enough to reliably play a single game.
Then, add a loop to continue to prompt the user on bad input. That is, if they enter an invalid move (a String when you require an int or a String or int that does not correspond to a valid move), you should print an error message and prompt them for a valid move again.
Once you can reliably play a single game without crashing regardless of input, add another loop so that you play "best 2 out of 3". That is, the game will continue until the player has either 2 wins or 2 losses. Note that this does not mean you always play 3 games. Also, if a game ends in a draw, it needs to be replayed.
D:\TA\grading\A12>java ZtomaszeA12 This is a simple rock-paper-scissors game. (Game 1 of 3) Enter your move: rock Computer's move: rock Game was a tie. Let's play that one again... (Game 1 of 3) Enter your move: rock Computer's move: paper You lose. (Game 2 of 3) Enter your move: scissors Computer's move: rock You lose. Game over! You lost 2 out of 3 games. D:\TA\grading\A12>java ZtomaszeA12 This is a simple rock-paper-scissors game. (Game 1 of 3) Enter your move: stone Sorry, but you did not enter a valid move. Enter your move: a stone is the same thing as a rock Sorry, but you did not enter a valid move. Enter your move: 0 If you enter your move as a number, it must be between 1 and 3. Try again. Enter your move: sigh... okay, rock Sorry, but you did not enter a valid move. Enter your move: ROCK Computer's move: paper You lose. (Game 2 of 3) Enter your move: scissors Computer's move: scissors Game was a tie. Let's play that one again... (Game 2 of 3) Enter your move: this game suxors Sorry, but you did not enter a valid move. Enter your move: scissors Computer's move: paper You win! (Game 3 of 3) Enter your move: yeah, take that, you foul computer! Sorry, but you did not enter a valid move. Enter your move: One track mind, huh? Sorry, but you did not enter a valid move. Enter your move: paper Computer's move: rock You win! Game over! You won 2 out of 3 games!
Note that this is just an example. You are not required to support both integer and String input or to support input of different cases ("ROCK" vs "rock") as I did here.
Upload your UsernameA12.java
file to Tamarin. Don't forget to include your RockPaperScissors class in the file.
RockPaperScissors
object
RockPaperScissors
class if you want. In particular, you may find some of the following methods valuable:
Remember that RockPaperScissors
tracks wins and losses from the perspective of the external/human player. The first two methods here are probably the most useful. You can add some instance variables to your RockPaperScissors
class, such as wins
, losses
, and draws
. Then, in your play method, whenever a valid move results in a win game in the play method, wins++;
If the game was a loss, losses++;
. If neither of these, then draws++;
. Then, these four methods would each take only a single line to implement. (Total games = wins + losses + draws.)
I think it makes a little more sense for the RockPaperScissors
object to track these details itself. This will also make your main code a little cleaner.