Design and implement an application that plays Rock, Paper, Scissors, Lizard, Sp
ID: 3758601 • Letter: D
Question
Design and implement an application that plays Rock, Paper, Scissors, Lizard, Spock against the computer. The user should select Rock, Paper, Scissors, Lizard or Spock. The program should randomly select a number. The program should then reveal both choices and prints a statement indicating whether the user won, computer won or there was a tie. Continue playing until the user chooses to stop and then print the number of user wins, losses and ties. The class name should be “RPSLS”. You could use multiple classes, but only one is required.
The program should request a user to enter “rock”, “paper” , “scissors” , “Lizard ”Or “Spock”. The program should keep a running total of wins/losses/ties. The game should continue until the user wants to stop playing [unless the user selected a best of option
Explanation / Answer
working java code:
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class RPSLS {
public enum Move {
Rock, Paper, Scissors, Lizard, Spock;
static {
Rock.willBeat(Scissors, Lizard);
Paper.willBeat(Rock,Spock);
Scissors.willBeat(Paper, Lizard);
Lizard.willBeat(Spock,Paper);
Spock.willBeat(Rock,Scissors);
}
private Move[] ibeat;
private void willBeat(Move...moves) {
ibeat = moves;
}
public boolean beats(Move move) {
// use binary search in case someone wants to set up crazy rules.
return Arrays.binarySearch(ibeat, move) >= 0;
}
}
private static final String MOVEPROMPT = buildOptions();
private static String buildOptions() {
StringBuilder sb = new StringBuilder();
sb.append(" -> ");
for (Move m : Move.values()) {
sb.append(m.ordinal() + 1).append(") ").append(m.name()).append(" ");
}
// include a quit option.
sb.append(" q) Quit");
return sb.toString();
}
private static final char prompt(Scanner scanner, String prompt, char defval) {
System.out.print(prompt + ": ");
String input = scanner.nextLine();
if (input.isEmpty()) {
return defval;
}
return input.charAt(0);
}
private static boolean playAgain(Scanner scanner) {
return ('n' != prompt(scanner, " Play Again (y/n)?", 'y'));
}
private static Move getUSERMove(Scanner scanner) {
do {
char val = prompt(scanner, MOVEPROMPT, 'q');
if ('q' == val) {
return null;
}
int num = (val - '0') - 1;
if (num >= 0 && num < Move.values().length) {
return Move.values()[num];
}
System.out.println("Invalid move " + val);
} while (true);
}
public static void main(String[] args) {
final Random rand = new Random();
final Move[] moves = Move.values();
final Scanner scanner = new Scanner(System.in);
int htotal = 0;
int ctotal = 0;
do {
System.out.println(" Best of 3.... Go!");
int hscore = 0;
int cscore = 0;
bestofthree: do {
final Move computer = moves[rand.nextInt(moves.length)];
final Move user = getUSERMove(scanner);
if (user == null) {
System.out.println("USER quits Best-of-3...");
break bestofthree;
}
if (user == computer) {
System.out.printf(" DRAW... play again!! (%s same as %s) ", user, computer);
} else if (user.beats(computer)) {
hscore++;
System.out.printf(" USER beats Computer (%s beats %s) ", user, computer);
} else {
cscore++;
System.out.printf(" COMPUTER beats USER (%s beats %s) ", computer, user);
}
} while (hscore != 2 && cscore != 2);
if (hscore == 2) {
htotal++;
} else {
ctotal++;
}
String winner = hscore == 2 ? "USER" : "Computer";
System.out.printf(" %s **** %s wins Best-Of-Three (USER=%d, Computer=%d - game total is USER=%d Computer=%d) ",
winner.toUpperCase(), winner, hscore, cscore, htotal, ctotal);
} while (playAgain(scanner));
System.out.printf("Thank you for playing. The final game score was USER=%d and Computer=%d ", htotal, ctotal);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.