-
Notifications
You must be signed in to change notification settings - Fork 1
/
Yahtzee.java
353 lines (298 loc) · 8.89 KB
/
Yahtzee.java
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* File: Yahtzee.java
* ------------------
* This program will eventually play the Yahtzee game.
*/
import acm.io.*;
import acm.program.*;
import acm.util.*;
public class Yahtzee extends GraphicsProgram implements YahtzeeConstants {
public static void main(String[] args) {
new Yahtzee().start(args);
}
public void run() {
IODialog dialog = getDialog();
nPlayers = dialog.readInt("Enter number of players");
playerNames = new String[nPlayers];
for (int i = 1; i <= nPlayers; i++) {
playerNames[i - 1] = dialog.readLine("Enter name for player " + i);
}
display = new YahtzeeDisplay(getGCanvas(), playerNames);
initializeArrays();
player = 1;
playGame();
}
// Take the array of player names with a length defined as nPlayers and sets
// the current player as the one at an indicated index
private void setCurrentPlayer() {
String currentPlayer = playerNames[player - 1];
display.printMessage(currentPlayer + "’s turn. Roll the dice!");
}
/*
* Creates a 2D array that stores the used and unused categories for each player
* so no one can re-use any category, as well as the player's total score array
* by setting all the stored values to zero initially
*/
private void initializeArrays() {
usedCategories = new int[nPlayers][N_CATEGORIES];
for (int i = 0; i < nPlayers; i++) {
for (int y = 0; y < N_CATEGORIES; y++) {
usedCategories[i][y] = 0;
}
}
playersTotalScore = new int[nPlayers];
for (int i = 0; i < nPlayers; i++) {
playersTotalScore[i] = 0;
}
upperScoreUpdate = new int[nPlayers];
for (int i = 0; i < nPlayers; i++) {
upperScoreUpdate[i] = 0;
}
lowerScoreUpdate = new int[nPlayers];
for (int i = 0; i < nPlayers; i++) {
lowerScoreUpdate[i] = 0;
}
}
// Plays the game immediately proceeding the run() method
private void playGame() {
for (int i = 0; i < (nPlayers * N_SCORING_CATEGORIES); i++) {
setCurrentPlayer();
firstRoll();
secondAndThirdRoll();
selectACategory();
setNextPlayer();
}
declareWinner();
}
// Takes the array of possible dice values for the assigned dice
// and creates a random number so as to simulate randomized rolls.
// only for the first roll.
private void firstRoll() {
display.waitForPlayerToClickRoll(player);
for (int i = 0; i < N_DICE; i++) {
diceVals[i] = rgen.nextInt(1, 6);
}
display.displayDice(diceVals);
}
// Proceeds the first roll() method and urges user to select dice and roll
// again.
private void secondAndThirdRoll() {
for (int i = 0; i < 2; i++) {
display.waitForPlayerToSelectDice();
for (int j = 0; j < N_DICE; j++) {
if (display.isDieSelected(j) == true) {
diceVals[j] = rgen.nextInt(1, 6);
}
}
display.displayDice(diceVals);
}
}
// Allows the user to select a category after the third roll. Also checks if the
// dice roll is valid
private void selectACategory() {
category = display.waitForPlayerToSelectCategory();
if (usedCategories[player - 1][category] == 1) {
while (usedCategories[player - 1][category] == 1) {
display.printMessage("This category has been used. Please choose another.");
category = display.waitForPlayerToSelectCategory();
}
}
if (checkCategory(category) == true) {
if (category <= 6) {
for (int k = 0; k < diceVals.length; k++) {
if (diceVals[k] == category) {
Score += diceVals[k];
}
}
} else if (category == 9 || category == 10 || category == 15) {
for (int j = 0; j < diceVals.length; j++) {
Score += diceVals[j];
}
} else if (category >= 11 || category <= 14) {
switch (category) {
case 11:
Score = 25;
break;
case 12:
Score = 30;
break;
case 13:
Score = 40;
break;
case 14:
Score = 50;
break;
}
}
} else {
Score = 0;
}
display.updateScorecard(category, player, Score);
if (category <= 6) {
upperScoreUpdate[player - 1] += Score;
updateUpperTotal();
Score = 0;
}
if (category >= 9 && category <= 15) {
lowerScoreUpdate[player - 1] += Score;
updateLowerTotal();
Score = 0;
}
}
// This method is called inside selectACategory() and it checks if the dice
// rolled
// is valid for whatever category the user selects
private boolean checkCategory(int category) {
if (category <= 6) {
usedCategories[player - 1][category] = 1;
for (int i = 0; i < N_DICE; i++) {
if (diceVals[i] == category) {
return true;
}
}
} else if (category == 9) {
usedCategories[player - 1][category] = 1;
for (int j = 0; j < N_DICE; j++) {
for (int k = j + 1; k < N_DICE; k++) {
for (int l = k + 1; l < N_DICE; l++) {
if (diceVals[j] == diceVals[k] && diceVals[k] == diceVals[l])
return true;
}
}
}
} else if (category == 10) {
usedCategories[player - 1][category] = 1;
int fourOfAKindCounter = 0;
for (int i = 0; i < N_DICE - 1; i++) {
if (diceVals[i] == diceVals[i + 1]) {
fourOfAKindCounter++;
if (fourOfAKindCounter >= 3) {
return true;
}
}
}
} else if (category == 11) {
usedCategories[player - 1][category] = 1;
int threeSame = 0;
for (int j = 0; j < N_DICE; j++) {
for (int k = j + 1; k < N_DICE; k++) {
for (int l = k + 1; l < N_DICE; l++) {
if (diceVals[j] == diceVals[k] && diceVals[k] == diceVals[l]) {
threeSame++;
fullHouseChecker = diceVals[j];
}
}
}
}
if (threeSame == 1) {
for (int i = 0; i < N_DICE; i++) {
for (int j = i + 1; j < N_DICE; j++) {
if (diceVals[i] != fullHouseChecker && diceVals[i] == diceVals[j])
return true;
}
}
}
} else if (category == 12) {
usedCategories[player - 1][category] = 1;
for (int m = 0; m < N_DICE; m++) {
for (int n = 0; n < N_DICE; n++) {
for (int o = 0; o < N_DICE; o++) {
for (int p = 0; p < N_DICE; p++) {
if (diceVals[n] == diceVals[m] + 1 && diceVals[o] == diceVals[n] + 1
&& diceVals[p] == diceVals[o] + 1)
return true;
}
}
}
}
} else if (category == 13) {
usedCategories[player - 1][category] = 1;
for (int m = 0; m < N_DICE; m++) {
for (int n = 0; n < N_DICE; n++) {
for (int o = 0; o < N_DICE; o++) {
for (int p = 0; p < N_DICE; p++) {
for (int q = 0; q < N_DICE; q++) {
if (diceVals[n] == diceVals[m] + 1 && diceVals[o] == diceVals[n] + 1
&& diceVals[p] == diceVals[o] + 1 && diceVals[q] == diceVals[p] + 1)
return true;
}
}
}
}
}
} else if (category == 14) {
usedCategories[player - 1][category] = 1;
int yahtzeeCounter = 0;
for (int i = 0; i < N_DICE - 1; i++) {
if (diceVals[i] == diceVals[i + 1]) {
yahtzeeCounter++;
if (yahtzeeCounter == 4) {
return true;
}
}
}
} else if (category == 15) {
usedCategories[player - 1][category] = 1;
return true;
}
return false;
}
// This method checks the upperScoreUpdate array value to see if the
// player has a valid score for the upper bonus (35 points)
// Also is a live update for the upper total score; a sum of the first six categories
private void updateUpperTotal() {
playersTotalScore[player - 1] += Score;
display.updateScorecard(UPPER_SCORE, player, upperScoreUpdate[player - 1]);
if (upperScoreUpdate[player - 1] >= 63) {
UpperBonus = 35;
display.updateScorecard(UPPER_BONUS, player, UpperBonus);
}
display.updateScorecard(TOTAL, player, (playersTotalScore[player - 1]) + UpperBonus);
}
// Is a live update for the lower total score; a sum of the last seven categories
private void updateLowerTotal() {
playersTotalScore[player - 1] += Score;
display.updateScorecard(LOWER_SCORE, player, lowerScoreUpdate[player - 1]);
display.updateScorecard(TOTAL, player, playersTotalScore[player - 1]);
}
// Uses a player counter to transition to the next player
// after a category is chosen
// Resets player to one after the last player's turn
private void setNextPlayer() {
if (player < nPlayers) {
player++;
} else {
player = 1;
}
}
// Checks to see the greatest value of the total score inside the
// playersTotalScore array
// then prints out the player's name with said score
private void declareWinner() {
topScore = 0;
for (int i = 0; i < nPlayers; i++) {
if (playersTotalScore[i] > topScore)
topScore = playersTotalScore[i];
}
for (int i = 0; i < nPlayers; i++) {
if (playersTotalScore[i] == topScore)
display.printMessage(playerNames[i] + " WINS!");
}
}
/* Private instance variables */
private int nPlayers;
private String[] playerNames;
private YahtzeeDisplay display;
private RandomGenerator rgen = new RandomGenerator();
private int[] diceVals = new int[N_DICE];
private int Score;
private int[][] usedCategories;
private int[] playersTotalScore;
private int category;
private int fullHouseChecker;
private int[] upperScoreUpdate;
private int[] lowerScoreUpdate;
private int UpperBonus = 0;
private int player;
private int topScore;
}