Solving the Rock Paper Scissors Game in Java

Playing the rock paper scissors game is one of my most memorable times of childhood. Whenever our siblings had any consensus issues among us, always chosen this wonderful game to decide. Those tiny quarrels are very precious and so are the games of childhood. Thought, today let’s connect childhood and adulthood. So let’s make this simple and fun rock paper scissors game in Java.

Don’t worry if you are not aware of the rock, paper, and scissors game then before actual program code, we will let you know the rules of this simple and cool game.

Introduction to Rock Paper Scissors Game

Okay, the game is super simple. It is played with two players only. Each Player will hide their hands in the back and in the count of three, they will make the symbol of either rock, paper or, scissors with their hand at the same time.

Now, who will win? Let’s find out:

The Rock beats the Scissors, simply because the rock has the power to crush the scissor so if one opponent opted Rock and other Scissors then the player with rock will win the game.

The Scissors beat the Paper because scissors can cut the paper. If one opponent has scissor and other with paper then the player with paper is unfortunate. The scissor wins in this case.

The Paper beats the Rock. Are you thinking, How on earth paper is powerful than a rock? well, it is in this game as paper can cover the rock so Paper beats the rock and wins the game.

And of course, if both opponents opt the same element then the game is drawn.

You get the point, right? Let’s move this game further and code the program to make this rock paper scissors game in java.

Rock Paper Scissors Game – The Java way

This game is very simple to play and similarly, it is very simple to implement in Java. Of course, we cannot introduce hands to hide and show the symbol in Java. But, we will introduce the Player and Game in our java program. We will make it the Java way, introducing everything as Object and its state and behavior.

The code for this game in Java can be implemented and played two ways –

  • Two-Player mode
  • Player vs Computer mode

We will provide code for both the program modes in Java. Let’s start the making of rock paper scissors game in java.

The Program to implement the two-player mode

Let’s think of this game in Java perspective. We will have two objects Player and the Game. The Player will have states like id, name, and value. the value here refers to the value which player chooses either R, P, or S which respectively represents Rock, Paper, and Scissors.

Oracle Java Certification

Pro tip:

With Java, Always try to compare your program with real world.

Think everything in Objects, its state and behavior.

Programming with Java becomes much easier with this approach.

Let’s define the Player class:

package com.main.twoplayermode;

public class Player {

	private int id;
	private String name;
	private String value;

	//Setters and Getters for id, name, and value

}

The Game class will have method play(). This method will accept two arguments- the two players or opponents.

Let’s define the Game class, it will have the method to determine which is the winner based on the value.

package com.main.twoplayermode;

public class Game {

	private Player winner;

	public Player play(Player player1, Player player2) {

		String val1 = player1.getValue();
		String val2 = player2.getValue();

		if (val1.equalsIgnoreCase("R")) {
			winner = val2.equalsIgnoreCase("S") ? player1 : player2;
		} else if (val1.equalsIgnoreCase("P")) {
			winner = val2.equalsIgnoreCase("R") ? player1 : player2;
		} else {
			winner = val2.equalsIgnoreCase("P") ? player1 : player2;
		}
		return winner;

	}

}

The code in above program is very simple. It has one property winner of type Player. It has one method which accepts two players. Each player will have one unique id and the game value associated with them. Now, this method decide which is the winner based on the conditions.

We will now have one utility class for multiple purpose like –

  • accepting the user’s input for the game value.
  • printing the result.

Let’s define the App class:

package com.main.twoplayermode;

import java.util.Scanner;

public class App {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		String value = "";

		Player player1 = new Player();
		Player player2 = new Player();

		player1.setId(1);
		player1.setName("Player 1");
		
		player2.setId(2);
		player2.setName("Player 2");
		
		System.out.println("** R stands for Rock, P stands for Paper, S stands for Scissors **\n");
		
		System.out.println("Hi " + player1.getName() + " , Please enter any one of the values - R, P, S");
		
		value = scan.next();
		
