For this assignment you are limited to the language features in chapters 1 through 4 of the textbook.
Write a program to allow
a human to play a game of Rock, Paper, Scissors versus the computer.
Look at the sample output below which shows 2 sample runs from my
solution to this problem. This shows you what the output of your
program should be. (In the sample data the input from the user was
their name, the number of rounds to play, and a choice for rock, paper,
or scissors each round). The sample runs are shown in green to
distinguish them from project requirements.
Sample Run:
Welcome to Rock
Paper Scissors. I, the computer, will be your opponent.
Please type in your name and press return: Nathan
Welcome Nathan.
All right Nathan. How many games would you like to play?
Enter the number of rounds you want to play and press return: 9
Round 1.
Nathan, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 1
Computer picked rock, Nathan picked rock.
This round is a draw.
Round 2.
Nathan, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 2
Computer picked rock, Nathan picked paper.
Paper covers rock. You win.
Round 3.
Nathan, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 3
Computer picked rock, Nathan picked scissors.
Rock breaks scissors. I win.
Round 4.
Nathan, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 1
Computer picked paper, Nathan picked rock.
Paper covers rock. I win.
Round 5.
Nathan, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 2
Computer picked paper, Nathan picked paper.
This round is a draw.
Round 6.
Nathan, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 3
Computer picked paper, Nathan picked scissors.
Scissors cut paper. You win.
Round 7.
Nathan, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 1
Computer picked scissors, Nathan picked rock.
Rock breaks scissors. You win.
Round 8.
Nathan, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 2
Computer picked scissors, Nathan picked paper.
Scissors cut paper. I win.
Round 9.
Nathan, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 3
Computer picked scissors, Nathan picked scissors.
This round is a draw.
We played 9 games of Rock Paper Scissors.
The computer won 3 times.
Nathan won 3 times.
There were 3 draws.
We are evenly matched at this game.
Sample Run 2:
Welcome to
Rock Paper Scissors. I, the computer, will be your opponent.
Please type in
your name and press return: Lily
Welcome Lily.
All right
Lily. How many games would you like to play?
Enter the
number of rounds you want to play and press return: 7
Round 1.
Lily, please
enter your choice for this round.
1 for rock, 2
for paper, and 3 for scissors: 1
Computer
picked scissors, Lily picked rock.
Rock breaks
scissors. You win.
Round 2.
Lily, please
enter your choice for this round.
1 for rock, 2
for paper, and 3 for scissors: 1
Computer
picked rock, Lily picked rock.
This round is
a draw.
Round 3.
Lily, please
enter your choice for this round.
1 for rock, 2
for paper, and 3 for scissors: 1
Computer
picked rock, Lily picked rock.
This round is
a draw.
Round 4.
Lily, please
enter your choice for this round.
1 for rock, 2
for paper, and 3 for scissors: 1
Computer
picked scissors, Lily picked rock.
Rock breaks
scissors. You win.
Round 5.
Lily, please
enter your choice for this round.
1 for rock, 2
for paper, and 3 for scissors: 3
Computer
picked paper, Lily picked scissors.
Scissors cut
paper. You win.
Round 6.
Lily, please
enter your choice for this round.
1 for rock, 2
for paper, and 3 for scissors: 3
Computer
picked scissors, Lily picked scissors.
This round is
a draw.
Round 7.
Lily, please
enter your choice for this round.
1 for rock, 2
for paper, and 3 for scissors: 2
Computer
picked paper, Lily picked paper.
This round is
a draw.
We played 7
games of Rock Paper Scissors.
The computer
won 0 times.
Lily won 3
times.
There were 4
draws.
You are a
master at Rock, Paper, Scissors.
The program should:
This is not an easy program, mostly due to the size of the program. The individual steps are not too difficult, but there are quite a few steps!! The program description above gives you a rough idea of how to break the program up into parts.
Approach the problem is
steps as we have done in class. Have a high level structure and then
implement parts of that structure (the individual methods) one at a
time, testing to make sure they work before going on. You may have to
write some testing code that will not be part of the final program.
Write psuedocode before you start writing Java code!
Here are some tips on the various parts of the program.
1. main method. In the main program declare a Scanner variable that is hooked up to System.in. You need to include the line of code
import java.util.*;
at the top of your program. Pass the Scanner object you create as a parameter to any methods that need it. The main method should not have a lot of statements, instead it will be calling other methods.
2. ask the user for their name. This is a good candidate for a separate method that returns a String.
3. ask the user how many rounds of Rock, Paper, Scissors they want to play. This is another good candidate for a separate method that returns an int. You do not have to do any error checking on the user input. If they enter something that is not an int it is appropriate for the program to end due to a runtime error.
Recall that if you use
the nextInt() method from the Scanner class you will have
to call the nextLine() method after that to advance the
Scanner past the end of line character. The nextInt()
method does not advance past the end of line character.
4. playing the rounds of the game Given our current programming tools this will be the largest and most complex method. It is in turn broken down into several parts. You will need a number of local variables in this method and they should be declared at the top of the method. Obviously you will need a loop to play the appropriate number of rounds.
5. ask
the user what their choice is. The user will enter an integer
as their choice. You do not need to error check their input. Recall
that if you use the nextInt() method from the Scanner
class you will have to call the nextLine() method after
that to advance the Scanner past the end of line character. The nextInt()
method does not advance past the end of line character.
6.
have the computer make a random choice. To do this you need the computer
to pick a random number between 1 and 3. Create a Random object and
then call nextInt() on that Random object repeatedly to produce the
random numbers.
7. print out each player's choice. You will find it useful to have a method that is passed an int parameters and returns the correct String for that int. In this program 1 represents "Rock", 2 represents "Paper", and 3 represents "Scissors".
8. print the results of that round. This is the most algorithmically difficult part of the assignment because their are nine possible outcomes and using the programming tools of chapters 1 - 4 it is difficult to remove redundancy. The nine possible outcomes are
| Computer Choice | Human Choice | Result |
| Rock | Rock | Draw |
| Rock | Paper | Human Wins |
| Rock | Scissors | Computer Wins |
| Paper | Rock | Computer Wins |
| Paper | Paper | Draw |
| Paper | Scissors | Human Wins |
| Scissors | Rock | Human Wins |
| Scissors | Paper | Computer Wins |
| Scissors | Scissors | Draw |
You can eliminate some redundancy with these nine outcomes, but not much.
9. after playing the specified number of rounds display how time the user won, how many times the computer won, and how many draws occurred. The method that runs the rounds should call a method to display this information.
10. declare who the better player was based on the number of wins. This can be part of the results method but will again require some conditional execution with if statements.
11. Use class constants for the integers representing rock, paper, and scissors. Recall class constants are declared at the top of the class outside any methods:
public static
final int ROCK = 1;
Style issues. Not only will we be grading whether your program works, but does it have good style. Did you provide a good structure to the program using static methods? Did you use meaningful identifiers? Did you provide consistent tabbing and spacing for code inside loops and if statements? Did you provide comments for you methods and your code?
Extras: If you are looking to do something extra and interesting on this project, consider make modifications to how the computer makes its choice. If the human opponent is truly making random guesses than making random guesses is the best computer strategy. It turns out is very hard for people to make truly random guesses. They fall into tendencies. For example people will often will make a choice different than the one they last made. An interesting approach would be to try a different computer picking algorithm. For example what if the computer looks at a human's last guess and assumes they won't make that guess for this round. How could the computer make its choice to increase its chances of winning using this strategy?
When finished turn in your RockPaperScissors.java program using the turnin program. If you are working with another person turn the assignment in to only one person's account, but ensure the header is filled in with both of your names and the unique class ID for your sections.