Kaynağa Gözat

ScoreSheet complete with tests other than ones where I need a Dice[]

Lauren Green 6 yıl önce
ebeveyn
işleme
0dc1ff654c

+ 4
- 0
src/main/java/io/zipcoder/casino/Dice.java Dosyayı Görüntüle

@@ -7,4 +7,8 @@ public class Dice {
7 7
     public void Roll(){
8 8
 
9 9
     }
10
+
11
+    public int getValue() {
12
+        return value;
13
+    }
10 14
 }

+ 46
- 11
src/main/java/io/zipcoder/casino/ScoreSheet.java Dosyayı Görüntüle

@@ -45,7 +45,10 @@ public class ScoreSheet {
45 45
 
46 46
     public void setRow(ROW row, Dice[] cup){
47 47
 
48
-        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(cup);
48
+        ArrayList<Integer> numbers = new ArrayList<>();
49
+        for(Dice d : cup) {
50
+            numbers.add(d.getValue());
51
+        }
49 52
         Collections.sort(numbers);
50 53
 
51 54
         switch(row) {
@@ -89,14 +92,14 @@ public class ScoreSheet {
89 92
                 }
90 93
                 break;
91 94
             case SMALLSTRAIGHT:
92
-                if(checkSmallStraight()) {
95
+                if(checkSmallStraight(numbers)) {
93 96
                     scores.put(ROW.SMALLSTRAIGHT, 30);
94 97
                 } else {
95 98
                     scores.put(ROW.SMALLSTRAIGHT, 0);
96 99
                 }
97 100
                 break;
98 101
             case LARGESTRAIGHT:
99
-                if(checkLargeStraight()) {
102
+                if(checkLargeStraight(numbers)) {
100 103
                     scores.put(ROW.LARGESTRAIGHT, 40);
101 104
                 } else {
102 105
                     scores.put(ROW.LARGESTRAIGHT, 0);
@@ -116,7 +119,7 @@ public class ScoreSheet {
116 119
 
117 120
     }
118 121
 
119
-    private boolean checkFullHouse(ArrayList<Integer> numbers) {
122
+    public boolean checkFullHouse(ArrayList<Integer> numbers) {
120 123
 
121 124
         boolean check2 = checkOfaKind(numbers, 2);
122 125
         boolean check3 = checkOfaKind(numbers, 3);
@@ -124,17 +127,49 @@ public class ScoreSheet {
124 127
         return (check2 && check3);
125 128
     }
126 129
 
127
-    private boolean checkSmallStraight() {
130
+    public boolean checkSmallStraight(ArrayList<Integer> numbers) {
128 131
 
129
-        return false;
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
+        System.out.println(numbers);
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
+        System.out.println(numbers);
147
+
148
+        boolean check = false;
149
+        if(numbers.toString().equals("[1, 2, 3, 4]") ||
150
+                numbers.toString().equals("[2, 3, 4, 5]") ||
151
+                numbers.toString().equals("[3, 4, 5, 6]")) {
152
+            check = true;
153
+        } else {
154
+            check = false;
155
+        }
156
+        return check;
130 157
     }
131 158
 
132
-    private boolean checkLargeStraight() {
159
+    public boolean checkLargeStraight(ArrayList<Integer> numbers) {
133 160
 
134
-        return false;
161
+        boolean check = false;
162
+        for(int i = 0; i < numbers.size() - 1; i++) {
163
+            if((numbers.get(i) + 1) == numbers.get(i + 1)) {
164
+                check = true;
165
+            } else {
166
+                check = false;
167
+            }
168
+        }
169
+        return check;
135 170
     }
136 171
 
137
-    private boolean checkOfaKind(ArrayList<Integer> numbers, int numb) {
172
+    public boolean checkOfaKind(ArrayList<Integer> numbers, int numb) {
138 173
 
139 174
         int[] counts = new int[6];
140 175
 
@@ -147,7 +182,7 @@ public class ScoreSheet {
147 182
         return false;
148 183
     }
149 184
 
150
-    private int scoreNumbers(ArrayList<Integer> numbers, int numb){
185
+    public int scoreNumbers(ArrayList<Integer> numbers, int numb){
151 186
 
152 187
         int count = 0;
153 188
         for(Integer i : numbers) {
@@ -158,7 +193,7 @@ public class ScoreSheet {
158 193
         return count * numb;
159 194
     }
160 195
 
161
-    private int scoreTotalDice(ArrayList<Integer> numbers) {
196
+    public int scoreTotalDice(ArrayList<Integer> numbers) {
162 197
 
163 198
         int score = 0;
164 199
         for(Integer i : numbers) {

+ 279
- 0
src/test/java/io/zipcoder/casino/ScoreSheetTest.java Dosyayı Görüntüle

@@ -1,4 +1,283 @@
1 1
 package io.zipcoder.casino;
2 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
+
3 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
+
4 283
 }