Java Rock Paper Scissors Game

The rock paper scissors is a game where through hand formations; the decision of the winner is made. The game works on the following principles:

  1. The rock defeats scissors.
  2. The scissors defeat the paper.
  3. The paper defeats the rocks.
  4. If the formation is same from both the side, it is considered a draw.
  5. whoever wins 5 times is the winner.

This game is made in java which encompasses a competition between the user and his computer system.

Java Rock Paper Scissors Game

Java Rock Paper Scissors Program

import java.util.Random;
import java.util.Scanner;

public class Game {

	public static final String ROCK = "R";
	public static final String PAPER = "P";
	public static final String SCISSORS = "S";

	/**
	 * Get the game Result
	 * */
	public static void getResult(String usersMove, String computersMove) {
	
        System.out.println("Computer's move is: " + computersMove);

        if (usersMove.equals(computersMove))
	        System.out.println("It's a tie!");
        else if (usersMove.equals(ROCK)) 
        {
	        if (computersMove.equals(SCISSORS))
		        System.out.println("You win!! Rock crushes scissors.");
	        else if (computersMove.equals(PAPER))
		        System.out.println("You lose!! Paper eats rock.");
        } 
        else if (usersMove.equals(PAPER)) 
        {
            if (computersMove.equals(ROCK))
		        System.out.println("You win!! Paper eats rock.");
	        else if (computersMove.equals(SCISSORS))
		        System.out.println("You lose!! Scissor cuts paper.");
        } 
        else if (usersMove.equals(SCISSORS)) 
        {
	        if (computersMove.equals(PAPER))
		        System.out.println("You win!! Scissor cuts paper.");
	        else if (computersMove.equals(ROCK))
		        System.out.println("You lose!! Rock breaks scissors.");
        } 
        else
    	    System.out.println("Invalid user input.");
    }
	/**
	 * Get Computer's move
	 * */
public static String getComputersMove(){
		int computersNum;
		String computersMove="";
		Random random = new Random();
		computersNum = random.nextInt(3) + 1;
		if (computersNum == 1)
			computersMove = ROCK;
		else if (computersNum == 2)
			computersMove = PAPER;
		else if (computersNum == 3)
			computersMove = SCISSORS;
		
		return computersMove;
	}
	/**
	 * Get User's move
	 * */
	
	public static String getUsersMove(){
	    Scanner scanner = new Scanner(System.in);
	    System.out.println("Enter your play: ");
		String input = scanner.next().toUpperCase();
        return input;
    }
	
    /**
     * Main method
     * */	
	public static void main(String[] args) {
		System.out.println( "Rock, Paper, Scissors!\n"
				+ "Please enter a move.\n" 
				+"Rock = R, Paper= P, and Scissors = S.\n");
	
		String userInput = getUsersMove(); 
		if (userInput.equals(PAPER) || userInput.equals(ROCK) || userInput.equals(SCISSORS)) 
			getResult(userInput, getComputersMove());
		else 
			System.out.println("Invalid Input " + userInput);
	}
}

Output

Rock, Paper, Scissors!
Please enter a move.
Rock = R, Paper= P, and Scissors = S.

Enter your play: s

Computer’s move is: R
You lose!! Rock breaks scissors.

Leave a Comment

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