package Q2; import java.util.*; public class RockPaperScissorsLizardSpock { private int consecutiveWins = 0; private int lastWinner; public static final int LIZARD = 4; public static final int PAPER = 2; public static final int PLAYER1 = 1; public static final int PLAYER2 = 2; public static final int ROCK = 1; public static final int SCISSORS = 3; public static final int SPOCK = 5; public int getConsecutiveWins() { return this.consecutiveWins; } public int getLastWinner() { return this.lastWinner; } public int random() { return new Random().nextInt(4) + 1; } public void play(int player1, int player2) { int player1Roll = 0; int player2Roll = 0; int consecutiveWins = 0; int round = 0; this.puts("Staring a new game of Rock, paper, scissors, lizard, spock..."); while (true) { round++; player1Roll = random(); player2Roll = random(); this.newline(); this.puts("Round: %d", round); this.puts("Player 1: %s", convert(player1Roll)); this.puts("Player 2: %s", convert(player2Roll)); int winner = this.determineWinner(player1Roll, player2Roll); this.delcareRoundWinner(winner); this.incrementWinsFor(winner); this.puts("Player %d has %d consecutive wins.", this.lastWinner, this.consecutiveWins); if (this.consecutiveWins == 4) { this.declareWinner(); return; } } } public static String convert(int i) { switch (i) { case ROCK: return "rock"; case PAPER: return "paper"; case SCISSORS: return "scissor"; case LIZARD: return "lizard"; case SPOCK: return "spock"; default: return "error"; } } private int determineWinner(int player1Roll, int player2Roll) { if (player1Roll == player2Roll) return 0; switch (player1Roll) { case SCISSORS: return (player2Roll == PAPER || player2Roll == LIZARD) ? PLAYER1 : PLAYER2; case PAPER: return (player2Roll == ROCK || player2Roll == SPOCK) ? PLAYER1 : PLAYER2; case ROCK: return (player2Roll == LIZARD || player2Roll == SCISSORS) ? PLAYER1 : PLAYER2; case LIZARD: return (player2Roll == SPOCK || player2Roll == PAPER) ? PLAYER1 : PLAYER2; case SPOCK: return (player2Roll == SCISSORS || player2Roll == ROCK) ? PLAYER1 : PLAYER2; default: return 0; } } private void puts(String message, Object... args) { System.out.println(String.format(message, args)); } private void newline() { System.out.println(); } private void declareWinner() { this.newline(); this.puts("***********************************"); this.puts("The winner of the game is player %d!", this.lastWinner); this.puts("***********************************"); this.newline(); } private void incrementWinsFor(int winner) { if (this.lastWinner == winner) { this.consecutiveWins++; } else if (winner > 0) { this.lastWinner = winner; this.consecutiveWins = 1; } } private void delcareRoundWinner(int winner) { if (winner == 0) this.puts("Player 1 and Player 2 tie!"); else this.puts("The winner of this round is player %d!", winner); } public static void main(String[] args) { Scanner in = new Scanner(System.in); RockPaperScissorsLizardSpock game = new RockPaperScissorsLizardSpock(); game.play(RockPaperScissorsLizardSpock.PLAYER1, RockPaperScissorsLizardSpock.PLAYER2); } }