summaryrefslogtreecommitdiff
path: root/src/Q2/RockPaperScissorsLizardSpock.java
blob: 1715c4e8c3dd3cac47a281dc0f7ee5154a8f8691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * Assignment 2, COMP268 Class: RockPaperScissorsLizardSpock.java
 *
 * @description Provide a class to play the game rock, paper, scissors, lizard spock.
 * @author: mo khan Student ID: 3431709
 * @date Jun 6, 2019
 * @version 1.0
 */
package Q2;

import java.util.*;

public class RockPaperScissorsLizardSpock {
  private int consecutiveWins = 0;
  private int lastWinner;

  public static final int PLAYER1 = 1;
  public static final int PLAYER2 = 2;

  public static final int LIZARD = 4;
  public static final int PAPER = 2;
  public static final int ROCK = 1;
  public static final int SCISSORS = 3;
  public static final int SPOCK = 5;

  /**
   * Returns the # of consecutive wins for the current winner.
   *
   * @return the # of consecutive wins for the current winner.
   */
  public int getConsecutiveWins() {
    return this.consecutiveWins;
  }

  /**
   * Returns the id of the current winner. See PLAYER1 and PLAYER2 constants for the possible ids.
   *
   * @return the id of the current winner.
   */
  public int getLastWinner() {
    return this.lastWinner;
  }

  /**
   * Returns a random integer between 1 - 5
   *
   * @return a random integer between 1 - 5
   */
  public int random() {
    return new Random().nextInt(4) + 1;
  }

  /**
   * Plays the game until a winner has 4 consecutive wins.
   *
   * @param player1 the choice made by player 1
   * @param player2 the choice made by player 2
   */
  public void play(int player1, int player2) {
    this.puts("Player 1: %s", convert(player1));
    this.puts("Player 2: %s", convert(player2));

    this.incrementWinsFor(this.determineWinner(player1, player2));
  }

  /**
   * Converts a integer to the equivalent name.
   *
   * @param i the integer to convert to a name.
   * @return the name for the corresponding integer value.
   */
  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 incrementWinsFor(int winner) {
    if (winner == 0) return;

    if (this.lastWinner == winner) {
      this.consecutiveWins++;
    } else if (winner > 0) {
      this.lastWinner = winner;
      this.consecutiveWins = 1;
    }
  }

  /**
   * The entrypoint into the console application.
   *
   * @param args an argument vector that represents the command line arguments
   */
  public static void main(String[] args) {
    RockPaperScissorsLizardSpock game = new RockPaperScissorsLizardSpock();
    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;
      }
    }
  }
}