Code Snippet #5: Rock Paper Scissors! Shoot!

So, todays code Snippet is made in java, and it's the classic game of rock paper scissors. Maybe I might have the next few snippets be various classic games.

so this is how to make rock paper scissors in Java!

anyways here is the code snippet! 

 how to make rock paper scissors in Java

import java.util.Scanner;
public class RockPaperScissors{
  public static void main(String[] args){
    try (Scanner input = new Scanner(System.in)) {
      System.out.println("Rock, Paper, Scissors!");
      System.out.print("Choose your weapon: (rock, paper, scissors): ");
      String userChoice = input.nextLine();

      String[] choices = {"rock", "paper", "scissors"};
      int computerChoice = (int) (Math.random() * 3);
      String computerChoiceStr = choices[computerChoice];

      System.out.println("Computer chooses: " + computerChoiceStr);

      if (userChoice.equals(computerChoiceStr)) {
        System.out.println("It's a tie!");
      } else if ((userChoice.equals("rock") && computerChoiceStr.equals("scissors")) ||
                (userChoice.equals("paper") && computerChoiceStr.equals("rock")) ||
                (userChoice.equals("scissors") && computerChoiceStr.equals("paper"))) {
        System.out.println("You win!");
      } else {
        System.out.println("Computer wins!");
      }
    }
  }
}

and now here is Code-master.

Code Exploration:

User Input and Initialization:

The program begins by creating a Scanner object named input to capture user input from the command line.

The user is prompted to choose their weapon (rock, paper, or scissors) using System.out.print and input.nextLine().

The choices are stored in an array named choices, and a random index is generated to simulate the computer's choice. The computer's choice is then converted to a string (computerChoiceStr).

Comparison and Outcome Determination:

The user's choice and the computer's choice are compared using a series of if-else statements to determine the winner or if the game results in a tie.

If the user's choice equals the computer's choice, it's a tie, and the corresponding message is displayed.

If the user's choice beats the computer's choice based on the game rules, the user wins. Otherwise, the computer wins.

Understanding the Conditions:

The conditions for the user to win are based on the classic Rock, Paper, Scissors rules: rock beats scissors, paper beats rock, and scissors beat paper.

The program uses string comparisons (equals) to evaluate these conditions.

Outcome Display:

The final step involves displaying the computer's choice and the outcome of the game using System.out.println.

Execution:

When the program is executed, the user interacts with the console by entering their choice.

The computer randomly selects its choice, and the program evaluates the conditions to determine the winner.

Application and Learning:

Randomization: Explore how the use of Math.random() facilitates the random selection of the computer's choice.

Conditional Logic: Gain a deeper understanding of how conditional statements are employed to navigate through different game scenarios.

Conclusion:

This basic implementation showcases the core mechanics of Rock Paper Scissors in Java. To enhance the experience, consider these additions:

Best-of-Three: Introduce a scoring system to determine the overall winner across multiple rounds.

Enhanced User Interface: Create a visual interface using Java Swing or JavaFX to provide a more immersive experience.

Customizable Options: Allow players to choose the number of rounds or introduce new weapon options.

AI Opponents: Explore strategies for creating more challenging computer opponents.

The possibilities are endless! Let the games begin!

Comments

Quote of the day