w3resource

Java Project: Rock, Paper, Scissors Game

Java - Rock, Paper, Scissors using Random Class and switch Statements:

Rock, Paper, Scissors Game:

From Wikipedia -
Rock paper scissors (also known by several other names and word orders, see § Names) is an intransitive hand game, usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are "rock" (a closed fist), "paper" (a flat hand), and "scissors" (a fist with the index finger and middle finger extended, forming a V). The earliest form of "rock paper scissors"-style game originated in China and was subsequently imported into Japan, where it reached its modern standardized form, before being spread throughout the world in the early 20th century.

Input: User's choice: rock, paper, or scissors.
Output: Result of the game (win, lose, or draw).

Example:

  • Input: "Rock"
  • Output: "Computer chose Scissors. You win!"
  • Input: "Paper"
  • Output: "Computer chose Rock. You win!"

Solution 1: Rock, Paper, Scissors Game using Random Class

Code:

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

public class RockPaperScissors {

    public static void main(String[] args) {
        // Array of choices
        String[] rps = {"Rock", "Paper", "Scissors"};

        // Create a Scanner for user input
        Scanner scanner = new Scanner(System.in);

        // Get user's choice
        System.out.println("Enter your choice (Rock, Paper, or Scissors): ");
        String userChoice = scanner.nextLine();

        // Generate a random choice for the computer
        Random random = new Random();
        int randomIndex = random.nextInt(3);
        String computerChoice = rps[randomIndex];

        // Print the computer's choice
        System.out.println("Computer chose: " + computerChoice);

        // Determine and print the result of the game
        if (userChoice.equalsIgnoreCase(computerChoice)) {
            System.out.println("It's a draw!");
        } else if (userChoice.equalsIgnoreCase("Rock") && computerChoice.equals("Scissors") ||
                   userChoice.equalsIgnoreCase("Paper") && computerChoice.equals("Rock") ||
                   userChoice.equalsIgnoreCase("Scissors") && computerChoice.equals("Paper")) {
            System.out.println("You win!");
        } else {
            System.out.println("You lose!");
        }

        scanner.close();
    }
} 

Output:

 Enter your choice (Rock, Paper, or Scissors): 
 Rock
Computer chose: Scissors
You win!
Enter your choice (Rock, Paper, or Scissors): 
 Paper
Computer chose: Paper
It's a draw!

Explanation :

  • The program starts by defining an array of choices: Rock, Paper, and Scissors.
  • It takes user input through a Scanner.
  • It uses the Random class to randomly select a choice for the computer.
  • The program compares the user's choice with the computer's choice and prints the result (win, lose, or draw).

Solution 2: Rock, Paper, Scissors Game using switch Statements

Code:

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

public class RockPaperScissorsSwitch {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        // Get user input
        System.out.println("Enter your choice (Rock, Paper, or Scissors): ");
        String userChoice = scanner.nextLine().toLowerCase(); // Convert to lowercase for uniform comparison

        // Generate random computer choice
        int computerChoice = random.nextInt(3);
        String computerChoiceString = "";
        
        switch (computerChoice) {
            case 0:
                computerChoiceString = "rock";
                break;
            case 1:
                computerChoiceString = "paper";
                break;
            case 2:
                computerChoiceString = "scissors";
                break;
        }

        // Display computer's choice
        System.out.println("Computer chose: " + computerChoiceString);

        // Determine game outcome
        switch (userChoice) {
            case "rock":
                if (computerChoiceString.equals("scissors")) {
                    System.out.println("You win!");
                } else if (computerChoiceString.equals("rock")) {
                    System.out.println("It's a draw!");
                } else {
                    System.out.println("You lose!");
                }
                break;
            case "paper":
                if (computerChoiceString.equals("rock")) {
                    System.out.println("You win!");
                } else if (computerChoiceString.equals("paper")) {
                    System.out.println("It's a draw!");
                } else {
                    System.out.println("You lose!");
                }
                break;
            case "scissors":
                if (computerChoiceString.equals("paper")) {
                    System.out.println("You win!");
                } else if (computerChoiceString.equals("scissors")) {
                    System.out.println("It's a draw!");
                } else {
                    System.out.println("You lose!");
                }
                break;
            default:
                System.out.println("Invalid input. Please enter Rock, Paper, or Scissors.");
                break;
        }

        scanner.close();
    }
}

Output:

Enter your choice (Rock, Paper, or Scissors): 
Scissors
Compute chose: rock
You lose!
Enter your choice (Rock, Paper, or Scissors): 
 Rock
Computer chose: scissors
You win!

Explanation:

  • This version of the game uses a switch statement for both user and computer choices.
  • The user's input is converted to lowercase to standardize comparison.
  • The Random class generates the computer's choice, and another switch statement handles the result evaluation.
  • The program compares the choices and prints whether the user wins, loses, or draws based on the rules.

Java Code Editor:




Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/java/java-project-rock-paper-scissors-game.php