Make a Rock, Paper, Scissors Game in Java

Rock, Paper, Scissors is a hand game played by two people. Both people would say "rock, paper, scissors" and then simultaneously form one of three objects (rock, paper, or scissors) with an outstretched hand. The winner is determined by the hand formations. Scissors beats paper, paper beats rock, and rock beats scissors. If both players play the same hand formation, it is considered a tie. We will write a simple game in Java to simulates Rock, Paper, Scissors where one player is the user and the other player is the computer.

Steps

  1. Create the main class and call it RockPaperScissors. This will be the class where we will write the game. You may choose to name it something else such as Game or Main. Write in method declarations for the constructor and the main method.
  2. Create an enumeration for the hand gestures (rock, paper, or scissors). We could use strings to represent rock, paper, or scissors, but an enumeration allows us to predefine our constants which means that using the enumeration is a better design. We will call our enum type Move with the values ROCK, PAPER, and SCISSORS.
  3. Create two private classes User and Computer. These classes will represent our players in the game. You may choose to make these classes public. The User class will be the class that prompts the user for either rock, paper, or scissors, so we will need to write a getMove() method. The Computer class will also need to have a getMove() method so that the computer can also make a move. We will put placeholders in these methods and implement them later. The User class will require a constructor that sets up the Scanner object to take in the user input. We will put the Scanner as a private field for the user and then initiate it in the constructor. Since we are using the Scanner class, we need to write an import statement for it at the top of our code. The Computer class does not require a constructor, so we do not need to write one; when we initiate the Computer object, we will just be calling the default constructor. Here is what our RockPaperScissors class looks like now:
  4. Write the getMove() method for the Computer class. This method will return a random Move. We can get an array of Move enumerations by calling the values() method: Move.values(). To choose a random Move enumeration in this values array, we need to generate a random index that is an integer between 0 and the length of our values array. To do this, we can use the nextInt() method of the Random class which we need to import from java.util. After we have gotten the random index, we can return the Move of that index from our values array.
  5. Write the getMove() method for the User class. This method will return a Move corresponding to what the user has input. We will expect the user to write either "rock", "paper", or "scissors". First, we need to prompt the user for an input: System.out.print("Rock, paper, or scissors? "). Then use the nextLine() method of the Scanner object to get the user input as a string. We need now need to check if the user has submitted a valid move, but we can be lenient if the user has misspelled a word. So we will only check if the first letter of the user input is either "R" (for rock), "P" (for paper), or "S" (for scissors), and we won't care about the case because we will first use the toUpperCase() method of the String class to make the user input string all uppercase. If the user has not entered a remotely correct input, we will prompt the user again. Then, depending on what the user has put in, we will return a corresponding move.
  6. Write a playAgain() method for the User class. The user should be able to play the game over and over again. In order to determine whether the user wants to play again, we need to write a playAgain() method that returns a boolean telling the game whether the user has determined to play again or not. In this method, we are using the Scanner that we had previously initiated in the constructor to get a "Yes" or a "No" from the user. We will only check if the first letter is 'Y' to determine whether the user wants to play again. Any other input will mean that the user does not want to play again.
  7. Connect the User and Computer classes together in the RockPaperScissors class. Now that we have finished writing the User and Computer classes, we can focus on working on our actual game. Create private fields for the User and Computer classes in the RockPaperScissors class. We will need to access these fields to access the getMove() methods when we're playing the game. In the constructor for the RockPaperScissors class, initiate these fields. We will also need to keep track of the score in userScore and computerScore fields, which we need to initiate as 0 in the constructor. We need to keep track of the number of games as well, which will also be a field initiated as 0.
  8. Extend the Move enum to include a method that tells us which move wins in each case. We need to write a compareMoves() method that returns 0 if the moves are the same, 1 if the current move beats the other move, and -1 if the current move loses to the other move. This will be useful for determining the winner in the game. To implement this method, we will first return 0 if the moves are the same and therefore we have a tie. Then write a switch statement for returning 1 or -1.
  9. Create a startGame() method in the RockPaperScissors class. This method will be the playing of the game. Start out by putting a simple System.out.println in the method.
  10. Get moves from the user and the computer. In the startGame() method, use the getMove() methods from the User class and the Computer class to get the user and the computer's moves.
  11. Compare the two moves and determine whether the user won or the computer won. Use the compareMoves() method from the Move enum to to determine whether the user won or not. If the user won, increment the user score by 1. If the user lost, increment the computer score by 1. If there was a tie, do not increment any of the scores. Then increment the number of games played by one.
  12. Ask if the user wants to play again. If the user wants to play again, call startGame() again. Otherwise, call printGameStats() which will print out the statistics of the game. We will write this method in the next step.
  13. Write the printGameStats() method. This method will display the statistics of the game: number of wins, number of losses, number of ties, number of games played, and percentage of games won by the user. The percentage of games won is calculated by the (# wins + (# ties/2))/(# games played). This method uses System.out.printf to print out formatted text.
  14. Start the game in the main class. In the main class, initialize an instance of the RockPaperScissors class and call the startGame() method.
  15. Test out your game. Now that we have gone through all the effort of writing the Rock, Paper, Scissors game, it's time to compile and test everything out!

Sample Program

Related Articles