Hello there ! how’d you like to learn about writing the game of Rock, Paper, Scissor in Java ?

Rock, paper, Scissor game is usually played between 2 people. Basically there are 3 shapes made with the outstretched hand. Rock (a simple fist), Paper(a flat hand) and scissor (a fist with the index and middle fingers together forming a V).

The game’s rules are, the rock beats the scissor but loses to the paper, the paper beats the rock but loses to the scissor and the scissor beats the paper but loses to the rock. This is simply because “rock crushes scissors”, “paper covers rock”, “scissors cut paper”. It will be a draw if the both players made the same shape.

This game is played to settle disputes, yes more or less like a coin flip.

Okay, let’s start writing code ! (One player will be the user and the other player will be the computer).

  •  I have created the main class and named it as RockPaperScissor. This will be the class where I will write the code. I have written the method declaration for the constructor and the main method. Then I have used the enumeration for the hand gestures. Here I have used enumeration instead of using strings because the enumeration allows to pre-define our constants. I have called the enum type Gesture with the values ROCK, PAPER, SCISSOR.

    Screenshot (138)

  • Then I have created 2 private classes as the User and the Opponent. These classes represents the two players in the game. The user class will be the class that takes the input of either ROCK, PAPER, SCISSOR from the user. So the getGesture method has to be implemented. The opponent class also need a getGesture method so that the computer can also make a gesture. I have put place holders in these methods and I have implemented them later.  The user class will need a constructor to set up the scanner object to acquire the user input. I have put the Scanner as a private field for the user and then I have instantiated it in the constructor. The Opponent class won’t be needing a constructor, when I initiate the Opponent object, it will be the default constructor that get called.

Screenshot (139)

  • Then I have implemented the getgesture method in the opponent class and this method will return a random gesture. I have taken an array of gesture enumerations by calling values() method. To choose a random gesture enumeration in this values array, a random index (integer between 0 and gestures.length) is generated. To do this, I have used the nextInt() method of the random class. After acquiring a random index, I have returned the gesture of that index from the values array.

Screenshot (140)

  • Next I have implemented the getGesture method in the User class and this method will return a gesture corresponding to the user’s input. First there will be a prompt for the user to input a string. The nextLine() method of the scanner class is used to get a string as the input. I have only checked the first letter of the input in case the user has misspelled the word. So if the first letter of the user input is either “R” (for rock), “P” (for paper), or “S” (for scissors), and I have used  toUpperCase() method of the String class to make the user input string all uppercase.

Screenshot (142)

  • I have implemented a playAgain() method for the user class as the user must be able to play the game over and over again. The playAgain() method will returns a Boolean on whether the users wants to play again or not. I have used the Scanner that i  previously initiated in the constructor to get a “Yes” or a “No” from the user. This will only check if the first letter is ‘Y’ to determine whether the user wants to play again.Screenshot (143)

  • Then I have connected the User and the Opponent class together in the RockPaperScissor class. Next I have created private fields for the user and Opponent in the  RockPaperScissor class. To access the getGesture() method first these fields should be accessed. So in the RockPaperScissor class, I have initiated these fields. Also I have declared two fields as UserScore and OpponentScore to keep track of the score and initiated them as 0. And the field numberOfGames is initialized as 0 as well, this will keep track of the number of games.

Screenshot (144)

  • I have extended the Gesture enum to include a method to  decide which gesture won each time. I have written a compare() method that returns 0 if the gestures are the same, 1 if the current gesture beats the other gesture and -1 if the current gesture loses to the other gesture. This will be useful in determining the winner.

    Screenshot (145)

  • I have created a startGame() method in the RockPaperScissor class. This method use the getGesture() methods from the user class and the opponent class to get the user and the opponents gestures.

Screenshot (146)

  • I have implemented the gameStats method to display the statistics of the game.

Screenshot (152)

  • Finally I have initialized an instance of the RockPaperScissor class and call the startGame() method.

Screenshot (148)

Well that’s it !!! Hope you guys understand this simple code. There are numerous ways to implement this game in java, some methods will be easier and efficient than this and some maybe a bit hard. This is a pretty simple approach done by myself. So drop a comment below if you have any clarifications to be done on this code.

14 thoughts on “Rock, Paper, Scissor game in java

Leave a comment