Wikipedia reference: https://en.wikipedia.org/wiki/Rock-paper-scissors
When two persons play this game, they both choose one item from the set {Rock, Paper, Scissors} without knowing the selection made by the opponent. After both players make their choices, the result of the game is determined as follows: if both players made the same selection, the result is a TIE (no one wins); otherwise the winner of the game is determined by the rules: {Rock beats Scissors, Scissors beats Paper, Paper beats Rock}. If choices are made randomly with equal probabilities then each player has 1/3 chance of winning with the probability of a tie being 1/3 also.
You are to write a program to play 10 rounds of the game between the program and a human player. In each round, the program makes a random choice and asks the human player to make his/her choice by typing an integer value in the range [1, 3] (1 = rock, 2 = paper, 3 = scissors). The program then determines and displays the result of that round. The program also maintains counts of wins by computer, wins by human player, and ties. Once all rounds are played, an overall winner is declared or a tie is declared.
The choices made by the computer and the player are internally stored as integers in the range [1, 3]. The results of individual rounds and the overall result from 10 rounds are also represented by integers in the range [1, 3]. To make the program more readable we use the following named constants:
static final int ROCK = 1, PAPER = 2, SCISSORS = 3;
static final int COMPUTER_WIN = 1, PLAYER_WIN = 2, TIE = 3;
We also use the following String arrays to help show word representations of the numbers stored internally.
static final String[] CHOICE_NAMES = {"", "Rock", "Paper", "Scissors"};
static final String[] OUTCOME_NAMES = {"", "Computer win", "Player win", "Tie"};
Note that the first element (at index 0) in each array above is an empty string. Those elements are never used. We used a similar technique in a recent Lab.
The program will use 3 (parallel) arrays to store the computer's choice, the player's choice, and the result from each round of the game in 3 arrays. These arrays are called parallel because the values in the 3 arrays at the same index locations are related: they pertain to the same round of the game. Once again, we will not use the index 0 elements of these arrays.
The 3 parallel arrays allow us to print a table showing the choices and the results from each round of the game (a second pass over the data), after all rounds are played.
The final outcome from all games played (Computer wins, Player wins, or a Tie) is displayed after that table showing individual game results. We will also use some functions to keep the "main" function short.
The main function needs to do the following:
See the sample output. Since the computer choices are randomized, your results could be different from the sample even if the user input (player choices) happens to be the same.
1.Write getPlayerChoice without input data validation.
2.Write getRoundResult.
3.Write showRoundResult.
4.Play one round in main method and verify that the result shown is appropriate.
5.Add the loop in the main method; verify that all rounds are being played.
6.Add point counting in the main program's loop and display the summary result after the loop ends. Check for accuracy.
7.Add the statements storing the player choice, computer choice and round result in the 3 arrays within the loop in the main method. Write the showGameResults method and call it (from main) with appropriate arguments. Verify the table display is correct.
8.Add input validation to getPlayerChoice so if the player puts anything outside the range [1, 3] as his/her choice the method stays in a loop and asks for a valid value. Note: You do not have to guard against non-numeric input.
Each round result should indicate the choice made by the computer, the choice made by the player, and who won that round. Use wording similar to what you see in the sample output, showing as all relevant information. The final result should show the accumulated points by each side. For the final verdict use the following wording:
Computer win: Hurray for me! I'm the champ!
Human player win: Congratulations, you are the champ!
Tie: We're in a tie, let's play again soon!
Sample output: see image.
Sample output showing input validation: see image.
static final Random rand = new Random();
static final Scanner cin = new Scanner(System.in);
static final int ROUNDS = 10;
// choices to pick from
static final int ROCK = 1, PAPER = 2, SCISSORS = 3;
// results from one round of play
static final int COMPUTER_WIN = 1, PLAYER_WIN = 2, TIE = 3;
// String arrays to translate numbers to word desriptions
static final String[] CHOICE_NAMES = {"", "Rock", "Paper", "Scissors"};
static final String[] OUTCOME_NAMES = {"", "Computer win", "Player win", "Tie"};
public static void main(String[] args) {
// display sign-on message
// declare/initialize all primitive type and array reference variables
// The loop starts here
// end loop
// call the showGameResults method
// display final summary from 10 rounds
// sign-off
} // end main
static int getPlayerChoice() {
// stub code
return ROCK;
} // end getPlayerChoice
static int getRoundResult(final int computerChoice, final int playerChoice) {
// stub code
return PLAYER_WIN;
} // end getRoundResult
static void showRoundResult(final int computerChoice, final int playerChoice,
final int roundResult) {
// stub code
out.println("showRoundResult was called);
} // end showRoundResult
static void showGameResults(int[] computerChoices,
int[] playerChoices, int[] results) {
// stub code
out.println("showGameResults was called);
} // end showGameResults