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:
- Rock beats Scissors but loses to Paper.
- Paper beats Rock but loses to Scissors.
- Scissors beats Paper but loses to Rock.
Read also: Number guessing game in java
Let’s play with the Java program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
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:
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.