Procházet zdrojové kódy

Merge branch 'master' of laurengreen/ZCW-OOP-Casino into master

jonathan-hinds před 6 roky
rodič
revize
eaeac3795b

+ 1
- 0
src/main/java/io/zipcoder/casino/Dice.java Zobrazit soubor

@@ -13,4 +13,5 @@ public class Dice {
13 13
     {
14 14
         return value;
15 15
     }
16
+
16 17
 }

+ 14
- 0
src/main/java/io/zipcoder/casino/DicePlayer.java Zobrazit soubor

@@ -10,4 +10,18 @@ public class DicePlayer {
10 10
     public DicePlayer(Player player){
11 11
         this.player = player;
12 12
     }
13
+
14
+    public ScoreSheet getScoreSheet() {
15
+        return scoreSheet;
16
+    }
17
+
18
+    public Dice[] getCup() {
19
+        return cup;
20
+    }
21
+
22
+    public void printCup() {
23
+        for(Dice d : cup) {
24
+            System.out.print(d.getValue() + " ");
25
+        }
26
+    }
13 27
 }

+ 181
- 10
src/main/java/io/zipcoder/casino/ScoreSheet.java Zobrazit soubor

@@ -2,31 +2,202 @@
2 2
 
3 3
 package io.zipcoder.casino;
4 4
 
5
-import java.util.HashMap;
6
-import java.util.Map;
5
+import java.util.*;
7 6
 
