From 70d3f203cf1cf059f5545fd0e75dd2d567f984e8 Mon Sep 17 00:00:00 2001 From: mo Date: Thu, 6 Jun 2019 20:29:23 -0600 Subject: move main game loop out to main --- src/Q2/RockPaperScissorsLizardSpock.java | 69 +++++++++++++------------------- 1 file changed, 27 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/Q2/RockPaperScissorsLizardSpock.java b/src/Q2/RockPaperScissorsLizardSpock.java index d077f68..ec3934d 100644 --- a/src/Q2/RockPaperScissorsLizardSpock.java +++ b/src/Q2/RockPaperScissorsLizardSpock.java @@ -33,8 +33,7 @@ public class RockPaperScissorsLizardSpock { } /** - * Returns the id of the current winner. - * See PLAYER1 and PLAYER2 constants for the possible ids. + * Returns the id of the current winner. See PLAYER1 and PLAYER2 constants for the possible ids. * * @return the id of the current winner. */ @@ -58,33 +57,10 @@ public class RockPaperScissorsLizardSpock { * @param player2 id of player 2 */ public void play(int player1, int player2) { - int player1Roll = 0; - int player2Roll = 0; - int consecutiveWins = 0; - int round = 0; + this.puts("Player 1: %s", convert(player1)); + this.puts("Player 2: %s", convert(player2)); - 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); - if (this.lastWinner > 0) - this.puts("Player %d has %d consecutive wins.", this.lastWinner, this.consecutiveWins); - - if (this.lastWinner > 0 && this.consecutiveWins == 4) { - this.declareWinner(); - return; - } - } + this.incrementWinsFor(this.determineWinner(player1, player2)); } /** @@ -137,14 +113,6 @@ 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 (winner == 0) return; @@ -156,11 +124,6 @@ public class RockPaperScissorsLizardSpock { } } - 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); - } - /** * The entrypoint into the console application. * @@ -168,6 +131,28 @@ public class RockPaperScissorsLizardSpock { */ public static void main(String[] args) { RockPaperScissorsLizardSpock game = new RockPaperScissorsLizardSpock(); - game.play(RockPaperScissorsLizardSpock.PLAYER1, RockPaperScissorsLizardSpock.PLAYER2); + int round = 0; + + game.puts("Starting a new game of Rock, paper, scissors, lizard, spock..."); + while (true) { + round++; + game.newline(); + game.puts("Round: %d", round); + game.puts("--------------------------------------"); + + game.play(game.random(), game.random()); + if (game.getLastWinner() > 0) + game.puts( + "Player %d has %d consecutive wins.", game.getLastWinner(), game.getConsecutiveWins()); + + if (game.getLastWinner() > 0 && game.getConsecutiveWins() == 4) { + game.newline(); + game.puts("**************************************"); + game.puts(" The winner is player %d! ", game.getLastWinner()); + game.puts("**************************************"); + game.newline(); + break; + } + } } } -- cgit v1.2.3