diff options
| -rw-r--r-- | src/Q2/RockPaperScissorsLizardSpock.java | 70 |
1 files changed, 49 insertions, 21 deletions
diff --git a/src/Q2/RockPaperScissorsLizardSpock.java b/src/Q2/RockPaperScissorsLizardSpock.java index 0c59ab9..4dee486 100644 --- a/src/Q2/RockPaperScissorsLizardSpock.java +++ b/src/Q2/RockPaperScissorsLizardSpock.java @@ -27,49 +27,55 @@ public class RockPaperScissorsLizardSpock { } public void play(int player1, int player2) { - int player1Roll = random(); - int player2Roll = random(); + 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: %d", player1Roll); - this.puts("Player 2: %d", player2Roll); + this.puts("Player 1: %s", convert(player1Roll)); + this.puts("Player 2: %s", convert(player2Roll)); int winner = this.determineWinner(player1Roll, player2Roll); - this.puts("The winner of this round is player %d!", winner); + this.delcareRoundWinner(winner); - if (this.lastWinner == winner) { - this.consecutiveWins++; - } else { - this.lastWinner = winner; - this.consecutiveWins = 1; - } + this.incrementWinsFor(winner); this.puts("Player %d has %d consecutive wins.", this.lastWinner, this.consecutiveWins); if (this.consecutiveWins == 4) { - this.newline(); - this.puts("***********************************"); - this.puts("The winner of the game is player %d!", this.lastWinner); - this.puts("***********************************"); - this.newline(); + this.declareWinner(); return; } } } public static String convert(int i) { - return String.format("Player %d", 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 PLAYER1; - } + public int determineWinner(int player1Roll, int player2Roll) { + if (player1Roll == player2Roll) return 0; + if (((player1Roll - player2Roll) % 5) < 3) return PLAYER1; + return PLAYER2; } @@ -81,6 +87,28 @@ public class RockPaperScissorsLizardSpock { 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(); |