8 7
 public class ScoreSheet {
9
-    private Map<String, Integer> scores = new HashMap<>();
10 8
 
11
-    ScoreSheet(){}
9
+    private enum ROW{
10
+        ACES, TWOS, THREES, FOURS, FIVES, SIXES, THREEOFAKIND, FOUROFAKIND, FULLHOUSE, SMALLSTRAIGHT, LARGESTRAIGHT, YAHTZEE, CHANCE;
11
+    }
12
+    private Map<ROW, Integer> scores = new EnumMap<>(ROW.class);
13
+
14
+    ScoreSheet(){
15
+
16
+    }
17
+
18
+    public int getTotalScore() {
19
+
20
+        int topTotalScore = 0;
21
+        int bottomTotalScore = 0;
22
+        int index = 0;
23
+        for(ROW r : ROW.values()) {
24
+            while (index < 6) {
25
+                topTotalScore += scores.get(r);
26
+                index++;
27
+            }
28
+            if (topTotalScore >= 63) {
29
+                topTotalScore += 35;
30
+            }
31
+            while (index >= 6) {
32
+                bottomTotalScore += scores.get(r);
33
+                index++;
34
+            }
35
+        }
36
+        return topTotalScore + bottomTotalScore;
37
+        }
38
+
39
+    public void printScoreCard(){
40
+        for(ROW r : ROW.values()) {
41
+            System.out.println(r + ": " + scores.get(r));
42
+        }
12 43
 
13
-    public int getScore() {
14
-        return 0;
15 44
     }
16 45
 
17
-    public void setScore(int score) {
46
+    public void setRow(ROW row, Dice[] cup){
47
+
48
+        ArrayList<Integer> numbers = new ArrayList<>();
49
+        for(Dice d : cup) {
50
+            numbers.add(d.getValue());
51
+        }
52
+        Collections.sort(numbers);
53
+
54
+        switch(row) {
55
+            case ACES:
56
+                scores.put(ROW.ACES, scoreNumbers(numbers, 1));
57
+                break;
58
+            case TWOS:
59
+                scores.put(ROW.TWOS, scoreNumbers(numbers, 2));
60
+                break;
61
+            case THREES:
62
+                scores.put(ROW.THREES, scoreNumbers(numbers, 3));
63
+                break;
64
+            case FOURS:
65
+                scores.put(ROW.FOURS, scoreNumbers(numbers, 4));
66
+                break;
67
+            case FIVES:
68
+                scores.put(ROW.FIVES, scoreNumbers(numbers, 5));
69
+                break;
70
+            case SIXES:
71
+                scores.put(ROW.SIXES, scoreNumbers(numbers, 6));
72
+                break;
73
+            case THREEOFAKIND:
74
+                if(checkOfaKind(numbers, 3)) {
75
+                scores.put(ROW.THREEOFAKIND, scoreTotalDice(numbers));
76
+                } else {
77
+                    scores.put(ROW.THREEOFAKIND, 0); 
78
+                }
79
+                break;
80
+            case FOUROFAKIND:
81
+                if(checkOfaKind(numbers, 4)) {
82
+                    scores.put(ROW.FOUROFAKIND, scoreTotalDice(numbers));
83
+                } else {
84
+                    scores.put(ROW.FOUROFAKIND, 0);
85
+                }
86
+                break;
87
+            case FULLHOUSE:
88
+                if(checkOfaKind(numbers,5) || checkFullHouse(numbers)) {
89
+                    scores.put(ROW.FULLHOUSE, 25);
90
+                } else {
91
+                    scores.put(ROW.FULLHOUSE, 0);
92
+                }
93
+                break;
94
+            case SMALLSTRAIGHT:
95
+                if(checkSmallStraight(numbers)) {
96
+                    scores.put(ROW.SMALLSTRAIGHT, 30);
97
+                } else {
98
+                    scores.put(ROW.SMALLSTRAIGHT, 0);
99
+                }
100
+                break;
101
+            case LARGESTRAIGHT:
102
+                if(checkLargeStraight(numbers)) {
103
+                    scores.put(ROW.LARGESTRAIGHT, 40);
104
+                } else {
105
+                    scores.put(ROW.LARGESTRAIGHT, 0);
106
+                }
107
+                break;
108
+            case YAHTZEE:
109
+                if(checkOfaKind(numbers, 5)) {
110
+                    scores.put(ROW.YAHTZEE, 50);
111
+                } else {
112
+                    scores.put(ROW.YAHTZEE, 0);
113
+                }
114
+                break;
115
+            case CHANCE:
116
+                scores.put(ROW.CHANCE, scoreTotalDice(numbers));
117
+                break;
118
+        }
18 119
 
19 120
     }
20 121
 
21
-    public void printScore(){
122
+    public boolean checkFullHouse(ArrayList<Integer> numbers) {
123
+
124
+        boolean check2 = checkOfaKind(numbers, 2);
125
+        boolean check3 = checkOfaKind(numbers, 3);
126
+
127
+        return (check2 && check3);
128
+    }
129
+
130
+    public boolean checkSmallStraight(ArrayList<Integer> numbers) {
131
+
132
+        for(int i = 0; i < numbers.size() - 1; i++) {
133
+            if(numbers.get(i) == numbers.get(i+1)) {
134
+                numbers.remove(i);
135
+            }
136
+        }
137
+
138
+        while(numbers.size() > 4) {
139
+            if (numbers.get(0) + 1 != numbers.get(1)) {
140
+                numbers.remove(0);
141
+            } else {
142
+                numbers.remove(4);
143
+            }
144
+        }
145
+        if(numbers.size() < 4) return false;
146
+
147
+        boolean check = false;
148
+        if(numbers.toString().equals("[1, 2, 3, 4]") ||
149
+                numbers.toString().equals("[2, 3, 4, 5]") ||
150
+                numbers.toString().equals("[3, 4, 5, 6]")) {
151
+            check = true;
152
+        } else {
153
+            check = false;
154
+        }
155
+        return check;
156
+    }
157
+
158
+    public boolean checkLargeStraight(ArrayList<Integer> numbers) {
159
+
160
+        boolean check = false;
161
+        for(int i = 0; i < numbers.size() - 1; i++) {
162
+            if((numbers.get(i) + 1) == numbers.get(i + 1)) {
163
+                check = true;
164
+            } else {
165
+                check = false;
166
+            }
167
+        }
168
+        return check;
169
+    }
170
+
171
+    public boolean checkOfaKind(ArrayList<Integer> numbers, int numb) {
172
+
173
+        int[] counts = new int[6];
174
+
175
+        for (int i = 0; i < numbers.size(); i++)
176
+            counts[numbers.get(i) - 1]++;
22 177
 
178
+        for (int i: counts) {
179
+            if (i == numb) return true;
180
+        }
181
+        return false;
23 182
     }
24 183
 
25
-    public void setRow(String setRow){
184
+    public int scoreNumbers(ArrayList<Integer> numbers, int numb){
26 185
 
186
+        int count = 0;
187
+        for(Integer i : numbers) {
188
+            if(i == numb) {
189
+                count++;
190
+            }
191
+        }
192
+        return count * numb;
27 193
     }
28 194
 
29
-    public void calculateScore(String row, int value){
195
+    public int scoreTotalDice(ArrayList<Integer> numbers) {
30 196
 
197
+        int score = 0;
198
+        for(Integer i : numbers) {
199
+            score += i;
200
+        }
201
+        return score;
31 202
     }
32 203
 }

+ 22
- 2
src/main/java/io/zipcoder/casino/Yahtzee.java Zobrazit soubor

@@ -1,11 +1,17 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 public class Yahtzee extends DiceGame implements Game{
4
+
4 5
     private Dice[] cup;
6
+    DicePlayer dicePlayer;
5 7
 
8
+    public Yahtzee(Player player) {
9
+        this.dicePlayer = new DicePlayer(player);
10
+    }
6 11
 
7 12
     public void compareScore(DicePlayer player1, DicePlayer player2){
8 13
 
14
+        player1.getScoreSheet().getTotalScore();
9 15
     }
10 16
 
11 17
     public void reRoll(){
@@ -23,11 +29,25 @@ public class Yahtzee extends DiceGame implements Game{
23 29
 
24 30
     }
25 31
 
26
-    public void StartGame() {
32
+    public void StartGame(DicePlayer dicePlayer) {
33
+        Dice dice1 = new Dice();
34
+        Dice dice2 = new Dice();
35
+        Dice dice3 = new Dice();
36
+        Dice dice4 = new Dice();
37
+        Dice dice5 = new Dice();
38
+
39
+        dicePlayer.getCup()[0] = dice1;
40
+        dicePlayer.getCup()[1] = dice2;
41
+        dicePlayer.getCup()[2] = dice3;
42
+        dicePlayer.getCup()[3] = dice4;
43
+        dicePlayer.getCup()[4] = dice5;
27 44
 
28 45
     }
29 46
 
30 47
     public void StartRound() {
31
-
48
+        for(Dice d : dicePlayer.getCup()) {
49
+            d.roll();
50
+        }
51
+        dicePlayer.printCup();
32 52
     }
33 53
 }

+ 283
- 0
src/test/java/io/zipcoder/casino/ScoreSheetTest.java Zobrazit soubor

@@ -0,0 +1,283 @@
1
+package io.zipcoder.casino;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.lang.reflect.Array;
7
+import java.util.ArrayList;
8
+import java.util.Arrays;
9
+import java.util.Collections;
10
+
11
+public class ScoreSheetTest {
12
+
13
+    @Test
14
+    public void testCheckThreeOfAKindFalse() {
15
+        //Given
16
+        ScoreSheet scoreSheet = new ScoreSheet();
17
+        ArrayList<Integer> test = new ArrayList<>();
18
+        test.add(5);
19
+        test.add(3);
20
+        test.add(2);
21
+        test.add(2);
22
+        test.add(3);
23
+
24
+        //Then
25
+        Assert.assertFalse(scoreSheet.checkOfaKind(test, 3));
26
+
27
+    }
28
+
29
+    @Test
30
+    public void testCheckThreeOfAKindTrue() {
31
+        //Given
32
+        ScoreSheet scoreSheet = new ScoreSheet();
33
+        ArrayList<Integer> test = new ArrayList<>();
34
+        test.add(2);
35
+        test.add(3);
36
+        test.add(2);
37
+        test.add(2);
38
+        test.add(3);
39
+
40
+        //Then
41
+        Assert.assertTrue(scoreSheet.checkOfaKind(test, 3));
42
+
43
+    }
44
+
45
+    @Test
46
+    public void testCheckFourOfAKindFalse() {
47
+        //Given
48
+        ScoreSheet scoreSheet = new ScoreSheet();
49
+        ArrayList<Integer> test = new ArrayList<>();
50
+        test.add(5);
51
+        test.add(3);
52
+        test.add(2);
53
+        test.add(2);
54
+        test.add(3);
55
+
56
+        //Then
57
+        Assert.assertFalse(scoreSheet.checkOfaKind(test, 4));
58
+
59
+    }
60
+
61
+    @Test
62
+    public void testCheckFourOfAKindTrue() {
63
+        //Given
64
+        ScoreSheet scoreSheet = new ScoreSheet();
65
+        ArrayList<Integer> test = new ArrayList<>();
66
+        test.add(2);
67
+        test.add(3);
68
+        test.add(2);
69
+        test.add(2);
70
+        test.add(2);
71
+
72
+        //Then
73
+        Assert.assertTrue(scoreSheet.checkOfaKind(test, 4));
74
+
75
+    }
76
+
77
+    @Test
78
+    public void testCheckYahtzeeFalse() {
79
+        //Given
80
+        ScoreSheet scoreSheet = new ScoreSheet();
81
+        ArrayList<Integer> test = new ArrayList<>();
82
+        test.add(5);
83
+        test.add(3);
84
+        test.add(2);
85
+        test.add(2);
86
+        test.add(3);
87
+
88
+        //Then
89
+        Assert.assertFalse(scoreSheet.checkOfaKind(test, 5));
90
+
91
+    }
92
+
93
+    @Test
94
+    public void testCheckYahtzeeTrue() {
95
+        //Given
96
+        ScoreSheet scoreSheet = new ScoreSheet();
97
+        ArrayList<Integer> test = new ArrayList<>();
98
+        test.add(2);
99
+        test.add(2);
100
+        test.add(2);
101
+        test.add(2);
102
+        test.add(2);
103
+
104
+        //Then
105
+        Assert.assertTrue(scoreSheet.checkOfaKind(test, 5));
106
+
107
+    }
108
+
109
+    @Test
110
+    public void testCheckFullHouseFalse() {
111
+        //Given
112
+        ScoreSheet scoreSheet = new ScoreSheet();
113
+        ArrayList<Integer> test = new ArrayList<>();
114
+        test.add(5);
115
+        test.add(3);
116
+        test.add(2);
117
+        test.add(2);
118
+        test.add(3);
119
+
120
+        //Then
121
+        Assert.assertFalse(scoreSheet.checkFullHouse(test));
122
+
123
+    }
124
+
125
+    @Test
126
+    public void testCheckFullHouseTrue() {
127
+        //Given
128
+        ScoreSheet scoreSheet = new ScoreSheet();
129
+        ArrayList<Integer> test = new ArrayList<>();
130
+        test.add(2);
131
+        test.add(3);
132
+        test.add(2);
133
+        test.add(2);
134
+        test.add(3);
135
+
136
+        //Then
137
+        Assert.assertTrue(scoreSheet.checkFullHouse(test));
138
+
139
+    }
140
+
141
+    @Test
142
+    public void testCheckLargeStraightFalse() {
143
+        //Given
144
+        ScoreSheet scoreSheet = new ScoreSheet();
145
+        ArrayList<Integer> test = new ArrayList<>();
146
+        test.add(5);
147
+        test.add(3);
148
+        test.add(2);
149
+        test.add(2);
150
+        test.add(3);
151
+        Collections.sort(test);
152
+
153
+        //Then
154
+        Assert.assertFalse(scoreSheet.checkLargeStraight(test));
155
+
156
+    }
157
+
158
+    @Test
159
+    public void testCheckLargeStraightTrue() {
160
+        //Given
161
+        ScoreSheet scoreSheet = new ScoreSheet();
162
+        ArrayList<Integer> test = new ArrayList<>();
163
+        test.add(2);
164
+        test.add(3);
165
+        test.add(1);
166
+        test.add(5);
167
+        test.add(4);
168
+        Collections.sort(test);
169
+
170
+        //Then
171
+        Assert.assertTrue(scoreSheet.checkLargeStraight(test));
172
+
173
+    }
174
+
175
+    @Test
176
+    public void testCheckSmallStraightFalse() {
177
+        //Given
178
+        ScoreSheet scoreSheet = new ScoreSheet();
179
+        ArrayList<Integer> test = new ArrayList<>();
180
+        test.add(5);
181
+        test.add(3);
182
+        test.add(2);
183
+        test.add(2);
184
+        test.add(3);
185
+        Collections.sort(test);
186
+
187
+        //Then
188
+        Assert.assertFalse(scoreSheet.checkSmallStraight(test));
189
+
190
+    }
191
+
192
+    @Test
193
+    public void testCheckSmallStraightTrue1() {
194
+        //Given
195
+        ScoreSheet scoreSheet = new ScoreSheet();
196
+        ArrayList<Integer> test = new ArrayList<>();
197
+        test.add(5);
198
+        test.add(3);
199
+        test.add(5);
200
+        test.add(2);
201
+        test.add(4);
202
+        Collections.sort(test);
203
+
204
+        //Then
205
+        Assert.assertTrue(scoreSheet.checkSmallStraight(test));
206
+
207
+    }
208
+
209
+    @Test
210
+    public void testCheckSmallStraightTrue2() {
211
+        //Given
212
+        ScoreSheet scoreSheet = new ScoreSheet();
213
+        ArrayList<Integer> test = new ArrayList<>();
214
+        test.add(1);
215
+        test.add(3);
216
+        test.add(6);
217
+        test.add(2);
218
+        test.add(4);
219
+        Collections.sort(test);
220
+
221
+        //Then
222
+        Assert.assertTrue(scoreSheet.checkSmallStraight(test));
223
+
224
+    }
225
+
226
+    @Test
227
+    public void testCheckSmallStraightTrue3() {
228
+        //Given
229
+        ScoreSheet scoreSheet = new ScoreSheet();
230
+        ArrayList<Integer> test = new ArrayList<>();
231
+        test.add(1);
232
+        test.add(3);
233
+        test.add(5);
234
+        test.add(6);
235
+        test.add(4);
236
+        Collections.sort(test);
237
+
238
+        //Then
239
+        Assert.assertTrue(scoreSheet.checkSmallStraight(test));
240
+
241
+    }
242
+
243
+    @Test
244
+    public void testScoreNumbers() {
245
+        //Given
246
+        ScoreSheet scoreSheet = new ScoreSheet();
247
+        ArrayList<Integer> test = new ArrayList<>();
248
+        test.add(2);
249
+        test.add(3);
250
+        test.add(2);
251
+        test.add(2);
252
+        test.add(3);
253
+        int expected = 6;
254
+
255
+        //When
256
+        int actual = scoreSheet.scoreNumbers(test, 2);
257
+
258
+        //Then
259
+        Assert.assertEquals(expected, actual);
260
+
261
+    }
262
+
263
+    @Test
264
+    public void testScoreTotalDice() {
265
+        //Given
266
+        ScoreSheet scoreSheet = new ScoreSheet();
267
+        ArrayList<Integer> test = new ArrayList<>();
268
+        test.add(2);
269
+        test.add(3);
270
+        test.add(2);
271
+        test.add(2);
272
+        test.add(3);
273
+        int expected = 12;
274
+
275
+        //When
276
+        int actual = scoreSheet.scoreTotalDice(test);
277
+
278
+        //Then
279
+        Assert.assertEquals(expected, actual);
280
+
281
+    }
282
+
283
+}