|
@@ -30,7 +30,6 @@ public class Yahtzee extends DiceGame {
|
30
|
30
|
//==================================================================================
|
31
|
31
|
// PLAY GAME METHOD
|
32
|
32
|
//==================================================================================
|
33
|
|
-
|
34
|
33
|
public void playGame() {
|
35
|
34
|
printWelcomeMessage();
|
36
|
35
|
while (playAgain) {
|
|
@@ -55,9 +54,7 @@ public class Yahtzee extends DiceGame {
|
55
|
54
|
// SCORE SHEET METHODS
|
56
|
55
|
//==================================================================================
|
57
|
56
|
|
58
|
|
- /*
|
59
|
|
- Check score sheet for completion
|
60
|
|
- */
|
|
57
|
+ //Check score sheet for completion
|
61
|
58
|
private boolean checkScoreSheetForCompletion() {
|
62
|
59
|
for (Map.Entry<YahtzeeField, Integer> entry : scoreSheet.entrySet()) {
|
63
|
60
|
Integer value = entry.getValue();
|
|
@@ -68,9 +65,7 @@ public class Yahtzee extends DiceGame {
|
68
|
65
|
return true;
|
69
|
66
|
}
|
70
|
67
|
|
71
|
|
- /*
|
72
|
|
- Get total score
|
73
|
|
- */
|
|
68
|
+ //Get total score
|
74
|
69
|
private int getTotalScore() {
|
75
|
70
|
int sumOfUpperSection = getUpperSectionScore();
|
76
|
71
|
int bonus = 0;
|
|
@@ -81,9 +76,7 @@ public class Yahtzee extends DiceGame {
|
81
|
76
|
return sumOfUpperSection + bonus + sumOfLowerSection;
|
82
|
77
|
}
|
83
|
78
|
|
84
|
|
- /*
|
85
|
|
- Get upper section score
|
86
|
|
- */
|
|
79
|
+ //Get upper section score
|
87
|
80
|
private int getUpperSectionScore() {
|
88
|
81
|
int upperScore = 0;
|
89
|
82
|
Iterator it = scoreSheet.entrySet().iterator();
|
|
@@ -98,9 +91,7 @@ public class Yahtzee extends DiceGame {
|
98
|
91
|
return upperScore;
|
99
|
92
|
}
|
100
|
93
|
|
101
|
|
- /*
|
102
|
|
- Get lower section score
|
103
|
|
- */
|
|
94
|
+ //Get lower section score
|
104
|
95
|
private int getLowerSectionScore() {
|
105
|
96
|
int lowerScore = 0;
|
106
|
97
|
Iterator it = scoreSheet.entrySet().iterator();
|
|
@@ -116,23 +107,18 @@ public class Yahtzee extends DiceGame {
|
116
|
107
|
return lowerScore;
|
117
|
108
|
}
|
118
|
109
|
|
119
|
|
-
|
120
|
110
|
//==================================================================================
|
121
|
111
|
// SCORING ROLL METHODS
|
122
|
112
|
//==================================================================================
|
123
|
113
|
|
124
|
|
- /*
|
125
|
|
- Score roll
|
126
|
|
- */
|
|
114
|
+ //Score roll
|
127
|
115
|
public void scoreRoll() {
|
128
|
116
|
YahtzeeField fieldChoice = chooseYahtzeeField();
|
129
|
117
|
int score = scoreDice(fieldChoice);
|
130
|
118
|
updateScoreSheet(fieldChoice, score);
|
131
|
119
|
}
|
132
|
120
|
|
133
|
|
- /*
|
134
|
|
- Choose yahtzee field to score
|
135
|
|
- */
|
|
121
|
+ //Convert user input into yahtzee field to score
|
136
|
122
|
public YahtzeeField chooseYahtzeeField() {
|
137
|
123
|
String userInput = userInputFieldToScore();
|
138
|
124
|
while (!isValidYahtzeeField(userInput)) {
|
|
@@ -142,9 +128,7 @@ public class Yahtzee extends DiceGame {
|
142
|
128
|
return YahtzeeField.valueOf(userInput);
|
143
|
129
|
}
|
144
|
130
|
|
145
|
|
- /*
|
146
|
|
- Check if yahtzee field is valid and free
|
147
|
|
- */
|
|
131
|
+ //Check if yahtzee field is valid and free
|
148
|
132
|
public boolean isValidYahtzeeField(String userInput) {
|
149
|
133
|
for (YahtzeeField aYahtzeeField : YahtzeeField.values()) {
|
150
|
134
|
if (aYahtzeeField.toString().equals(userInput)) {
|
|
@@ -156,9 +140,7 @@ public class Yahtzee extends DiceGame {
|
156
|
140
|
return false;
|
157
|
141
|
}
|
158
|
142
|
|
159
|
|
- /*
|
160
|
|
- Score dice
|
161
|
|
- */
|
|
143
|
+ //Score dice
|
162
|
144
|
private int scoreDice(YahtzeeField yahtzeeField) {
|
163
|
145
|
setDiceValueCounts();
|
164
|
146
|
int score = 0;
|
|
@@ -206,16 +188,12 @@ public class Yahtzee extends DiceGame {
|
206
|
188
|
return score;
|
207
|
189
|
}
|
208
|
190
|
|
209
|
|
- /*
|
210
|
|
- Score dice "count" fields: aces, twos, threes, fours, fives, or sixes
|
211
|
|
- */
|
|
191
|
+ //Score dice "count" fields: aces, twos, threes, fours, fives, or sixes
|
212
|
192
|
public int scoreCountFields(int countField) {
|
213
|
193
|
return diceValueCounts[countField - 1] * countField;
|
214
|
194
|
}
|
215
|
195
|
|
216
|
|
- /*
|
217
|
|
- Check "of a kind" criteria is met: 3/4 of a kind, chance, yahtzee
|
218
|
|
- */
|
|
196
|
+ //Check "of a kind" criteria is met: 3/4 of a kind, chance, yahtzee
|
219
|
197
|
public boolean ofAKindCriteria(int ofAKind) {
|
220
|
198
|
for (int count : diceValueCounts) {
|
221
|
199
|
if (count >= ofAKind) {
|
|
@@ -225,23 +203,26 @@ public class Yahtzee extends DiceGame {
|
225
|
203
|
return false;
|
226
|
204
|
}
|
227
|
205
|
|
228
|
|
- /*
|
229
|
|
- Score dice 3/4 of a kind, or chance
|
230
|
|
- */
|
|
206
|
+ //Score dice 3/4 of a kind, or chance
|
231
|
207
|
public int scoreOfAKind(boolean meetsOfAKindCriteria) {
|
232
|
208
|
int score = 0;
|
233
|
209
|
if (meetsOfAKindCriteria) {
|
234
|
|
- for (Die d : dice) {
|
235
|
|
- score += d.getValue();
|
236
|
|
- }
|
|
210
|
+ score = sumOfDice();
|
237
|
211
|
}
|
238
|
212
|
return score;
|
239
|
213
|
}
|
240
|
214
|
|
241
|
|
- /*
|
242
|
|
- Score yahtzee
|
243
|
|
- */
|
244
|
|
- private int scoreYahtzee(boolean meetsOfAKindCriteria) {
|
|
215
|
+ //Get sum of dice
|
|
216
|
+ public int sumOfDice(){
|
|
217
|
+ int sum = 0;
|
|
218
|
+ for (Die d : dice) {
|
|
219
|
+ sum += d.getValue();
|
|
220
|
+ }
|
|
221
|
+ return sum;
|
|
222
|
+ }
|
|
223
|
+
|
|
224
|
+ //Score yahtzee
|
|
225
|
+ public int scoreYahtzee(boolean meetsOfAKindCriteria) {
|
245
|
226
|
int score = 0;
|
246
|
227
|
if (meetsOfAKindCriteria) {
|
247
|
228
|
score = 50;
|
|
@@ -249,9 +230,7 @@ public class Yahtzee extends DiceGame {
|
249
|
230
|
return score;
|
250
|
231
|
}
|
251
|
232
|
|
252
|
|
- /*
|
253
|
|
- Check Full House criteria is met
|
254
|
|
- */
|
|
233
|
+ //Check Full House criteria is met
|
255
|
234
|
public boolean fullHouseCriteria() {
|
256
|
235
|
int i = 0;
|
257
|
236
|
for (int count : diceValueCounts) {
|
|
@@ -265,9 +244,7 @@ public class Yahtzee extends DiceGame {
|
265
|
244
|
return false;
|
266
|
245
|
}
|
267
|
246
|
|
268
|
|
- /*
|
269
|
|
- Score Full House
|
270
|
|
- */
|
|
247
|
+ //Score Full House
|
271
|
248
|
public int scoreFullHouse(boolean meetsFullHouseCriteria) {
|
272
|
249
|
int score = 0;
|
273
|
250
|
if (meetsFullHouseCriteria) {
|
|
@@ -276,9 +253,7 @@ public class Yahtzee extends DiceGame {
|
276
|
253
|
return score;
|
277
|
254
|
}
|
278
|
255
|
|
279
|
|
- /*
|
280
|
|
- Check Small Straight criteria is met
|
281
|
|
- */
|
|
256
|
+ //Check Small Straight criteria is met
|
282
|
257
|
public boolean smStraightCriteria() {
|
283
|
258
|
boolean meetsCriteria = false;
|
284
|
259
|
int i = 0;
|
|
@@ -293,10 +268,8 @@ public class Yahtzee extends DiceGame {
|
293
|
268
|
return meetsCriteria;
|
294
|
269
|
}
|
295
|
270
|
|
296
|
|
- /*
|
297
|
|
- Score SM Straight
|
298
|
|
- */
|
299
|
|
- private int scoreSmStraight(boolean meetsSmStraightCriteria) {
|
|
271
|
+ //Score SM Straight
|
|
272
|
+ public int scoreSmStraight(boolean meetsSmStraightCriteria) {
|
300
|
273
|
int score = 0;
|
301
|
274
|
if (meetsSmStraightCriteria) {
|
302
|
275
|
score = 30;
|
|
@@ -304,9 +277,7 @@ public class Yahtzee extends DiceGame {
|
304
|
277
|
return score;
|
305
|
278
|
}
|
306
|
279
|
|
307
|
|
- /*
|
308
|
|
- Check Large Straight criteria is met
|
309
|
|
- */
|
|
280
|
+ //Check Large Straight criteria is met
|
310
|
281
|
public boolean lgStraightCriteria() {
|
311
|
282
|
boolean meetsCriteria = false;
|
312
|
283
|
int i = 0;
|
|
@@ -321,9 +292,7 @@ public class Yahtzee extends DiceGame {
|
321
|
292
|
return meetsCriteria;
|
322
|
293
|
}
|
323
|
294
|
|
324
|
|
- /*
|
325
|
|
- Score LG Straight
|
326
|
|
- */
|
|
295
|
+ //Score LG Straight
|
327
|
296
|
public int scoreLgStraight(boolean meetsLgStraightCriteria) {
|
328
|
297
|
int score = 0;
|
329
|
298
|
if (meetsLgStraightCriteria) {
|
|
@@ -336,9 +305,7 @@ public class Yahtzee extends DiceGame {
|
336
|
305
|
// ROLLING DICE METHODS
|
337
|
306
|
//==================================================================================
|
338
|
307
|
|
339
|
|
- /*
|
340
|
|
- Allow the user to roll again up to 2 more times
|
341
|
|
- */
|
|
308
|
+ //Allow the user to roll again up to 2 more times
|
342
|
309
|
public void rollAgainLoop() {
|
343
|
310
|
boolean rollAgain = userInputYesNoToBoolean(userInputRollAgain());
|
344
|
311
|
int rollCounter = 1;
|
|
@@ -353,18 +320,14 @@ public class Yahtzee extends DiceGame {
|
353
|
320
|
}
|
354
|
321
|
}
|
355
|
322
|
|
356
|
|
- /*
|
357
|
|
- Roll again
|
358
|
|
- */
|
|
323
|
+ //Roll again
|
359
|
324
|
public void rollAgain() {
|
360
|
325
|
List<Integer> diceToRollAgainList = userInputChooseDice();
|
361
|
326
|
Integer[] diceToRollAgain = turnRollAgainListToArray(diceToRollAgainList);
|
362
|
327
|
rollSelectedDiceAgain(diceToRollAgain);
|
363
|
328
|
}
|
364
|
329
|
|
365
|
|
- /*
|
366
|
|
- Ask user to select which dice to roll again
|
367
|
|
- */
|
|
330
|
+ //Ask user to select which dice to roll again
|
368
|
331
|
public List<Integer> userInputChooseDice() {
|
369
|
332
|
List<Integer> diceToRollAgainList = new ArrayList<Integer>();
|
370
|
333
|
boolean stillSelecting = true;
|
|
@@ -383,18 +346,14 @@ public class Yahtzee extends DiceGame {
|
383
|
346
|
return diceToRollAgainList;
|
384
|
347
|
}
|
385
|
348
|
|
386
|
|
- /*
|
387
|
|
- Turn user input of dice to roll again into an array
|
388
|
|
- */
|
|
349
|
+ //Turn user input of dice to roll again into an array
|
389
|
350
|
public Integer[] turnRollAgainListToArray(List<Integer> diceToRollAgainList) {
|
390
|
351
|
Integer[] diceToRollAgainArray = null;
|
391
|
352
|
diceToRollAgainArray = diceToRollAgainList.toArray(new Integer[0]);
|
392
|
353
|
return diceToRollAgainArray;
|
393
|
354
|
}
|
394
|
355
|
|
395
|
|
- /*
|
396
|
|
- Roll the selected dice again
|
397
|
|
- */
|
|
356
|
+ //Roll the selected dice again
|
398
|
357
|
public void rollSelectedDiceAgain(Integer... diceToRollAgain) {
|
399
|
358
|
for (int i = 0; i < diceToRollAgain.length; i++) {
|
400
|
359
|
dice[diceToRollAgain[i] - 1].rollADice();
|
|
@@ -402,51 +361,39 @@ public class Yahtzee extends DiceGame {
|
402
|
361
|
}
|
403
|
362
|
|
404
|
363
|
//==================================================================================
|
405
|
|
- // Get User Input
|
|
364
|
+ // Console Methods Input
|
406
|
365
|
//==================================================================================
|
407
|
366
|
|
408
|
|
- /*
|
409
|
|
- Ask user if they want to roll again
|
410
|
|
- */
|
|
367
|
+ //Ask user if they want to roll again
|
411
|
368
|
public String userInputRollAgain() {
|
412
|
369
|
return aConsole.yesOrNo("Roll again?");
|
413
|
370
|
}
|
414
|
371
|
|
415
|
|
- /*
|
416
|
|
- Ask user the position of the dice that they want to roll again
|
417
|
|
- */
|
|
372
|
+ //Ask user the position of the dice that they want to roll again
|
418
|
373
|
public String userInputDiceToRollAgain() {
|
419
|
374
|
return aConsole.getStringInput("Enter position of dice to roll again or enter to perform roll");
|
420
|
375
|
}
|
421
|
376
|
|
422
|
|
- /*
|
423
|
|
- Ask user which field they want to score
|
424
|
|
- */
|
|
377
|
+ //Ask user which field they want to score
|
425
|
378
|
public String userInputFieldToScore() {
|
426
|
379
|
return aConsole.getStringInput("Which field do you want to score?").toUpperCase();
|
427
|
380
|
}
|
428
|
381
|
|
429
|
|
- /*
|
430
|
|
- Ask user if they want to play again
|
431
|
|
- */
|
|
382
|
+ //Ask user if they want to play again
|
432
|
383
|
public String userInputPlayAgain(){
|
433
|
384
|
return aConsole.yesOrNo("Would you like to play again?");
|
434
|
385
|
}
|
435
|
386
|
|
436
|
|
- /*
|
437
|
|
- Convert user input to boolean
|
438
|
|
- */
|
|
387
|
+ //Convert user input to boolean
|
439
|
388
|
public boolean userInputYesNoToBoolean(String userInputString) {
|
440
|
389
|
return (userInputString.toUpperCase().equals("YES"));
|
441
|
390
|
}
|
442
|
391
|
|
443
|
392
|
//==================================================================================
|
444
|
|
- // Print Output
|
|
393
|
+ // Console Methods Output
|
445
|
394
|
//==================================================================================
|
446
|
395
|
|
447
|
|
- /*
|
448
|
|
- Print dice
|
449
|
|
- */
|
|
396
|
+ //Print dice
|
450
|
397
|
public void printDice() {
|
451
|
398
|
DrawYahtzeeDice diceDrawer = new DrawYahtzeeDice();
|
452
|
399
|
setDiceValues();
|
|
@@ -454,46 +401,34 @@ public class Yahtzee extends DiceGame {
|
454
|
401
|
aConsole.println(" 1 2 3 4 5 ");
|
455
|
402
|
}
|
456
|
403
|
|
457
|
|
- /*
|
458
|
|
- Print welcome message
|
459
|
|
- */
|
|
404
|
+ //Print welcome message
|
460
|
405
|
public void printWelcomeMessage() {
|
461
|
406
|
aConsole.println("Welcome to Yahtzee " + aPlayer.getName());
|
462
|
407
|
printRules();
|
463
|
408
|
}
|
464
|
409
|
|
465
|
|
- /*
|
466
|
|
- Print rules
|
467
|
|
- */
|
|
410
|
+ //Print rules
|
468
|
411
|
public void printRules() {
|
469
|
412
|
aConsole.println("Roll 5 dice up to 3 times and try to get the highest score");
|
470
|
413
|
}
|
471
|
414
|
|
472
|
|
- /*
|
473
|
|
- Print score sheet
|
474
|
|
- */
|
|
415
|
+ //Print score sheet
|
475
|
416
|
private void printScoreSheet() {
|
476
|
417
|
aConsole.println("Scoresheet: " + scoreSheet.toString());
|
477
|
418
|
}
|
478
|
419
|
|
479
|
|
- /*
|
480
|
|
- Print end of game stats
|
481
|
|
- */
|
|
420
|
+ //Print end of game stats
|
482
|
421
|
public void printEndOfGameMessage(int totalScore) {
|
483
|
422
|
aConsole.println("Game over!");
|
484
|
423
|
aConsole.println("Your total score is: " + totalScore);
|
485
|
424
|
}
|
486
|
425
|
|
487
|
|
- /*
|
488
|
|
- Print error when yahtzee field input is invalid or already full
|
489
|
|
- */
|
|
426
|
+ //Print error when yahtzee field input is invalid or already full
|
490
|
427
|
public void printYahtzeeFieldErrorMessage() {
|
491
|
428
|
aConsole.println("please enter valid & free yahtzee field.");
|
492
|
429
|
}
|
493
|
430
|
|
494
|
|
- /*
|
495
|
|
- Print error when user enters dice to roll outside of 1-5 bounds
|
496
|
|
- */
|
|
431
|
+ //Print error when user enters dice to roll outside of 1-5 bounds
|
497
|
432
|
public void printDicePositionErrorMessage() {
|
498
|
433
|
aConsole.println("Enter a number between 1 and 5 or enter to roll");
|
499
|
434
|
}
|
|
@@ -502,24 +437,12 @@ public class Yahtzee extends DiceGame {
|
502
|
437
|
// Getters and Setters (and Re-setters)
|
503
|
438
|
//==================================================================================
|
504
|
439
|
|
505
|
|
- /*
|
506
|
|
- Set dice (roll all dice)
|
507
|
|
- */
|
|
440
|
+ //Set dice (roll all dice)
|
508
|
441
|
public void rollDice() {
|
509
|
442
|
dice = diceRoller.rollAll();
|
510
|
443
|
}
|
511
|
444
|
|
512
|
|
- /*
|
513
|
|
- Get dice
|
514
|
|
- */
|
515
|
|
- public Die[] getDice(){
|
516
|
|
- return this.dice;
|
517
|
|
- }
|
518
|
|
-
|
519
|
|
-
|
520
|
|
- /*
|
521
|
|
- Set value count array
|
522
|
|
- */
|
|
445
|
+ //Set value count array
|
523
|
446
|
public void setDiceValueCounts() {
|
524
|
447
|
resetDiceValueCounts();
|
525
|
448
|
for (Die d : dice) {
|
|
@@ -527,57 +450,38 @@ public class Yahtzee extends DiceGame {
|
527
|
450
|
}
|
528
|
451
|
}
|
529
|
452
|
|
530
|
|
- /*
|
531
|
|
- Reset count of each value in dice
|
532
|
|
- */
|
|
453
|
+ //Reset count of each value in dice
|
533
|
454
|
public void resetDiceValueCounts() {
|
534
|
455
|
for (int i = 0; i < diceValueCounts.length; i++) {
|
535
|
456
|
diceValueCounts[i] = 0;
|
536
|
457
|
}
|
537
|
458
|
}
|
538
|
459
|
|
539
|
|
- /*
|
540
|
|
- Get value count array
|
541
|
|
- */
|
|
460
|
+ //Get value count array
|
542
|
461
|
public int[] getDiceValueCounts() {
|
543
|
462
|
return this.diceValueCounts;
|
544
|
463
|
}
|
545
|
464
|
|
546
|
|
- /*
|
547
|
|
- Set dice values array
|
548
|
|
- */
|
|
465
|
+ //Set dice values array
|
549
|
466
|
public void setDiceValues() {
|
550
|
467
|
for (int i = 0; i < dice.length; i++) {
|
551
|
468
|
diceValues[i] = (dice[i].getValue());
|
552
|
469
|
}
|
553
|
470
|
}
|
554
|
471
|
|
555
|
|
- /*
|
556
|
|
- Get dice values array
|
557
|
|
- */
|
558
|
|
- public int[] getDiceValues() {
|
559
|
|
- return this.diceValues;
|
560
|
|
- }
|
561
|
|
-
|
562
|
|
- /*
|
563
|
|
- Set score sheet with scored value
|
564
|
|
- */
|
|
472
|
+ //Set score sheet with scored value
|
565
|
473
|
public void updateScoreSheet(YahtzeeField yahtzeeField, int score) {
|
566
|
474
|
scoreSheet.put(yahtzeeField, score);
|
567
|
475
|
}
|
568
|
476
|
|
569
|
|
- /*
|
570
|
|
- Reset score sheet to blank
|
571
|
|
- */
|
|
477
|
+ //Reset score sheet to blank
|
572
|
478
|
public void createBlankScoreSheet() {
|
573
|
479
|
for (YahtzeeField field : YahtzeeField.values()) {
|
574
|
480
|
scoreSheet.put(field, null);
|
575
|
481
|
}
|
576
|
482
|
}
|
577
|
483
|
|
578
|
|
- /*
|
579
|
|
- Get score sheet
|
580
|
|
- */
|
|
484
|
+ //Get score sheet
|
581
|
485
|
public Map<YahtzeeField, Integer> getScoreSheet() {
|
582
|
486
|
return this.scoreSheet;
|
583
|
487
|
}
|