Rock paper scissors game

The problem

Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows:

  1. When the program begins, a random integer number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If thenumber is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the computer’s choice yet)
  2. The user enters his or her choice of “rock”, “paper”, or “scissors” at the keyboard. You can use a menu if you prefer)
  3. The computer’s choice is displayed.
  4. A winner is selected according to the following rules:
    • If one player chooses rock and the other player choosesscissors, then rock wins.
    • If one player chooses scissors and the other player chooses paper, then scissors wins.
    • If one player chooses paper and the other player chooses rock, then paper wins.
    • If both players make the same choice, the game must be played again to determine the winner.
  5. After a winner is selected, the program asks if the user wants to repeat the game. If the answer is yes, restart the game from step 1.

Divide the program into functions that perform each majortask.

Breaking it down

A method to determine choices

/**
 * Method will return null if an invalid choice is given. 
 * 1=rock, 2=paper, or 3=scissors.
 * 
 * @param number
 * @return string type
 * 
 */
public static String getChoice (int number) {
    
    String choice;
    
    switch (number) {
        case 1:
            choice = "rock";
            break;
        case 2:
            choice = "paper";
            break;
        case 3:
            choice = "scissors";
            break;
        default:
            choice = null;
    }
    
    return choice;
}

Computer's choice

/**
 * Method should generate a random number and then return the
 * computers choice.
 * 
 * @return The computer's choice of "rock", "paper", or "scissors".
 */
public static String computerChoice() {
    
    // Create a Random object.
    Random rand = new Random();

    // Generate a random number in the range of
    // 1 through 3.
    int num = rand.nextInt(3) + 1;

    // Return the computer's choice.
    return getChoice (num) ;
}

User's choice

/**
 * Method should return the user's choice.
 * 
 * @return The user's choice of "rock", "paper", or "scissors".
 */
public static String userChoice(Scanner keyboard) {
    
    // Ask the user for input
    System.out.print("Enter 1 - rock, 2 - paper, or 3 - scissors: ");

    int userChoice = keyboard.nextInt();
    
    String play = getChoice (userChoice);
    
    // Validate the choice.
    while (play == null) {
        
        System.out.print("Enter 1 - rock, 2 - paper, or 3 - scissors: ");
        userChoice = keyboard.nextInt();
        play = getChoice (userChoice);
    }

    // Return the user's choice.
    return play;
}

Determine winner

/**
 * The determineWinner method returns the output based on parameters
 * 
 * @param computerChoice The computer's choice.
 * @param userChoice The user's choice.
 */
public static String determineWinner (String computerChoice, String userChoice) {
    
    checkNotNull(computerChoice, "computer must have a choice");
    checkNotNull(userChoice, "user must have a choice");
    
    String output;
    
    output = "The computer's choice was " + computerChoice + ".\n";
    output += "The user's choice was " + userChoice + ".\n\n";

    // check logic
    if (userChoice.equalsIgnoreCase("rock")) {
        if (computerChoice.equalsIgnoreCase("scissors"))
            output += "Rock beats scissors.\nThe user wins!";
        else if (computerChoice.equalsIgnoreCase("paper"))
            output += "Paper beats rock.\nThe computer wins!";
        else
            output += "The game is tied!\nPlay again...";
    } else if (userChoice.equalsIgnoreCase("paper")) {
        if (computerChoice.equalsIgnoreCase("scissors"))
            output += "Scissors beats paper.\nThe computer wins!";
        else if (computerChoice.equalsIgnoreCase("rock"))
            output += "Paper beats rock.\nThe user wins!";
        else
            output += "The game is tied!\nPlay again...";
    } else if (userChoice.equalsIgnoreCase("scissors")) {
        if (computerChoice.equalsIgnoreCase("rock"))
            output += "Rock beats scissors.\nThe computer wins!";
        else if (computerChoice.equalsIgnoreCase("paper"))
            output += "Scissors beats paper.\nThe user wins!";
        else
            output += "The game is tied!\nPlay again...";
    }
    
    return output;
}

Putting it together

public static void main(String[] args) {
    
    String computer;
    String user;
    Scanner keyboard = new Scanner(System.in);

    // Play the game as long as there is a tie.
    do {
        // Get the computer's choice.
        computer = computerChoice();

        // Get the user's choice.
        user = userChoice(keyboard);

        // Determine the winner.
        String output = determineWinner(computer, user);
        System.out.println(output);
        
    } while (user.equalsIgnoreCase(computer));

    keyboard.close();
}

Output

Enter 1 - rock, 2 - paper, or 3 - scissors: 3
The computer's choice was paper.
The user's choice was scissors.

Scissors beats paper.
The user wins!

Unit tests

@Test
public void test_computerChoice () {
    assertNotNull(RockPaperScissorsGame.computerChoice());;
}

@Test
public void test_getChoice_1 () {
    assertEquals("rock", RockPaperScissorsGame.getChoice(1));
}

@Test
public void test_getChoice_2 () {
    assertEquals("paper", RockPaperScissorsGame.getChoice(2));
}

@Test
public void test_getChoice_3 () {
    assertEquals("scissors", RockPaperScissorsGame.getChoice(3));
}

@Test
public void test_getChoice_null () {
    assertNull(RockPaperScissorsGame.getChoice(4));
}

Level Up

  • Display a message to the user when a non number is entered by the user.
  • For the user input, create a menu for readability.
  • There are multiple methods that contain the choices. How could you refactor this program so this information isn't repeated?
  • Add the functionality to continue playing game keeping track of wins/losses.