Rock Paper Scissors Game in Java

In this topic, we are going to learn a funtastic game Rock Paper Scissors. You may have played it during childhood. Let’s understand it by using simple steps. We will see Rock Paper Scissors Game implementation using Java.

It is a two player game and contains three main components Rock, Paper and Scissors. Each player has these components and simultaneously chooses either Rock, Paper, or Scissors. It has some rules like:

  1. Rock beats Scissors but loses to Paper.
  2. Paper beats Rock but loses to Scissors.
  3. Scissors beats Paper but loses to Rock.

RockPaperScissors

Read also: Number guessing game in java
Let’s play with the Java program.

import java.util.*;
public class Main
{
    enum Move {
        ROCK,
        PAPER,
        SCISSORS
    }

    public static String getPlayerMove()
    {
        Scanner scan = new Scanner(System.in);
        String choice = scan.next();
        String playerChoice = choice.toUpperCase();
        scan.close();
        if(playerChoice.equals("ROCK") || playerChoice.equals("PAPER") || playerChoice.equals("SCISSORS")){
            return playerChoice;
        }else{
            System.out.println("This is not Valid Move, Try again!");
            return "Bad Input";
        }

    }

    public static String getComputerMove()
    {
        String computerChoice;
        Random random = new Random();
        int input = random.nextInt(3)+1;
        if (input == 1) {
            computerChoice = Move.ROCK.name();
        }
        else if(input == 2) {
            computerChoice = Move.PAPER.name();
        }
        else {
            computerChoice = Move.SCISSORS.name();
        }
        return computerChoice;    
    }

    public static void main(String args[]) 
    {
        System.out.println("Welcome to Rock Paper Game! \nYour's Game partner is Computer. \nEnter Your Move:  ");
        System.out.println("ROCK");
        System.out.println("PAPER");
        System.out.println("SCISSORS");

        String playerMove = getPlayerMove();
        System.out.println("Your move is: "+ playerMove);
        if(!playerMove.equals("Bad Input")) {
            String computerMove = getComputerMove();
            System.out.println("Computer move is: " + computerMove);
            if (playerMove.equals(computerMove)) {
                System.out.println("Game is Tie !!");
            }
            // If playerMove is ROCK         
            else if (playerMove.equals(Move.ROCK.name())) {
                if(computerMove.equals(Move.PAPER.name())) {
                    System.out.println("Computer Wins");
                    System.out.println("Better Luck Next Time!");
                }else {
                    System.out.println("You Win!");
                    System.out.println("OOhhOO, Congratulations!!! ");
                }
            }
            // If playerMove is PAPER
            else if (playerMove.equals(Move.PAPER.name())) {
                if(computerMove.equals(Move.SCISSORS.name())) { 
                    System.out.println("Computer Wins");
                    System.out.println("Better Luck Next Time!");
                }
                else { 
                    System.out.println("You Win!");
                    System.out.println("OOhhOO, Congratulations!!! ");
                }
            }
            // If playerMove is SCISSORS    
            else {
                if(computerMove.equals(Move.ROCK.name())) {
                    System.out.println("Computer Wins");
                    System.out.println("Better Luck Next Time!");
                }
                else { 
                    System.out.println("You Win!");
                    System.out.println("OOhhOO, Congratulations!!! ");
                }
            }
        }
    }
}

Output:

Welcome to Rock Paper Game!
Your’s Game partner is Computer.
Enter Your Move:
ROCK
PAPER
SCISSORS

Rock
Your move is: ROCK
Computer move is: SCISSORS
You Win!
OOhhOO, Congratulations!!!

That’s all about Rock Paper Scissors Game in Java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.