|
@@ -1,29 +1,25 @@
|
1
|
1
|
package io.zipcoder.casino.Leviathan.Games;
|
|
2
|
+
|
2
|
3
|
import io.zipcoder.casino.Leviathan.Games.GameUtilities.*;
|
3
|
4
|
import io.zipcoder.casino.Leviathan.*;
|
|
5
|
+
|
4
|
6
|
import java.util.*;
|
5
|
7
|
|
6
|
8
|
|
7
|
9
|
public class Yahtzee extends DiceGame {
|
8
|
10
|
|
9
|
11
|
Player aPlayer;
|
10
|
|
- Dice diceRoller;
|
11
|
|
- Die[] dice;
|
12
|
|
- Console console;
|
13
|
|
- Map<YahtzeeField, Integer> scoreSheet;
|
|
12
|
+ Dice diceRoller = new Dice(5);
|
|
13
|
+ Die[] dice = diceRoller.rollAll();
|
|
14
|
+ int[] diceValueCounts = new int[6];
|
|
15
|
+ Console aConsole = new Console();
|
|
16
|
+ boolean playAgain;
|
|
17
|
+ boolean scoreSheetFull = false;
|
|
18
|
+ Map<YahtzeeField, Integer> scoreSheet = new LinkedHashMap<YahtzeeField, Integer>();
|
14
|
19
|
|
15
|
20
|
|
16
|
21
|
public Yahtzee(Player aPlayer) {
|
17
|
22
|
this.aPlayer = aPlayer;
|
18
|
|
- diceRoller = new Dice(5);
|
19
|
|
- scoreSheet = new LinkedHashMap<YahtzeeField, Integer>();
|
20
|
|
- dice = diceRoller.rollAll();
|
21
|
|
- }
|
22
|
|
-
|
23
|
|
- public static void main(String[] args) {
|
24
|
|
- Player aPlayer = new Player("eric", 20, 18);
|
25
|
|
- Yahtzee yahtzee = new Yahtzee(aPlayer);
|
26
|
|
- yahtzee.playGame();
|
27
|
23
|
}
|
28
|
24
|
|
29
|
25
|
//==================================================================================
|
|
@@ -32,19 +28,28 @@ public class Yahtzee extends DiceGame {
|
32
|
28
|
|
33
|
29
|
public void playGame() {
|
34
|
30
|
|
35
|
|
- createBlankScoreSheet();
|
36
|
|
- printScoreSheet();
|
|
31
|
+ aConsole.println("Welcome to Yahtzee " + aPlayer.getName());
|
|
32
|
+
|
|
33
|
+ boolean playAgain = true;
|
|
34
|
+ while (playAgain) {
|
37
|
35
|
|
38
|
|
- boolean scoreSheetFull = checkScoreSheetForCompletion();
|
|
36
|
+ createBlankScoreSheet();
|
|
37
|
+ printScoreSheet();
|
39
|
38
|
|
40
|
|
- while (!scoreSheetFull) {
|
41
|
|
- rollDice();
|
|
39
|
+ while (!scoreSheetFull) {
|
|
40
|
+ rollDice();
|
42
|
41
|
|
43
|
|
- rollAgainLoop();
|
|
42
|
+ rollAgainLoop();
|
44
|
43
|
|
45
|
|
- YahtzeeField fieldChoice = chooseYahtzeeField();
|
46
|
|
- int score = scoreDice(fieldChoice);
|
47
|
|
- updateScoreSheet(fieldChoice, score);
|
|
44
|
+ YahtzeeField fieldChoice = chooseYahtzeeField();
|
|
45
|
+ int score = scoreDice(fieldChoice);
|
|
46
|
+ updateScoreSheet(fieldChoice, score);
|
|
47
|
+ printScoreSheet();
|
|
48
|
+ scoreSheetFull = checkScoreSheetForCompletion();
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ int totalScore = getTotalScore();
|
|
52
|
+ printEndOfGameMessage(totalScore);
|
48
|
53
|
}
|
49
|
54
|
|
50
|
55
|
}
|
|
@@ -56,8 +61,8 @@ public class Yahtzee extends DiceGame {
|
56
|
61
|
/*
|
57
|
62
|
Create blank score sheet
|
58
|
63
|
*/
|
59
|
|
- public void createBlankScoreSheet(){
|
60
|
|
- for (YahtzeeField field : YahtzeeField.values()){
|
|
64
|
+ public void createBlankScoreSheet() {
|
|
65
|
+ for (YahtzeeField field : YahtzeeField.values()) {
|
61
|
66
|
scoreSheet.put(field, null);
|
62
|
67
|
}
|
63
|
68
|
}
|
|
@@ -65,25 +70,93 @@ public class Yahtzee extends DiceGame {
|
65
|
70
|
/*
|
66
|
71
|
Check score sheet for completion
|
67
|
72
|
*/
|
68
|
|
- public boolean checkScoreSheetForCompletion(){
|
69
|
|
-
|
70
|
|
- for(Map.Entry<YahtzeeField, Integer> entry : scoreSheet.entrySet()){
|
|
73
|
+ public boolean checkScoreSheetForCompletion() {
|
|
74
|
+ for (Map.Entry<YahtzeeField, Integer> entry : scoreSheet.entrySet()) {
|
71
|
75
|
Integer value = entry.getValue();
|
72
|
|
- if (value == null){
|
|
76
|
+ if (value == null) {
|
73
|
77
|
return false;
|
74
|
78
|
}
|
75
|
79
|
}
|
76
|
|
-
|
77
|
80
|
return true;
|
78
|
81
|
}
|
79
|
82
|
|
80
|
83
|
/*
|
81
|
84
|
Print score sheet
|
82
|
85
|
*/
|
83
|
|
- public void printScoreSheet(){
|
84
|
|
- console.println("Scoresheet: " + scoreSheet.toString());
|
|
86
|
+ public void printScoreSheet() {
|
|
87
|
+ aConsole.println("Scoresheet: " + scoreSheet.toString());
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ /*
|
|
91
|
+ Get total score
|
|
92
|
+ */
|
|
93
|
+ public int getTotalScore() {
|
|
94
|
+
|
|
95
|
+ int sumOfUpperSection = getUpperSectionScore();
|
|
96
|
+
|
|
97
|
+ int bonus = 0;
|
|
98
|
+ if (sumOfUpperSection >= 63){
|
|
99
|
+ bonus = 35;
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ int sumOfLowerSection = getLowerSectionScore();
|
|
103
|
+
|
|
104
|
+ return sumOfUpperSection + bonus + sumOfLowerSection;
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ /*
|
|
108
|
+ Get upper section score
|
|
109
|
+ */
|
|
110
|
+ public int getUpperSectionScore(){
|
|
111
|
+ int upperScore = 0;
|
|
112
|
+ Iterator it = scoreSheet.entrySet().iterator();
|
|
113
|
+ while (it.hasNext()){
|
|
114
|
+ Map.Entry scoreEntry = (Map.Entry)it.next();
|
|
115
|
+ if (scoreEntry.getKey() == YahtzeeField.ACES ||
|
|
116
|
+ scoreEntry.getKey() == YahtzeeField.TWOS ||
|
|
117
|
+ scoreEntry.getKey() == YahtzeeField.THREES ||
|
|
118
|
+ scoreEntry.getKey() == YahtzeeField.FOURS ||
|
|
119
|
+ scoreEntry.getKey() == YahtzeeField.FIVES ||
|
|
120
|
+ scoreEntry.getKey() == YahtzeeField.SIXES){
|
|
121
|
+ upperScore += (int)(scoreEntry.getValue());
|
|
122
|
+ }
|
|
123
|
+ }
|
|
124
|
+ return upperScore;
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ /*
|
|
128
|
+ Get lower section score
|
|
129
|
+ */
|
|
130
|
+ public int getLowerSectionScore(){
|
|
131
|
+ int lowerScore = 0;
|
|
132
|
+ Iterator it = scoreSheet.entrySet().iterator();
|
|
133
|
+ while (it.hasNext()){
|
|
134
|
+ Map.Entry scoreEntry = (Map.Entry)it.next();
|
|
135
|
+ if (scoreEntry.getKey() == YahtzeeField.THREEOFAKIND ||
|
|
136
|
+ scoreEntry.getKey() == YahtzeeField.FOUROFAKIND ||
|
|
137
|
+ scoreEntry.getKey() == YahtzeeField.FULLHOUSE ||
|
|
138
|
+ scoreEntry.getKey() == YahtzeeField.SMSTRAIGHT ||
|
|
139
|
+ scoreEntry.getKey() == YahtzeeField.LGSTRAIGHT ||
|
|
140
|
+ scoreEntry.getKey() == YahtzeeField.YAHTZEE ||
|
|
141
|
+ scoreEntry.getKey() == YahtzeeField.CHANCE){
|
|
142
|
+ lowerScore += (int)(scoreEntry.getValue());
|
|
143
|
+ }
|
|
144
|
+ }
|
|
145
|
+ return lowerScore;
|
85
|
146
|
}
|
86
|
147
|
|
|
148
|
+ /*
|
|
149
|
+ Print end of game stats
|
|
150
|
+ */
|
|
151
|
+ public void printEndOfGameMessage(int totalScore){
|
|
152
|
+ aConsole.println("Game over!");
|
|
153
|
+ aConsole.println("Your total score is: " + totalScore);
|
|
154
|
+
|
|
155
|
+ if(aConsole.yesOrNo("Would you like to play again?").equalsIgnoreCase("no")){
|
|
156
|
+ playAgain= false;
|
|
157
|
+ }
|
|
158
|
+ }
|
|
159
|
+
|
87
|
160
|
//==================================================================================
|
88
|
161
|
// SCORING ROLL METHODS
|
89
|
162
|
//==================================================================================
|
|
@@ -91,20 +164,225 @@ public class Yahtzee extends DiceGame {
|
91
|
164
|
/*
|
92
|
165
|
See fields and formula for score
|
93
|
166
|
*/
|
|
167
|
+ public void printRules(){
|
|
168
|
+ aConsole.println("Roll 5 dice up to 3 times and try to get the highest score");
|
|
169
|
+
|
|
170
|
+ }
|
94
|
171
|
|
|
172
|
+ /*
|
|
173
|
+ Choose yahtzee field to score
|
|
174
|
+ */
|
95
|
175
|
public YahtzeeField chooseYahtzeeField() {
|
96
|
176
|
String userInput = aConsole.getStringInput
|
97
|
177
|
("Which field do you want to score?").toUpperCase();
|
98
|
|
- return YahtzeeField.ACES;
|
|
178
|
+ return YahtzeeField.valueOf(userInput);
|
99
|
179
|
}
|
100
|
180
|
|
|
181
|
+ /*
|
|
182
|
+ Score dice
|
|
183
|
+ */
|
101
|
184
|
public int scoreDice(YahtzeeField yahtzeeField) {
|
102
|
|
- //take in field and dice array and give a score
|
103
|
|
- return -1;
|
|
185
|
+
|
|
186
|
+ updateDiceValues();
|
|
187
|
+
|
|
188
|
+ int score = 0;
|
|
189
|
+ switch (yahtzeeField) {
|
|
190
|
+ case ACES:
|
|
191
|
+ score = scoreCountFields(1);
|
|
192
|
+ break;
|
|
193
|
+ case TWOS:
|
|
194
|
+ score = scoreCountFields(2);
|
|
195
|
+ break;
|
|
196
|
+ case THREES:
|
|
197
|
+ score = scoreCountFields(3);
|
|
198
|
+ break;
|
|
199
|
+ case FOURS:
|
|
200
|
+ score = scoreCountFields(4);
|
|
201
|
+ break;
|
|
202
|
+ case FIVES:
|
|
203
|
+ score = scoreCountFields(5);
|
|
204
|
+ break;
|
|
205
|
+ case SIXES:
|
|
206
|
+ score = scoreCountFields(6);
|
|
207
|
+ break;
|
|
208
|
+ case THREEOFAKIND:
|
|
209
|
+ score = scoreOfAKind(ofAKindCriteria(3));
|
|
210
|
+ break;
|
|
211
|
+ case FOUROFAKIND:
|
|
212
|
+ score = scoreOfAKind(ofAKindCriteria(4));
|
|
213
|
+ break;
|
|
214
|
+ case FULLHOUSE:
|
|
215
|
+ score = scoreFullHouse(fullHouseCriteria());
|
|
216
|
+ break;
|
|
217
|
+ case SMSTRAIGHT:
|
|
218
|
+ score = scoreSmStraight(smStraighCriteria());
|
|
219
|
+ break;
|
|
220
|
+ case LGSTRAIGHT:
|
|
221
|
+ score = scoreLgStraight(lgStraightCriteria());
|
|
222
|
+ break;
|
|
223
|
+ case YAHTZEE:
|
|
224
|
+ score = scoreYahtzee(ofAKindCriteria(5));
|
|
225
|
+ break;
|
|
226
|
+ case CHANCE:
|
|
227
|
+ score = scoreOfAKind(ofAKindCriteria(1));
|
|
228
|
+ break;
|
|
229
|
+ }
|
|
230
|
+ return score;
|
104
|
231
|
}
|
105
|
232
|
|
|
233
|
+ /*
|
|
234
|
+ Get count of each value in dice
|
|
235
|
+ */
|
|
236
|
+ public void updateDiceValues(){
|
|
237
|
+ resetDiceValues();
|
|
238
|
+ for (Die d : dice){
|
|
239
|
+ diceValueCounts[d.getValue()-1]++;
|
|
240
|
+ }
|
|
241
|
+ }
|
|
242
|
+
|
|
243
|
+ /*
|
|
244
|
+ Reset count of each value in dice
|
|
245
|
+ */
|
|
246
|
+ public void resetDiceValues(){
|
|
247
|
+ for (int i=0; i < diceValueCounts.length; i++){
|
|
248
|
+ diceValueCounts[i] = 0;
|
|
249
|
+ }
|
|
250
|
+ }
|
|
251
|
+
|
|
252
|
+ /*
|
|
253
|
+ Check "of a kind" criteria is met
|
|
254
|
+ */
|
|
255
|
+ public boolean ofAKindCriteria(int ofAKind){
|
|
256
|
+ boolean meetsCriteria = false;
|
|
257
|
+ for (int count : diceValueCounts){
|
|
258
|
+ if (count >= ofAKind){
|
|
259
|
+ meetsCriteria = true;
|
|
260
|
+ return meetsCriteria;
|
|
261
|
+ }
|
|
262
|
+ }
|
|
263
|
+ return meetsCriteria;
|
|
264
|
+ }
|
|
265
|
+
|
|
266
|
+ /*
|
|
267
|
+ Score dice "of a kind" fields: 3 of a kind, 4 of a kind, or chance
|
|
268
|
+ */
|
|
269
|
+ public int scoreOfAKind(boolean meetsOfAKindCriteria) {
|
|
270
|
+ int score = 0;
|
|
271
|
+ if (meetsOfAKindCriteria) {
|
|
272
|
+ for (Die d : dice){
|
|
273
|
+ score += d.getValue();
|
|
274
|
+ }
|
|
275
|
+ }
|
|
276
|
+ return score;
|
|
277
|
+ }
|
|
278
|
+
|
|
279
|
+ /*
|
|
280
|
+ Score yahtzee
|
|
281
|
+ */
|
|
282
|
+ public int scoreYahtzee(boolean meetsOfAKindCriteria){
|
|
283
|
+ int score = 0;
|
|
284
|
+ if (meetsOfAKindCriteria){
|
|
285
|
+ score = 50;
|
|
286
|
+ }
|
|
287
|
+ return score;
|
|
288
|
+ }
|
|
289
|
+
|
|
290
|
+ /*
|
|
291
|
+ Check Full House criteria is met
|
|
292
|
+ */
|
|
293
|
+ public boolean fullHouseCriteria(){
|
|
294
|
+ boolean meetsCriteria = false;
|
|
295
|
+ int i = 0;
|
|
296
|
+ for (int count : diceValueCounts){
|
|
297
|
+ if (count >= 2){
|
|
298
|
+ i++;
|
|
299
|
+ }
|
|
300
|
+ }
|
|
301
|
+ if(i==2){
|
|
302
|
+ meetsCriteria = true;
|
|
303
|
+ }
|
|
304
|
+ return meetsCriteria;
|
|
305
|
+ }
|
|
306
|
+
|
|
307
|
+ /*
|
|
308
|
+ Score Full House
|
|
309
|
+ */
|
|
310
|
+ public int scoreFullHouse(boolean meetsFullHouseCriteria){
|
|
311
|
+ int score = 0;
|
|
312
|
+ if (meetsFullHouseCriteria){
|
|
313
|
+ score = 25;
|
|
314
|
+ }
|
|
315
|
+ return score;
|
|
316
|
+ }
|
|
317
|
+
|
|
318
|
+ /*
|
|
319
|
+ Check Small Straight criteria is met
|
|
320
|
+ */
|
|
321
|
+ public boolean smStraighCriteria(){
|
|
322
|
+ boolean meetsCriteria = false;
|
|
323
|
+ int i = 0;
|
|
324
|
+ for (int count : diceValueCounts){
|
|
325
|
+ if (count > 1){
|
|
326
|
+ i++;
|
|
327
|
+ }
|
|
328
|
+ }
|
|
329
|
+ if(i<=1){
|
|
330
|
+ meetsCriteria = true;
|
|
331
|
+ }
|
|
332
|
+ return meetsCriteria;
|
|
333
|
+ }
|
|
334
|
+
|
|
335
|
+ /*
|
|
336
|
+ Score SM Straight
|
|
337
|
+ */
|
|
338
|
+ public int scoreSmStraight(boolean meetsSmStraightCriteria){
|
|
339
|
+ int score = 0;
|
|
340
|
+ if (meetsSmStraightCriteria){
|
|
341
|
+ score = 30;
|
|
342
|
+ }
|
|
343
|
+ return score;
|
|
344
|
+ }
|
|
345
|
+
|
|
346
|
+ /*
|
|
347
|
+ Check Large Straight criteria is met
|
|
348
|
+ */
|
|
349
|
+ public boolean lgStraightCriteria(){
|
|
350
|
+ boolean meetsCriteria = false;
|
|
351
|
+ int i = 0;
|
|
352
|
+ for (int count : diceValueCounts){
|
|
353
|
+ if (count > 1){
|
|
354
|
+ i++;
|
|
355
|
+ }
|
|
356
|
+ }
|
|
357
|
+ if(i<1){
|
|
358
|
+ meetsCriteria = true;
|
|
359
|
+ }
|
|
360
|
+ return meetsCriteria;
|
|
361
|
+ }
|
|
362
|
+
|
|
363
|
+ /*
|
|
364
|
+ Score LG Straight
|
|
365
|
+ */
|
|
366
|
+ public int scoreLgStraight(boolean meetsLgStraightCriteria){
|
|
367
|
+ int score = 0;
|
|
368
|
+ if (meetsLgStraightCriteria){
|
|
369
|
+ score = 40;
|
|
370
|
+ }
|
|
371
|
+ return score;
|
|
372
|
+ }
|
|
373
|
+
|
|
374
|
+ /*
|
|
375
|
+ Score dice "count" fields: aces, twos, threes, fours, fives, or sixes
|
|
376
|
+ */
|
|
377
|
+ public int scoreCountFields(int countField) {
|
|
378
|
+ return diceValueCounts[countField-1] * countField;
|
|
379
|
+ }
|
|
380
|
+
|
|
381
|
+ /*
|
|
382
|
+ Update score sheet with scored value
|
|
383
|
+ */
|
106
|
384
|
public void updateScoreSheet(YahtzeeField yahtzeeField, int score) {
|
107
|
|
- //Take in field and score and update sheet
|
|
385
|
+ scoreSheet.put(yahtzeeField, score);
|
108
|
386
|
}
|
109
|
387
|
|
110
|
388
|
//==================================================================================
|
|
@@ -114,7 +392,7 @@ public class Yahtzee extends DiceGame {
|
114
|
392
|
/*
|
115
|
393
|
Roll dice
|
116
|
394
|
*/
|
117
|
|
- public void rollDice(){
|
|
395
|
+ public void rollDice() {
|
118
|
396
|
dice = diceRoller.rollAll();
|
119
|
397
|
printDice();
|
120
|
398
|
}
|
|
@@ -130,20 +408,20 @@ public class Yahtzee extends DiceGame {
|
130
|
408
|
currentDice.append(", ");
|
131
|
409
|
}
|
132
|
410
|
}
|
133
|
|
- console.println("current roll: " + currentDice.toString());
|
|
411
|
+ aConsole.println("current roll: " + currentDice.toString());
|
134
|
412
|
}
|
135
|
413
|
|
136
|
414
|
/*
|
137
|
415
|
Ask user if they want to roll again
|
138
|
416
|
*/
|
139
|
417
|
public String userInputRollAgain() {
|
140
|
|
- return aConsole.getStringInput ("Roll again?").toUpperCase();
|
|
418
|
+ return aConsole.yesOrNo("Roll again?").toUpperCase();
|
141
|
419
|
}
|
142
|
420
|
|
143
|
421
|
/*
|
144
|
422
|
Convert user's choice to roll again to boolean
|
145
|
423
|
*/
|
146
|
|
- public boolean userInputRollAgainBoolean(String userInputString){
|
|
424
|
+ public boolean userInputRollAgainBoolean(String userInputString) {
|
147
|
425
|
boolean rollAgain;
|
148
|
426
|
if (userInputString.equals("YES")) {
|
149
|
427
|
rollAgain = true;
|
|
@@ -169,7 +447,9 @@ public class Yahtzee extends DiceGame {
|
169
|
447
|
rollSelectedDiceAgain(diceToRollAgain);
|
170
|
448
|
printDice();
|
171
|
449
|
rollCounter++;
|
172
|
|
- if (rollCounter >=3){break;}
|
|
450
|
+ if (rollCounter >= 3) {
|
|
451
|
+ break;
|
|
452
|
+ }
|
173
|
453
|
|
174
|
454
|
rollAgainString = userInputRollAgain();
|
175
|
455
|
rollAgain = userInputRollAgainBoolean(rollAgainString);
|
|
@@ -179,8 +459,8 @@ public class Yahtzee extends DiceGame {
|
179
|
459
|
/*
|
180
|
460
|
Ask user to select which dice to roll again
|
181
|
461
|
*/
|
182
|
|
- public Integer[] userInputChooseDice(){
|
183
|
|
- Integer [] diceToRollAgainArray = null;
|
|
462
|
+ public Integer[] userInputChooseDice() {
|
|
463
|
+ Integer[] diceToRollAgainArray = null;
|
184
|
464
|
ArrayList<Integer> diceToRollAgainList = new ArrayList<Integer>();
|
185
|
465
|
boolean stillSelecting = true;
|
186
|
466
|
while (stillSelecting) {
|
|
@@ -200,10 +480,9 @@ public class Yahtzee extends DiceGame {
|
200
|
480
|
*/
|
201
|
481
|
public void rollSelectedDiceAgain(Integer... diceToRollAgain) {
|
202
|
482
|
for (int i = 0; i < diceToRollAgain.length; i++) {
|
203
|
|
- dice[diceToRollAgain[i]-1].rollADice();
|
|
483
|
+ dice[diceToRollAgain[i] - 1].rollADice();
|
204
|
484
|
}
|
205
|
485
|
}
|
206
|
486
|
|
207
|
487
|
|
208
|
|
-
|
209
|
488
|
}
|