Rock Paper Scissor Game in Java

 Here's an example of a simple Rock-Paper-Scissors game implemented in Java:

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

public class Exercise_2_RockPaperScissors {
public static void main(String[] args) {
String[] choices = {"rock", "paper", "scissors"};

Scanner sc = new Scanner(System.in);
Random random = new Random();

System.out.println("Let's play Rock, Paper, Scissors");
System.out.println("Enter your choice (rock, paper or scissors): ");
String playerChoice = sc.nextLine().toLowerCase();

int ComputerIndex = random.nextInt(3);
String computerChoice = choices[ComputerIndex];

System.out.println("Computer chose: " + computerChoice);


// Determine the winner
if (playerChoice.equals(computerChoice)){
System.out.println("It's tie!");
} else if ((playerChoice.equals("rock") && computerChoice.equals("scissors")) ||
(playerChoice.equals("paper") && computerChoice.equals("rock")) ||
(playerChoice.equals("scissor")) && computerChoice.equals("rock")) {
System.out.println("You win!");
} else {
System.out.println("Computer wins!");
}
}
}

In this example, the program prompts the user to enter their choice (rock, paper, or scissors) using a Scanner. The computer's choice is generated randomly using the Random class.

The program then compares the player's choice with the computer's choice to determine the winner. If both choices are the same, it's a tie. Otherwise, the winning conditions for the player are checked: rock beats scissors, paper beats rock, and scissors beat paper. If none of these conditions are met, the computer wins.

Note: This is a basic implementation and doesn't include error handling or validation for user input.

Comments