Rock, Paper, Scissors Game in Java

 

Welcome to the Rock, Paper, Scissors game. The objective of the game is simple: you play against the computer to choose one of three options – Rock, Paper, or Scissors. The rules are:

  • Rock beats Scissors
  • Scissors beats Paper
  • Paper beats Rock

The game is a simple turn-based competition, where both you and the computer make a choice, and the winner is determined based on the rules above.

Java Code: Rock, Paper, Scissors


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

public class RockPaperScissors {

    public static void main(String[] args) {
        // Create a scanner object to get input from the user
        Scanner scanner = new Scanner(System.in);

        // Create an array for the options
        String[] choices = {"Rock", "Paper", "Scissors"};

        // Display game instructions
        System.out.println("Welcome to Rock, Paper, Scissors!");
        System.out.println("Please choose: Rock, Paper or Scissors.");
        
        // User input
        String userChoice = scanner.nextLine();
        // Validate user input
        if (!userChoice.equalsIgnoreCase("Rock") && 
            !userChoice.equalsIgnoreCase("Paper") && 
            !userChoice.equalsIgnoreCase("Scissors")) {
            System.out.println("Invalid choice. Please choose Rock, Paper, or Scissors.");
            return;
        }

        // Generate computer's choice
        Random random = new Random();
        String computerChoice = choices[random.nextInt(3)];

        // Display choices
        System.out.println("You chose: " + userChoice);
        System.out.println("Computer chose: " + computerChoice);

        // Determine the winner
        if (userChoice.equalsIgnoreCase(computerChoice)) {
            System.out.println("It's a tie!");
        } else if ((userChoice.equalsIgnoreCase("Rock") && computerChoice.equalsIgnoreCase("Scissors")) ||
                   (userChoice.equalsIgnoreCase("Scissors") && computerChoice.equalsIgnoreCase("Paper")) ||
                   (userChoice.equalsIgnoreCase("Paper") && computerChoice.equalsIgnoreCase("Rock"))) {
            System.out.println("You win!");
        } else {
            System.out.println("Computer wins!");
        }
    }
}
            

Program Explanation

This is a simple console-based implementation of the Rock, Paper, Scissors game written in Java. Here’s a breakdown of how the program works:

  • Scanner Object: This object is used to read input from the user (i.e., the choice of Rock, Paper, or Scissors).
  • Random Object: Used to randomly select the computer’s choice from the options array.
  • Array: The program uses an array choices containing the three possible game choices (Rock, Paper, Scissors).
  • Choice Validation: The program checks if the user’s input is valid. If it is not, the program terminates early with an error message.
  • Winning Logic: The game logic compares the player’s choice to the computer’s choice using if-else statements to determine the winner based on the rules of the game.

How to Run the Program

To run this Java program, follow these steps:

  1. Ensure you have Java installed on your system. You can download it from Oracle’s official website.
  2. Create a new text file and copy the Java code provided above.
  3. Save the file with the name RockPaperScissors.java.
  4. Open the command line/terminal and navigate to the folder where the file is saved.
  5. Compile the Java code by running the following command:
    javac RockPaperScissors.java
  6. Run the compiled program using this command:
    java RockPaperScissors
  7. The program will prompt you to input your choice (Rock, Paper, or Scissors) and then display the result.
© 2024 Learn Programming. All rights reserved.

 

Leave a Reply

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