This game is also known as scissors rock paper and scissors paper stone. If you are unfamiliar with this quite popular game then do not worry, I will share the rules of the game also. Let's start with the rules first before moving on to the rock paper scissors java program code.
Read Also: Hangman Game in Java
Image Source: Rock paper scissors by timlewisnm Under Creative Commons License
Rules of Rock Paper Scissors Game
1. The Rock beats the Scissors (rock crushes scissors)2. The Scissors beats the Paper (scissors cut paper)
3. The Paper beats the Rock
You might be wondering about 3rd point, how the paper beats the rock. The answer is, paper can cover the rock and hence beats it.
4. If both players create the same formation then the game is a tie or draw.
5. There should be at least 2 players required to play this game.
If you want to learn more about this game then check the Wikipedia link here.
Pseudo Code for Rock Paper Scissors Java Program
1. Prompt Player to enter any one of the following entries: ROCK, PAPER, SCISSORS.2. getPlayerMove() method uses Scanner class to get the move of the player. This method will print and return the move enter by the player.
3. getComputerMove() method uses Random class nextInt(num) method. nextInt(num) method produces the random number between 0 (inclusive) and num (exclusive). getComputerMove() method will print and return the computer move.
4. If playerMove and computerMove are the same, then the game is a tie or draw.
5. Else if playerMove and computerMove are different, then 3 cases are possible.
a. If playerMove is ROCK and computerMove is PAPER then computer wins.
If playerMove is ROCK and computerMove is SCISSORS then the player wins.
b. If playerMove is PAPER and computerMove is SCISSORS then computer wins. If playerMove is PAPER and computerMove is ROCK then the player wins.
c. If playerMove is SCISSORS and computerMove is ROCK then computer wins.
If playerMove is SCISSORS and computerMove is PAPER then the player wins.
Rock Paper Scissors Java Program Code
In the below java program code for rock paper scissors, the game will be played between a user and a computer.import java.util.*; public class Game { public static final String ROCK = "ROCK"; public static final String PAPER = "PAPER"; public static final String SCISSORS = "SCISSORS"; public static void main(String args[]) { System.out.println("Enter any one of the following inputs: "); System.out.println("ROCK"); System.out.println("PAPER"); System.out.println("SCISSORS"); System.out.println(); String playerMove = getPlayerMove(); String computerMove = getComputerMove();
//Rules of the Game Applied Below: /*if both playerMove and computerMove produces the same formation, then Game is a tie*/ if (playerMove.equals(computerMove)) System.out.println("Game is Tie !!"); // if playerMove is ROCK else if (playerMove.equals(Game.ROCK)) System.out.println(computerMove.equals(Game.PAPER) ? "Computer Wins": "Player wins"); // if playerMove is PAPER else if (playerMove.equals(Game.PAPER)) System.out.println(computerMove.equals(Game.SCISSORS) ? "Computer Wins": "Player wins"); // if playerMove is SCISSORS else System.out.println(computerMove.equals(Game.ROCK) ? "Computer Wins": "Player wins"); } /* Get Computer's move using Random class nextInt() method */ public static String getComputerMove() { String computermove; Random random = new Random(); int input = random.nextInt(3)+1; if (input == 1) computermove = Game.ROCK; else if(input == 2) computermove = Game.PAPER; else computermove = Game.SCISSORS; System.out.println("Computer move is: " + computermove); System.out.println(); return computermove; } /* Get Player's move using Scanner class */ public static String getPlayerMove() { Scanner in = new Scanner(System.in); String input = in.next(); String playermove = input.toUpperCase(); System.out.println("Player move is: "+ playermove); return playermove; } }
Output:
Enter any one of the following inputs:
ROCK
PAPER
SCISSORS
ROCK
Player move is: ROCK
Computer move is: SCISSORS
Player wins
That's all for the day, please mention in the comments if you have any questions related to the rock paper scissors game in java.