Register Login

Java Code for Rock Paper Scissors Game

25 Nov 2019 11:14 am || 0

In this tutorial, you will learn how to create Rock, Paper and Scissor game in Java Programming Language where one player will be Computer and the other player is User. This article contains simple source code of Rock Paper Scissor for two players with proper documentation.

What is Rock Paper Scissor Game?

Rock Paper and Scissor is a hand game usually played among two people where both the people form one of three objects (Rock, Paper, or Scissors) with an outstretched hand. The winner is decided based on the following:

  • Rock beats Scissor (Rock smashes Scissor)
  • Scissors beats Paper (Scissor cuts Paper)
  • Paper beats Rock (Paper wraps Rock)

Note: If both the players make a similar hand formation than the game will be considered as time.

Rock Paper Scissor Game in Java

Creating a Rock Paper Scissor game in Java is easy. We can create a two-player Rock Paper Scissor game in Java using if else condition and Java.util.Random.nextInt() function.

Java Source Code for Rock Paper Scissor Game

The following code uses Random.nextInt() function to take random input from computer (player 1) and prompt user (player 2)  to choose an option  rock, paper, or scissors and finishes the code by adding nested if-else statements to appropriately report  “User won”, or “Computer won”, or "No Winner won ( Both choose same )."

//Java program for popular Rock, Paper, Scissors Game

//Importing the Random class of util package
import java.util.Random;

//Importing the Scanner class of util package
import java.util.Scanner;

//Main Class of the program
public class Main{

    //Function to generate the computer choice
    public static String generateComputerChoice( Random  random){

        int wordNumber;
        //Choosing a random number using the inbuilt function
        wordNumber = random.nextInt( 3 ) + 1;
        String computerChoice = "";

        //Getting computer choice on the random number
        if( wordNumber == 1 ){
            computerChoice = "rock";
        }else if( wordNumber == 2 ){
            computerChoice = "paper";
        }else if( wordNumber == 3 ){
            computerChoice = "scissors";
        }

        System.out.println( "\nThe Computer already made it's choice" );
        return computerChoice;

    }

    //Function to show the list of available options
    public static void showMenu(){

        System.out.println( "Options to choose from :\n1.Rock\n2.Paper\n3.Scissors" );

    }

    //function to get the user choice
    public static String getUserChoice( Scanner scanner ){

        String userWordChoice = "";
        System.out.print( "\nPlease make yours : " );
        userWordChoice = scanner.nextLine();
        //Returning the user choice
        return userWordChoice;

    }

    //Function to get the user
    public static String chooseWinner( String computerChoice, String userChoice ){

        String winner = "No Winner";
        String customMessage = "Both choose same";
        String finalMessage = "";

        String rockCustomMessage = "The rock smashes the scissor";
        String scissorsCustomMessage = "Scissors cuts paper";
        String paperCustomMessage = "Paper wraps rock";

        //Winner Logic for the game start

        if( computerChoice.equals( "rock" ) && userChoice.equalsIgnoreCase( "scissors" ) ){
            winner = "Computer";
            customMessage = rockCustomMessage;
        }else if( userChoice.equalsIgnoreCase( "rock" ) && computerChoice.equals( "scissors" ) ){
            winner = "User";
            customMessage = rockCustomMessage;
        }

        if( computerChoice.equals( "scissors" ) && userChoice.equalsIgnoreCase( "paper" ) ){
            winner = "Computer";
            customMessage = scissorsCustomMessage;
        }else if( userChoice.equalsIgnoreCase( "scissors" ) && computerChoice.equals( "paper" ) ){
            winner = "User";
            customMessage = scissorsCustomMessage;
        }

        if( computerChoice.equals( "paper" ) && userChoice.equalsIgnoreCase( "rock" ) ){
            winner = "Computer";
            customMessage = scissorsCustomMessage;
        }else if( userChoice.equalsIgnoreCase( "paper" ) && computerChoice.equals( "rock" ) ){
            winner = "User";
            customMessage = paperCustomMessage;
        }

        //Winner Logic for the game ends
        finalMessage = winner + " won ( " + customMessage + " )";

        //Returning the final message
        return finalMessage;

    }

    //Main Function of the program
    public static void main( String [] args ){

        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        String computerChoice;
        String userChoice;
        String winner;

        showMenu();
        computerChoice = generateComputerChoice( random );
        userChoice = getUserChoice( scanner );
        winner = chooseWinner( computerChoice, userChoice );

        System.out.println( "\nYou choose : " + userChoice + "\nComputer choose : " +computerChoice );
        System.out.println( winner );

    }

} 

Output 1 (Tie) 

The Computer already made it's choice

Please make yours : rock

You choose : rock
Computer choose : rock
No Winner won ( Both choose same )

Output 2 (User Won)

Options to choose from :
1.Rock
2.Paper
3.Scissors

The Computer already made it's choice

Please make yours : paper

You choose : paper
Computer choose : rock
User won ( Paper wraps rock )

Output 3 (Computer Won)

Options to choose from :
1.Rock
2.Paper
3.Scissors

The Computer already made it's choice

Please make yours : paper

You choose : paper
Computer choose : scissors
Computer won ( Scissors cuts paper )