Ver código fonte

Merge branch 'dev' into DiceBuilder

merge
Chad 6 anos atrás
pai
commit
3c0f4c424f

+ 2
- 1
src/main/java/io/zipcoder/casino/Leviathan/Games/GameUtilities/YahtzeeField.java Ver arquivo

@@ -12,5 +12,6 @@ public enum YahtzeeField {
12 12
     FULLHOUSE,
13 13
     SMSTRAIGHT,
14 14
     LGSTRAIGHT,
15
-    YAHTZEE;
15
+    YAHTZEE,
16
+    CHANCE;
16 17
 }

+ 320
- 39
src/main/java/io/zipcoder/casino/Leviathan/Games/Yahtzee.java Ver arquivo

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