Rock Paper Scissors is among the widely played games across the globe. It is a fun game that consists of three items/moves (as the name itself suggests). Humans use three different hand motions/gestures to play this game. The “fist” represents rock, “the pointer and middle” fingers represent scissors, and the “flat hand” represents paper. To win this game, the player must beat the opponent’s move. If both players execute the same move, the game ties (draw).
To play this game, the rules are defined as follows:
- Rock Beats/Crushes the Scissors.
- Scissor Beats/Cuts the Paper.
- Paper Beats/Wraps the Rock.
Java is a multi-purpose language being used in numerous fields, including game development. If you are new to game development, creating the Rock Paper Scissors game in Java might be a good idea.
So, what are you waiting for? Follow this step-by-step guide to code the Rock Paper Scissors game in Java and cherish your childhood memories.
How to Create/Build a Rock Paper Scissors Game in Java?
To create a Rock Paper Scissors Game in Java, use the Scanner class to get the user’s move and the Random class to generate the computer’s move. More specifically, use the following algorithm to create this game in Java:
- Get the User’s move as input.
- Verify if the User’s move is valid (it must be either “rock”, “paper”, or “scissors”).
- Get the Computer’s move.
- Compare the user’s move with the computer’s move to check who wins the game.
- If both make the same move, return the message game ties.
- Else, return the winner based on the described game rules.
- Check if the user wants to continue playing or quit.
Code Implementation of Rock Paper Scissors Game in Java
First import the built-in Scanner and Random classes at the start of your program to take the user’s move and generate the computer’s move, respectively:
import java.util.Scanner;
import java.util.Random;
Let’s declare a couple of integer-type variables to count the games won by the user and computer. After this, use the Scanner class to start the game by getting the user’s move. Also, create an object of the Random class that will be used later to generate the computer’s move:
int userCount = 0;
int computerCount = 0;
Scanner userInput = new Scanner(System.in);
Random random = new Random();
System.out.println("Let's Play Rock, Paper, Scissors!");
Now employ a while loop to keep the game going until a break statement occurs. Within the loop, declare a couple of string-type variables to store the user’s and computer’s moves. Use another while loop and within that loop use a switch statement to take a valid user input:
while(true){
int num;
String userMove = "";
String computerMove = "";
OUTER:
while (true) {
System.out.print("Your Turn: rock, paper, or scissors? ");
userMove = userInput.nextLine();
switch (userMove) {
case "rock":
System.out.println("User's Move: Rock");
break OUTER;
case "paper":
System.out.println("User's Move: Paper");
break OUTER;
case "scissors":
System.out.println("User's Move: Scissors");
break OUTER;
default:
System.out.println(userMove +" Please Enter a Valid Move");
break;
}
}
Now let’s use the nextInt() method to generate three random integers between the range “0 <= num < 3”. The computer can play one of three moves: rock, paper, or scissors. So use the switch statement to deal with all three possible moves:
num = random.nextInt(3);
switch (num) {
case 0: computerMove = "rock";
case 1: computerMove = "paper";
case 2: computerMove = "scissors";
}
Print the computer’s move on the console:
switch (computerMove) {
case "rock" -> System.out.println("Computer's Move: Rock");
case "paper" -> System.out.println("Computer's Move: Paper");
case "scissors" -> System.out.println("Computer's Move: Scissors");
default -> {
}}
Let’s use the equals() method with the if-else statements to evaluate the user’s and computer’s moves and display the results accordingly:
if(userMove.equals("rock") && computerMove.equals("scissors"))
{
System.out.println("You won!");
userCount++;
}
else if(userMove.equals("paper") && computerMove.equals("rock"))
{
System.out.println("You won!");
userCount++;
}
else if(userMove.equals("scissors") && computerMove.equals("paper"))
{
System.out.println("You won!");
userCount++;
}
else if(userMove.equals("scissors") && computerMove.equals("rock"))
{
System.out.println("Computer won!");
computerCount++;
}
else if(userMove.equals("rock") && computerMove.equals("paper"))
{
System.out.println("Computer won!");
computerCount++;
}
else if(userMove.equals("paper") && computerMove.equals("scissors"))
{
System.out.println("Computer won!");
computerCount++;
}
else if(userMove.equals(computerMove))
{
System.out.println("Tie!");
}
Instruct the user to press the “y” key to continue playing this game. Any character other than “y” will stop/terminate the game:
System.out.println("Do You want to Play Again! (y/n)");
String Again = userInput.nextLine();
if (!Again.equalsIgnoreCase("y")){
System.out.println("You won " + userCount + " times and lost " + computerCount + " times");
System.out.println("Thanks for playing! See you again");
break;
}
}
userInput.close();
}
}
Review the entire code and execute it to play the game.
Output
The user played three games, out of which the user and computer won one game each, and one game was tied.
Conclusion
The “Rock Paper Scissors” is a classic game that involves two players following distinct rules. It is a physical game played using three hand gestures: “fist“, “flat hand“, and “Pointer & middle” fingers. You can easily code/create the Rock Paper Scissors Game in Java. To do this, use the Scanner class to get the user’s move and the Random class to generate the computer’s move. Use the conditional (Decision-making) statements to evaluate the game results. This guide demonstrates what a Rock Paper Scissors game is, and how to create and play it in Java.