In: Computer Science
Design and implement an Android 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 seek for the user’s selection (using your choice of an object such as editText, button etc., ). At that point, the program reveals both choices and shows a message (as a Toast or another Text field) indicating whether the user won, the computer won, or it was a tie.
Notes:
Submission:
PreviousNext
Solution:
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';
}
}
**********************************Thank You******************************