Java is a programming language that was initiated in 1991 and publicly implemented in 1996. It is a general-purpose language like game development, cloud computing, and many others. It offers the Java Runtime Environment to build and compile the code. Java is a platform-independent language as it can work on Windows, iOS, Linux, etc. The Rock Paper Scissors game is a popular game that can be built using Java to be played online.
What is a Rock Paper Scissor Game?
This game is played with hand gestures to depict Rock with the fist, Paper with the open palm, and Scissors with the first two fingers. Rock takes the upper hand or gets a point by crushing the Scissors. The scissors cut the paper to get a point and the paper wraps the rock to take a point. The main idea is to get as many points by intelligently choosing the gesture that beats the opponent’s hand. It creates a loop as there is no one gesture with an obvious advantage that creates fair play for every participant:
How to Create a Rock Paper Scissor Game in Java?
Java language is used to build online games that humans like to play and socialize with each other online. The following sections explain the process of building the Rock Paper Scissor game using Java programming language:
Before getting to the game-building process, import the built-in classes like Scanner and Random. The Scanner class is used to get the input from the user and the Random class generates the values randomly:
import java.util.Scanner;
import java.util.Random;
After getting the classes, use them to create their objects to use as per requirement throughout the code:
Scanner s1 = new Scanner(System.in);
Random rand = new Random();
while(true){
int num;
String userChoice = "";
String computerChoice = "";
System.out.println("Welcome to Rock, Paper, and Scissors!");
OUTER:
while (true) {
System.out.print("Please choose r)ock, p)aper, or s)cissors > ");
userChoice = s1.nextLine();
switch (userChoice) {
case "r":
System.out.println("User: rock");
break OUTER;
case "p":
System.out.println("User: paper");
break OUTER;
case "s":
System.out.println("User: scissor");
break OUTER;
default:
System.out.println(userChoice+" is not a Valid Input");
break; }
} //Asking if the user wants to keep on playing the game System.out.println("Do You want to Play Again! (y/n)"); String Again = s1.nextLine();
}
- Design a while loop to initialize the string variables that can store the choices made by both the opponents.
- The console asks the user to choose one of the three options using the inner loop in the nested structure.
- The only three options here are “r”, “p”, and “s”, and every other input is considered invalid.
- Here the input is stored using the object of the Scanner and then authenticated using the switch case structure.
- The outer loop asks the user if they want to keep on playing or exit the game.
Now, move on to get the computer’s choice using the object of the Random class:
// Getting the Computer choice
num = rand.nextInt(3);
switch (num) {
case 0: computerChoice = "r";
case 1: computerChoice = "p";
case 2: computerChoice = "s";
default: {
}
}
// Displaying the computer's choice
switch (computerChoice) {
case "s" -> System.out.println("Computer: scissors");
case "r" -> System.out.println("Computer: rock");
case "p" -> System.out.println("Computer: paper");
default -> {
}
}
- Create the num value to store a random option opted by the machine from the provided set.
- The switch case statement is used to store the computer’s choice in its variable.
- Now, make another switch case structure that follows the advanced structure of the case statement.
- It is used to display the choice made by the computer and now both the choices are in the play
As we got choices from both the participants, it is time to evaluate them and find the results using the following code:
int win = 0;
int loss = 0;// Evaluate the winner of each round
if(userChoice.equals("r") && computerChoice.equals("s"))
{
System.out.println("You have won!");
win++;
}
else if(userChoice.equals("p") && computerChoice.equals("r"))
{
System.out.println("You have won!");
win++;
}
else if(userChoice.equals("s") && computerChoice.equals("p"))
{
System.out.println("You have won!");
win++;
}
else if(userChoice.equals("s") && computerChoice.equals("r"))
{
System.out.println("The computer won!");
loss++;
}
else if(userChoice.equals("r") && computerChoice.equals("p"))
{
System.out.println("The computer won!");
loss++;
}
else if(userChoice.equals("p") && computerChoice.equals("s"))
{
System.out.println("The computer won!");
loss++;
}
else if(userChoice.equals(computerChoice))
{
System.out.println("Tie!");
}
- Create the win and loss variables to keep track of the results for each point.
- Simple If-else conditions are used to evaluate the results by comparing the choices of the user and the computer.
- Simply compare all the possible choices by both the opponents and print the result for that scenario.
- There are three possibilities of winning the point for each opponent and for a draw as well.
The following code is used to display the final output for all the games when the user wants to quit:
//Displaying final results
if (!Again.equals("y")){
System.out.println("You won " + win + " times and lost " + loss + " times");
System.out.println("Thanks for playing! See you again");
break;
}
- Create the if condition that will be invoked when the user wants to quit the game.
- The condition will run for any other value than “y” chosen by the player when asked to play again.
- Print the final result with overall points at the end using the win and loss variables.
Complete Code
Now, integrate all the above sections to make a Java program in a proper structure. The complete code of the above sections with proper structure using classes and the main method is mentioned below:
import java.util.Scanner;
import java.util.Random;
public class Game {
public static void main(String[] args) {
int win = 0;
int loss = 0;
Scanner s1 = new Scanner(System.in);
Random rand = new Random();
while(true){
int num;
String userChoice = "";
String computerChoice = "";
System.out.println("Welcome to Rock, Paper, and Scissors!");
OUTER:
while (true) {
System.out.print("Please choose r)ock, p)aper, or s)cissors > ");
userChoice = s1.nextLine();
switch (userChoice) {
case "r":
System.out.println("User: rock");
break OUTER;
case "p":
System.out.println("User: paper");
break OUTER;
case "s":
System.out.println("User: scissor");
break OUTER;
default:
System.out.println(userChoice+" is not a Valid Input");
break;
}
}
// Getting the Computer choice
num = rand.nextInt(3);
switch (num) {
case 0: computerChoice = "r";
case 1: computerChoice = "p";
case 2: computerChoice = "s";
default: {
}
}
// Displaying the computer's choice
switch (computerChoice) {
case "s" -> System.out.println("Computer: scissors");
case "r" -> System.out.println("Computer: rock");
case "p" -> System.out.println("Computer: paper");
default -> {
}
}
// Evaluate the winner of each round
if(userChoice.equals("r") && computerChoice.equals("s"))
{
System.out.println("You have won!");
win++;
}
else if(userChoice.equals("p") && computerChoice.equals("r"))
{
System.out.println("You have won!");
win++;
}
else if(userChoice.equals("s") && computerChoice.equals("p"))
{
System.out.println("You have won!");
win++;
}
else if(userChoice.equals("s") && computerChoice.equals("r"))
{
System.out.println("The computer won!");
loss++;
}
else if(userChoice.equals("r") && computerChoice.equals("p"))
{
System.out.println("The computer won!");
loss++;
}
else if(userChoice.equals("p") && computerChoice.equals("s"))
{
System.out.println("The computer won!");
loss++;
}
else if(userChoice.equals(computerChoice))
{
System.out.println("Tie!");
} //Asking if the user wants to keep on playing the game
System.out.println("Do You want to Play Again! (y/n)");
String Again = s1.nextLine();
//Displaying final results
if (!Again.equals("y")){
System.out.println("You won " + win + " times and lost " + loss + " times");
System.out.println("Thanks for playing! See you again");
break;
}
}
s1.close();
}
}
Output
The following screenshot displays the game playing for multiple inputs and the results in comparison to computers. At the end, the final result is displayed when the user enters “n” when asked to play again:
The practical demonstration of how to play the game is mentioned in the following GiF:
That’s all about how to create a Rock Paper Scissors game in Java.
Conclusion
The Rock Paper Scissors game is played using hand gestures between two opponents generally. The Rock beats Scissors, Scissors beats Paper, and Paper beats the Rock gesture to get the point. The Java language is used to build games like that to be played online with the computer or other opponents. The nested loops are used to get the choices from the opponents. The switch case and if statements are used to compare the choices and evaluate the results at the end.