Rock, Paper, Scissors game in Java

In this tutorial, I will show you how to create rock, paper and scissors game using java.

For this game, we will need two players whose choices are independent of each other. Each outcome is dependent on the rules of the game. We will simulate similar behavior in our program. Below are the rules of the game as you all know:

  1. Rock beats Scissors.
  2. Scissors beats Paper.
  3. Paper beats Rock.

If both the players made the same choice, it is declared as a tie. Also, whoever wins the 2 or more rounds in 3 chances wins the game.

Java program for Rock, paper and scissors Game

The game will be played between user and the computer. The choices taken by the computer are independent of the choice of the user.  At the end of the 3 rounds, the winner will be declared.

Firstly, we will create an array which contains all the possible choices for a single player.

The getComp() will generate choice taken by the computer by choosing a number randomly between 0-2 which will represent the index in the array of choices. Hence, we will use Random class to generate this number.



  • java.util package contains Random class.
  • An instance of Random class is used to generate random numbers.
  • If two instances have the same seed value, then they will generate the same sequence of random numbers.

Therefore, we call nextInt() method on that instance to generate random number. The parameter passed to this function is the maximum permissible value of the generated number(Excluding the limit). The maximum value in this case is 3.

After that, user will be prompted to enter his/her choice. We will use the Java Scanner class to get user input.

  • java.util package contains Scanner class

After creating an instance of Scanner class, we will use the nextLine() method to read Strings.

Java Code:

import java.util.*;
class prog{
static int user=0;
static int computer=0;
public static void main(String args[])
{
   Scanner sc=new Scanner(System.in);
   String choice[]={"Rock", "Paper", "Scissors"};
   int i=0;
while(i<3){
   int compChoice=getComp();
   System.out.print("User: ");
   String userString=sc.nextLine();
int userChoice=userString.equals(choice[0])?0:(userString.equals(choice[1])?1:2);
   getDecision(compChoice, userChoice);
   System.out.println("Computer: "+choice[compChoice]);
   System.out.println("User: "+user+" Comp: "+computer);
  i++;
  }
 getResult();  
}
public static int getComp(){
 Random obj=new Random();
 int move=obj.nextInt(3);
return move;
}
public static void getDecision(int choice1, int choice2){
if(choice1==choice2)
System.out.print("Tie=> ");
else{
switch(choice1){
case 0: if(choice2==2)
          computer+=1;
        else
          user+=1;
        break;

case 1: if(choice2==0)
          computer+=1;
        else
          user+=1;
        break;

case 2: if(choice2==1)
          computer+=1;
        else
          user+=1;
        break;
}}
}

public static void getResult(){
if(user>computer)
  System.out.println("User Wins");
else if(computer>user)
  System.out.println("Computer wins");
else
  System.out.println("It is a tie");
}
}

We will match the choice entered by the user to the index in the array of choices. Further, we will send both the integer values (representing computer and user choices respectively).

The getDecision(int,int) method will increment the score of the players on the basis of rules of the game. We have used switch case in this program. The control of the program will enter the switch cases if the choices entered by both the players is not same. Hence, switch case is kept in the else block.

Finally, we will display the choices made by both the players, along with the result of each round.

The getResult() method compares scores of both the players at the end of 3 rounds and prints the final result.

Output:

User: Paper
Computer: Scissors
User: 0 Comp: 1
User: Rock
Tie=> Computer: Rock
User: 0 Comp: 1
User: Rock
Computer: Scissors
User: 1 Comp: 1
It is a tie

Also read,

How to create a Puzzle game with java

Leave a Reply

Your email address will not be published. Required fields are marked *