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