Rock Paper Scissors Game in Java 2023

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:

  1. Rock: Represented by a closed fist
  2. Paper: Represented by an open hand
  3. 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:

  1. Rock vs. Scissors: Rock wins (Rock crushes Scissors).
  2. Scissors vs. Paper: Scissors win (Scissors cut Paper).
  3. 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:

  1. The program starts by importing the required classes and defining the main class (RockPaperScissorsGame) where the game logic will be implemented.
  2. We create a Scanner object to read input from the user. This will allow the player to enter their choice (rock, paper, or scissors).
  3. The main method begins by welcoming the player to the game and prompting them to enter their choice (rock, paper, or scissors).
  4. 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).
  5. 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 the choices array. This index corresponds to the computer’s choice.
  6. The computer’s choice is retrieved from the choices array, and both the player’s and computer’s choices are displayed in the console.
  7. 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, and else) to check all the possible outcomes.
  8. If the player and computer choose the same option, the program prints “It’s a tie!” and the game ends.
  9. If the player wins (rock beats scissors, paper beats rock, or scissors beat paper), the program prints “You win!” and the game ends.
  10. 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!”.
  11. 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

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

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