Question: public static void main(String[] args) { new part3().run(args); } private int totalPlays = 0, PLAYER_1WINS = 0, PLAYER_2WINS = 0, ties = 0; private void

public static void main(String[] args) { new part3().run(args); }

private int totalPlays = 0, PLAYER_1WINS = 0, PLAYER_2WINS = 0, ties = 0;

private void run(String[] args) {

String input, result; Choice playerChoice, compChoice;

Scanner scanner = new Scanner(System.in); printPrompt(); input = scanner.nextLine();

while (!input.equals("q")) { playerChoice = Choice.getChoice(input); if (playerChoice == null) { System.out .println("Invalid input. Type either (R)OCK / (P)APER / (S)CISSOR / (L)IZARD / SPOC(K)"); } else { compChoice = Choice.randomChoice(); if (playerChoice == compChoice) { result = "It's a tie!"; ties++; } else { result = playerChoice.beats(compChoice); if (!result.isEmpty()) { result += " Player1 wins!"; PLAYER_1WINS++; } else { result = compChoice.beats(playerChoice); result += " player2 wins!"; PLAYER_2WINS++; } } totalPlays++; System.out.println(String.format( "Player picks: %s. Computer picks: %s. %s ", playerChoice, compChoice, result)); } printPrompt(); input = scanner.nextLine(); } scanner.close(); printGameResults(); }

private void printPrompt() { System.out.print("Make a choice (R/P/S/L/K): "); }

private void printGameResults() { System.out.println(String.format("Total games played: %d", totalPlays)); if (totalPlays != 0) { System.out.println(String.format("Computer wins: %d (%d%%)", PLAYER_2WINS, ((PLAYER_2WINS * 100) / totalPlays))); System.out.println(String.format("Player Wins: %d (%d%%)", PLAYER_1WINS, ((PLAYER_1WINS * 100) / totalPlays))); System.out.println(String.format("Ties: %d (%d%%)", ties, ((ties * 100) / totalPlays))); } }

private enum Choice { ROCK("R"), PAPER("P"), SCISSORS("S"), LIZARD("L"), SPOCK("K");

private static Random random = new Random(System.currentTimeMillis());

private static Map choiceMap = new HashMap();

private String shortcut;

private Choice winsAgainst[];

private String actions[], comments[];

static { for (Choice ch : EnumSet.allOf(Choice.class)) { choiceMap.put(ch.toString(), ch); choiceMap.put(ch.shortcut, ch); }

ROCK.actions = new String[] { "crushes", "crushes" }; ROCK.winsAgainst = new Choice[] { SCISSORS, LIZARD };

PAPER.actions = new String[] { "covers", "disproves" }; PAPER.winsAgainst = new Choice[] { ROCK, SPOCK };

SCISSORS.actions = new String[] { "cut", "decapitate" }; SCISSORS.winsAgainst = new Choice[] { PAPER, LIZARD };

LIZARD.actions = new String[] { "poisons", "eats" }; LIZARD.winsAgainst = new Choice[] { SPOCK, PAPER };

SPOCK.actions = new String[] { "smashes", "vaporizes" }; SPOCK.winsAgainst = new Choice[] { SCISSORS, ROCK };

String format = "%c%s %s %s."; for (Choice ch : EnumSet.allOf(Choice.class)) { ch.comments = new String[2]; for (int i = 0; i < 2; ++i) { ch.comments[i] = String.format(format, ch.name().charAt(0), ch.name().toLowerCase().substring(1), ch.actions[i], ch.winsAgainst[i].name() .toLowerCase()); } } }

private Choice(String shortcut) { this.shortcut = shortcut; }

private String beats(Choice choice) { if (winsAgainst[0] == choice) return comments[0]; else if (winsAgainst[1] == choice) return comments[1]; else return ""; }

private static Choice randomChoice() { return values()[random.nextInt(5)]; }

private static Choice getChoice(String str) { return str == null ? null : choiceMap.get(str.toUpperCase()); } } }

for this code whic in java

i would like to have the out put like this

Welcome to the game of Rock Paper Scissors Lizard Spock Here are the rules: Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock (and as it always has) Rock crushes scissors Ready? Then let's begin! Player 1, enter your choice ( rock, paper, scissors, lizard, Spock ): rock OK, you chose rock Player 2 (computer) chooses rock It's a draw Play again (yes/no)? yes Player 1, enter your choice ( rock, paper, scissors, lizard, Spock ): spock OK, you chose spock Player 2 (computer) chooses lizard Lizard poisons Spock; Player 2 wins Play again (yes/no)? yes Player 1, enter your choice ( rock, paper, scissors, lizard, Spock ): banana <--- invalid input Invalid choice "banana"; try again. Player 1, enter your choice ( rock, paper, scissors, lizard, Spock ): Paper OK, you chose paper Player 2 (computer) chooses scissors Scissors cut paper; Player 2 wins Play again (yes/no)? yes Player 1, enter your choice ( rock, paper, scissors, lizard, spock ): <--- invalid input (empty string) Invalid choice ""; try again. Player 1, enter your choice ( rock, paper, scissors, lizard, Spock ): SCISSORS OK, you chose scissors Player 2 (computer) chooses paper Scissors cut paper; Player 1 wins Play again (yes/no)? yes Player 1, enter your choice ( rock, paper, scissors, lizard, Spock ): lIZARD OK, you chose lizard Player 2 (computer) chooses scissors Scissors decaptiate lizard; Player 2 wins Play again (yes/no)? no

which can give the user an opition to play next if he want .. the code above work good but ther is Play again (yes/no)? no.. so could you add that opition by using the same code in java

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!