The Rock, Paper, Scissors Game

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.

Your problem

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.

Named Constant Usage

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.

Using parallel arrays

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.

Functions (Methods) to be used (you could use more functions, but these functions must be used)

  • static int getPlayerChoice()
    This function will handle prompting the user for his/her choice and return the choice made by the user. This function should stay in a validation loop if the user enters a number that is not a valid choice (outside the range [1, 3]).
  • static int getRoundResult(final int computerChoice, final int playerChoice)
    This function receives the player's and the computer's choices and determines the result from this round of the game. It returns a value (one of the appropriate named constants) to indicate that result.
  • static void showRoundResult(final int computerChoice, final int playerChoice, final int roundResult)
    This function receives the choices made in a round and the result and displays them using appropriate wording, see sample output.
  • static void showGameResults(int[] computerChoices, int[] playerChoices, int[] results)
    This function receives the arrays storing game data. The function displays a table with 4 columns. The first column is the game rounds (1 10), the second column shows players choices, the third column shows the computers choices and fourth column shows the results from the rounds played.

Responsibilities of the "main" function

The main function needs to do the following:

  • Declare and initialize any variables you want to add, but note that a number of variables and arrays are already declared at the class level.
  • Run 10 rounds of the game. In each round
    • determine computer choice randomly from [1, 3]
    • get player choice (function call)
    • determine the round result (function call), add data from the round to the 3 arrays
    • show the result from round (function call)
    • any other appropriate actions, such as point counting
  • Show all round results in a table (function call)
  • Show final result from all rounds played (may design another function, or do this in main() itself)

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.

Developing this program incrementally

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.

Wording to use

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.

Initial Code

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
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.