diff options
| author | mo <mo.khan@gmail.com> | 2019-06-04 20:40:09 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2019-06-04 20:40:09 -0600 |
| commit | e8771fc7ab2faec769aff215ca11e01e988aed99 (patch) | |
| tree | d13e5a6d7dedabca0fa6553371d5582d03aa4a24 | |
| parent | 9af5a72706c3180c57422f296ad68b2cff69cf3e (diff) | |
build rock paper scissors game
| -rw-r--r-- | src/Q2/RockPaperScissorsLizardSpock.java | 55 |
1 files changed, 50 insertions, 5 deletions
diff --git a/src/Q2/RockPaperScissorsLizardSpock.java b/src/Q2/RockPaperScissorsLizardSpock.java index 2fbfcfa..0c59ab9 100644 --- a/src/Q2/RockPaperScissorsLizardSpock.java +++ b/src/Q2/RockPaperScissorsLizardSpock.java @@ -4,12 +4,12 @@ import java.util.Random; import java.util.Scanner; public class RockPaperScissorsLizardSpock { - private int consecutiveWins; + private int consecutiveWins = 0; private int lastWinner; public static final int LIZARD = 4; public static final int PAPER = 2; - public static final int PLAYER1 = 0; - public static final int PLAYER2 = 1; + 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; @@ -29,15 +29,60 @@ public class RockPaperScissorsLizardSpock { public void play(int player1, int player2) { int player1Roll = random(); int player2Roll = random(); + int consecutiveWins = 0; + int round = 0; + + this.puts("Staring a new game of Rock, paper, scissors, lizard, spock..."); + while (true) { + round++; + this.newline(); + this.puts("Round: %d", round); + this.puts("Player 1: %d", player1Roll); + this.puts("Player 2: %d", player2Roll); + + int winner = this.determineWinner(player1Roll, player2Roll); + this.puts("The winner of this round is player %d!", winner); + + if (this.lastWinner == winner) { + this.consecutiveWins++; + } else { + this.lastWinner = winner; + this.consecutiveWins = 1; + } + 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(); + return; + } + } } public static String convert(int i) { - return ""; + return String.format("Player %d", i); + } + + private int determineWinner(int player1Roll, int player2Roll) { + if (player1Roll > player2Roll) { + return PLAYER1; + } + return PLAYER2; + } + + private void puts(String message, Object... args) { + System.out.println(String.format(message, args)); + } + + private void newline() { + System.out.println(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); - System.out.println("Hi"); RockPaperScissorsLizardSpock game = new RockPaperScissorsLizardSpock(); game.play(RockPaperScissorsLizardSpock.PLAYER1, RockPaperScissorsLizardSpock.PLAYER2); } |