		while (!"R".equalsIgnoreCase(value) && !"P".equalsIgnoreCase(value) && !"S".equalsIgnoreCase(value)) {
			System.out.println("You have entered Invalid value, Enter any one of R, P, S ");
			value = scan.next();
		}
		
		player1.setValue(value);

		
		System.out.println("Hi " + player2.getName() + " , Please enter any one of the values - R, P, S\n");
		
		value = scan.next();
		
		while (!"R".equalsIgnoreCase(value) && !"P".equalsIgnoreCase(value) && !"S".equalsIgnoreCase(value)) {
			System.out.println("You have entered Invalid value, Enter any one of R, P, S ");
			value = scan.next();
		}
		
		player2.setValue(value);

		if (player1.getValue().equalsIgnoreCase(player2.getValue())) {
			System.out.println("Game is Draw");
		} else {
			Player winner = new Game().play(player1, player2);

			System.out.println("The Winner is - " + winner.getName());
		}
		
		scan.close();
	}

}

The above java class App is accepting the game value from Players and calling the class Game to make the decision of winner of the rock paper scissors game.

Now, the above rock paper scissors java game requires two players. However, we can make it as a player vs computer mode. Let’s take a look:

The Program to implement the player vs computer mode

In this game mode, we will make the computer to pick the random value between R,P, and S. The user will however input it’s value. And the Game class will decide the winner based on the conditions.

This approach is slightly different from the previous one. This requires only one player / opponent.

In this code approach, we will have same Player and Game class as the above. However, we will have a little different code in our utility class App. In addition, we will have one more class Computer. We are not repeating the same Player and Game class code here.

Pro tip:

The Computer class here will inherit the Player class as it represents parent child relationship.

Remember: Computer IS-A Player.

Let’s define our Computer class:

package com.main.playervscomputer;

import java.util.Random;

public class Computer extends Player {

	private String value;

	public String getValue() {

		return value;
	}

	public void setValue() {
		Random random = new Random();

		int num = random.nextInt(3) + 1;

		if (num == 1)
			value = "R";
		else if (num == 2)
			value = "P";
		else
			value = "S";
	}

}

In the above Computer class, we are using Random to generate the number between 1-3 and then we are assigning the R,P, and S value if the random number is 1,2, and 3 respectively.

Let’s define our App class:

package com.main.playervscomputer;

import java.util.Scanner;

public class App {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		String value = "";

		Player player1 = new Player();
		Computer computer = new Computer();

		player1.setId(1);
		player1.setName("Player 1");

		System.out.println("** R stands for Rock, P stands for Paper, S stands for Scissors **\n");

		System.out.println("Hi " + player1.getName() + " , Please enter any one of the values - R, P, S");

		value = scan.next();

		while (!"R".equalsIgnoreCase(value) && !"P".equalsIgnoreCase(value) && !"S".equalsIgnoreCase(value)) {
			System.out.println("You have entered Invalid value, Enter any one of R, P, S ");
			value = scan.next();
		}

		player1.setValue(value);
		computer.setValue();
		computer.setId(2);
		computer.setName("Computer");

		System.out.println("The value chosen/generated by Computer is - " + computer.getValue() + "\n");

		if (player1.getValue().equalsIgnoreCase(computer.getValue())) {
			System.out.println("Game is Draw");
		} else {
			Player winner = new Game().play(player1, computer);

			System.out.println("The Winner is - " + winner.getName());
		}

		scan.close();
	}

}

The logic here is simple, instead of second Player object, we have Computer object.

As said earlier, the logic in Game and Player class will be same as of two players mode of rock paper scissors.

Final Thoughts

We enjoyed making Rock Paper Scissors Game in Java as much as playing it with friends. It was easier to build as it is very relatable to the real world and Java is very much connected to real world entities. One more thing, this game is also called as Scissor Paper and Stone.

Would definitely suggest you to make it by yourself and play it with your friends using this new approach.

The complete rock paper scissors program is available on Github.

If you like this post, you can reach out to me through comments and social media.

Thanks! Happy Coding!

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply