In this post, you will learn how to write the Rock Paper Scissors Game in Java. Rock Paper Scissors, a timeless hand game enjoyed across cultures, captivates players of all ages with its simplicity and thrill. It involves forming one of three shapes – rock, paper, or scissors – with an outstretched hand.
The Rules of Engagement
Rock Paper Scissors is a two-player game where each participant simultaneously forms one of three possible shapes with an outstretched hand. The three shapes are as follows:
- Rock: Represented by a closed fist
- Paper: Represented by an open hand
- Scissors: Represented by a fist with the index and middle fingers extended like a pair of scissors
The rock paper scissors game rules
The players face each other, ready to make their choice. They usually chant “Rock, Paper, Scissors, Shoot!” and reveal their shape on “Shoot.” The winner is determined by comparing the choices:
- Rock vs. Scissors: Rock wins (Rock crushes Scissors).
- Scissors vs. Paper: Scissors win (Scissors cut Paper).
- Paper vs. Rock: Paper wins (Paper covers Rock).
Rock Paper Scissors Game Logic Building
Let’s go through the Rock Paper Scissors game’s logic step by step:
- The program starts by importing the required classes and defining the main class (
RockPaperScissorsGame
) where the game logic will be implemented. - We create a
Scanner
object to read input from the user. This will allow the player to enter their choice (rock, paper, or scissors). - The
main
method begins by welcoming the player to the game and prompting them to enter their choice (rock, paper, or scissors). - The player’s choice is read using
scanner.next().toLowerCase()
..next()
reads the next word the user enters, and.toLowerCase()
converts the input to lowercase, ensuring the program can handle variations in letter case (e.g., “Rock” and “rock” are treated the same). - The program then generates the computer’s choice randomly. It uses the
Math.random()
method, which returns a random decimal between 0 (inclusive) and 1 (exclusive). By multiplying the result by the number of choices (choices.length
), we get a random index within thechoices
array. This index corresponds to the computer’s choice. - The computer’s choice is retrieved from the
choices
array, and both the player’s and computer’s choices are displayed in the console. - The program then proceeds to determine the winner or if the game resulted in a tie. It uses a series of conditional statements (
if
,else if
, andelse
) to check all the possible outcomes. - If the player and computer choose the same option, the program prints “It’s a tie!” and the game ends.
- If the player wins (rock beats scissors, paper beats rock, or scissors beat paper), the program prints “You win!” and the game ends.
- If the player doesn’t win and it’s not a tie, the program assumes that the computer has won, and it prints “You lose!”.
- The game ends after declaring the result, and the
scanner
is closed to release system resources.
Java Program for Rock Paper Scissors Game
The Rock Paper Scissors game follows a simple flow: read the player’s choice, generate the computer’s choice, compare the choices, determine the winner, and print the result to the console. This process repeats every time the game is played.
package com.javacodepoint.game;
import java.util.Scanner;
/*
* A classic two-player game where each player chooses rock, paper, or scissors.
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Rock, Paper, Scissors Game!");
System.out.println("Enter your choice (rock, paper, or scissors): ");
String playerChoice = scanner.next().toLowerCase();
String[] choices = { "rock", "paper", "scissors" };
String computerChoice = choices[(int) (Math.random() * choices.length)];
System.out.println("The computer chose: " + computerChoice);
if (playerChoice.equals(computerChoice)) {
System.out.println("It's a tie!");
} else if ((playerChoice.equals("rock") && computerChoice.equals("scissors"))
|| (playerChoice.equals("paper") && computerChoice.equals("rock"))
|| (playerChoice.equals("scissors") && computerChoice.equals("paper"))) {
System.out.println("You win!");
} else {
System.out.println("You lose!");
}
scanner.close();
}
}
OUTPUT:
Welcome to Rock, Paper, Scissors Game!
Enter your choice (rock, paper, or scissors):
rock
The computer chose: scissors
You win!
Conclusion
Rock Paper Scissors remains a beloved game that brings joy, excitement, and a dash of competition into our lives. It’s a quick decision-making tool or a fun way to pass the time.
See also: Tic-Tac-Toe Game in Java