master
1/**
2 * Assignment 2, COMP268 Class: RockPaperScissorsLizardSpock.java
3 *
4 * @description Provide a class to play the game rock, paper, scissors, lizard spock.
5 * @author: mo khan Student ID: 3431709
6 * @date Jun 6, 2019
7 * @version 1.0
8 */
9package Q2;
10
11import java.util.*;
12
13public class RockPaperScissorsLizardSpock {
14 private int consecutiveWins = 0;
15 private int lastWinner;
16
17 public static final int PLAYER1 = 1;
18 public static final int PLAYER2 = 2;
19
20 public static final int LIZARD = 4;
21 public static final int PAPER = 2;
22 public static final int ROCK = 1;
23 public static final int SCISSORS = 3;
24 public static final int SPOCK = 5;
25
26 /**
27 * Returns the # of consecutive wins for the current winner.
28 *
29 * @return the # of consecutive wins for the current winner.
30 */
31 public int getConsecutiveWins() {
32 return this.consecutiveWins;
33 }
34
35 /**
36 * Returns the id of the current winner. See PLAYER1 and PLAYER2 constants for the possible ids.
37 *
38 * @return the id of the current winner.
39 */
40 public int getLastWinner() {
41 return this.lastWinner;
42 }
43
44 /**
45 * Returns a random integer between 1 - 5
46 *
47 * @return a random integer between 1 - 5
48 */
49 public int random() {
50 return new Random().nextInt(4) + 1;
51 }
52
53 /**
54 * Plays the game until a winner has 4 consecutive wins.
55 *
56 * @param player1 the choice made by player 1
57 * @param player2 the choice made by player 2
58 */
59 public void play(int player1, int player2) {
60 this.puts("Player 1: %s", convert(player1));
61 this.puts("Player 2: %s", convert(player2));
62
63 this.incrementWinsFor(this.determineWinner(player1, player2));
64 }
65
66 /**
67 * Converts a integer to the equivalent name.
68 *
69 * @param i the integer to convert to a name.
70 * @return the name for the corresponding integer value.
71 */
72 public static String convert(int i) {
73 switch (i) {
74 case ROCK:
75 return "rock";
76 case PAPER:
77 return "paper";
78 case SCISSORS:
79 return "scissor";
80 case LIZARD:
81 return "lizard";
82 case SPOCK:
83 return "spock";
84 default:
85 return "error";
86 }
87 }
88
89 private int determineWinner(int player1Roll, int player2Roll) {
90 if (player1Roll == player2Roll) return 0;
91
92 switch (player1Roll) {
93 case SCISSORS:
94 return (player2Roll == PAPER || player2Roll == LIZARD) ? PLAYER1 : PLAYER2;
95 case PAPER:
96 return (player2Roll == ROCK || player2Roll == SPOCK) ? PLAYER1 : PLAYER2;
97 case ROCK:
98 return (player2Roll == LIZARD || player2Roll == SCISSORS) ? PLAYER1 : PLAYER2;
99 case LIZARD:
100 return (player2Roll == SPOCK || player2Roll == PAPER) ? PLAYER1 : PLAYER2;
101 case SPOCK:
102 return (player2Roll == SCISSORS || player2Roll == ROCK) ? PLAYER1 : PLAYER2;
103 default:
104 return 0;
105 }
106 }
107
108 private void puts(String message, Object... args) {
109 System.out.println(String.format(message, args));
110 }
111
112 private void newline() {
113 System.out.println();
114 }
115
116 private void incrementWinsFor(int winner) {
117 if (winner == 0) return;
118
119 if (this.lastWinner == winner) {
120 this.consecutiveWins++;
121 } else if (winner > 0) {
122 this.lastWinner = winner;
123 this.consecutiveWins = 1;
124 }
125 }
126
127 /**
128 * The entrypoint into the console application.
129 *
130 * @param args an argument vector that represents the command line arguments
131 */
132 public static void main(String[] args) {
133 RockPaperScissorsLizardSpock game = new RockPaperScissorsLizardSpock();
134 int round = 0;
135
136 game.puts("Starting a new game of Rock, paper, scissors, lizard, spock...");
137 while (true) {
138 round++;
139 game.newline();
140 game.puts("Round: %d", round);
141 game.puts("--------------------------------------");
142
143 game.play(game.random(), game.random());
144 if (game.getLastWinner() > 0)
145 game.puts(
146 "Player %d has %d consecutive wins.", game.getLastWinner(), game.getConsecutiveWins());
147
148 if (game.getLastWinner() > 0 && game.getConsecutiveWins() == 4) {
149 game.newline();
150 game.puts("**************************************");
151 game.puts(" The winner is player %d! ", game.getLastWinner());
152 game.puts("**************************************");
153 game.newline();
154 break;
155 }
156 }
157 }
158}