In: Computer Science
JAVA : Design and implement an application that plays the Rock-Paper-Scissors game against the computer. When played between two people, each person picks one of three options (usually shown by a hand gesture) at the same time, and a winner is determined. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options (without revealing it) and then prompt for the user’s selection. At that point, the program reveals both choices and prints a statement indicating whether the user won, the computer won, or it was a tie. Continue playing until the user chooses to stop by saying either ‘y’ or ‘n’, where ‘y’ means ‘yes continue playing’, and ‘n’ means ‘no stop playing’. Then print the number of user wins, losses, and ties.
import java.util.Random;
import java.util.Scanner;
public class RockScissorPaperGame {
public static final String ROCK = "ROCK";
public static final String PAPER = "PAPER";
public static final String SCISSORS = "SCISSORS";
public static void main(String args[]) {
int totalGames = 0, wins = 0, losses = 0,ties=0;
do {
totalGames++;
System.out.println("Enter any one of the following inputs: ");
System.out.println("ROCK");
System.out.println("PAPER");
System.out.println("SCISSORS");
System.out.println();
String playerMove = getPlayerMove().toUpperCase();
String computerMove = getComputerMove();
if (playerMove.equals(computerMove)) {
System.out.println("Its tie !!");
ties++;
} // if player selects ROCK
else if (playerMove.equals(ROCK)) {
if (computerMove.equals(PAPER)) {
System.out.println("Computer wins");
losses++;
} else {
System.out.println("Player wins");
wins++;
}
} // if player selects PAPER
else if (playerMove.equals(PAPER)) {
if (computerMove.equals(SCISSORS)) {
System.out.println("Computer wins");
losses++;
} else {
System.out.println("Player wins");
wins++;
}
} // if player selects SCISSORS
else {
if (computerMove.equals(ROCK)) {
System.out.println("Computer wins");
losses++;
} else {
System.out.println("Player wins");
wins++;
}
}
} while (playAgain());
System.out.println("Total Games: "+totalGames);
System.out.println("Wons: "+wins);
System.out.println("Losses: "+losses);
System.out.println("Ties: "+ties);
}
public static String getComputerMove() {
String computermove;
Random random = new Random();
int input = random.nextInt(3) + 1;
if (input == 1) {
computermove = ROCK;
} else if (input == 2) {
computermove = PAPER;
} else {
computermove = SCISSORS;
}
System.out.println("Computer Pick is: " + computermove);
System.out.println();
return computermove;
}
public static String getPlayerMove() {
Scanner in = new Scanner(System.in);
String input = in.next();
String playermove = input.toUpperCase();
System.out.println("Player move is: " + playermove);
return playermove;
}
public static boolean playAgain() {
Scanner sc = new Scanner(System.in);
System.out.print("Do you want to play again? ");
String userInput = sc.nextLine();
userInput = userInput.toUpperCase();
return userInput.charAt(0) == 'Y';
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME