How to code a simple Rock-Paper-Scissors game in Java

David Saomi
Geek Culture
Published in
4 min readNov 22, 2021
Photo by Mohammad Rahmani on Unsplash

Rock-Paper-Scissors is a game famous for being played by not only children, but people of all ages. Here you will learn how to write a simple Java program to play the game against a computer. Whether you are fairly new to programming or not, this presents a useful project to either practice writing code or freshen up on your Java skills. Follow along!

The story starts simply…

We start with a file that looks something like this:

The .java file should consist of a public class of the same name and a main() method where we will put all of the code.

First let’s get input.

The first step is to get input from the user. We want to let our user choose what move they want to make, right? We do this with theScanner class, which needs to be imported at the top of our program via java.util.Scanner. In addition we will declare and initialize a Scanner object within our main() method like this:

This is good because the Scanner class provides many useful methods for getting input from the user, such as the methodnextLine() which returns a captured line of typed text. Furthermore, this text can be stored in a String variable called playerMove which can be used later for comparing the user’s move against the computer’s. nextLine() is called directly on our object scanner and note the lowercase ‘s’ beacause we are calling it on the object. The code thus far:

Now the program will ask the player to make a move and after typing either “rock”, “paper” or “scissors” the move will be saved in playerMove .

Hal 9000 responds.

The computer needs to make a move as well and we want it to be random. Luckily for us there is a Random class for generating randomness which we will use to get a number between 0 and 2 ( 0, 1 or 2), each one corresponding to one of the three moves (rock, paper or scissors). To do this we need two things: an object of type Random and an int variable to store the random integer. In addition we can call nextInt(3) on the object to get a value from 0 to 2. Do not forget to import java.util.Random at the top of your program!

Now we will declare a String variable called computerMove and below it we will decide what it should contain. Decisions are easily done with the help of conditions. So this is exactly what we will be doing with if-elseif-else blocks.

Now every random integer from 0 to 2 will assign computerMove a specific move.

So, who wins?

A Rock-Paper-Scissors game requires comparing the moves of both players, which can be done with more if-elseif-else blocks, but this could be very long and unreadable. Instead there is a much easier solution: creating a playerWins() method under main()which returns true if the player wins, false otherwise. Subsequently, this method would serve as a condition in an if-elseif-else block in main() so the program could decide what result to print. Our playerWins() method will look like this:

See how the method takes two arguments: playerMove and computerMove and returns true for all the cases where the player wins. For example if the player chose “rock” then if computerMove.equals("scissors") is true that means the player won, the other two cases being a loss and a draw (which we will handle soon).

Printing the results.

Going back to our main() method we will write the other if-elseif-else now. There are three possible scenarios: either the player wins, the player loses or it is a draw. SinceplayerWins()can handle wins and losses we will check for draws in the first if-block, wins in the ifelse-block and the last else will run only if the player lost. The condition playerMove.equals(computerMove) checks whether the choices were equal.

This main() method now calls playerWins() which is run from another place in our file!

Let’s make it multiple rounds!

To make it so we can play the game over and over again we will need to wrap it all in a method of its own like this:

playRockPaperScissors() is a static void method which takes the Scanner object as an argument and does everything our program did before. The code is just in a different place. Consequently, the game can now be run multiple times by looping the execution of playRockPaperScissors() which is precisely what we will be doing next.

Rock-Paper-Scissors is often played in rounds. So, we will ask the user how many rounds they want to play and then repeat the game that many rounds. A for-loop works well in this scenario since we want to iterate a known number of times and we ask for input again using our Scanner object. Unfortunately, there are some issues with Java skipping lines using scanner.nextInt() to immediately get an integer. We will instead use scanner.nextLine() and convert the received String to an int using Integer.parseInt() . The final program looks like this:

Extras

  • Clean code: The code above is very long and maybe hard to read. In programming it is often a good practice to divide your code into many smaller methods. Try doing that with the contents of playRockPaperScissors() !
  • Error handling: The way this game is written right now there is much room for logical errors. For example, the user could enter “Rock” or “rocK” and the program would not understand it, leading to weird outputs. Try writing a loop to keep asking the user for an input that is correct before proceeding! Alternatively write a method for converting text like “rocK” to “rock”!

--

--