Bläddra i källkod

Most methods written for ScoreSheet

Lauren Green 6 år sedan
förälder
incheckning
a267ac345e

+ 89
- 17
src/main/java/io/zipcoder/casino/ScoreSheet.java Visa fil

@@ -2,9 +2,7 @@
2 2
 
3 3
 package io.zipcoder.casino;
4 4
 
5
-import java.util.EnumMap;
6
-import java.util.HashMap;
7
-import java.util.Map;
5
+import java.util.*;
8 6
 
9 7
 public class ScoreSheet {
10 8
 
@@ -47,51 +45,125 @@ public class ScoreSheet {
47 45
 
48 46
     public void setRow(ROW row, Dice[] cup){
49 47
 
48
+        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(cup);
49
+        Collections.sort(numbers);
50
+
50 51
         switch(row) {
51 52
             case ACES:
52
-                scores.put(ROW.ACES, calculateScore(ROW.ACES, cup);
53
+                scores.put(ROW.ACES, scoreNumbers(numbers, 1));
53 54
                 break;
54 55
             case TWOS:
55
-                scores.put(ROW.TWOS, calculateScore(ROW.TWOS, cup);
56
+                scores.put(ROW.TWOS, scoreNumbers(numbers, 2));
56 57
                 break;
57 58
             case THREES:
58
-                scores.put(ROW.THREES, calculateScore(ROW.THREES, cup);
59
+                scores.put(ROW.THREES, scoreNumbers(numbers, 3));
59 60
                 break;
60 61
             case FOURS:
61
-                scores.put(ROW.FOURS, calculateScore(ROW.FOURS, cup);
62
+                scores.put(ROW.FOURS, scoreNumbers(numbers, 4));
62 63
                 break;
63 64
             case FIVES:
64
-                scores.put(ROW.FIVES, calculateScore(ROW.FIVES, cup);
65
+                scores.put(ROW.FIVES, scoreNumbers(numbers, 5));
65 66
                 break;
66 67
             case SIXES:
67
-                scores.put(ROW.SIXES, calculateScore(ROW.SIXES, cup);
68
+                scores.put(ROW.SIXES, scoreNumbers(numbers, 6));
68 69
                 break;
69 70
             case THREEOFAKIND:
70
-                scores.put(ROW.THREEOFAKIND, calculateScore(ROW.THREEOFAKIND, cup);
71
+                if(checkOfaKind(numbers, 3)) {
72
+                scores.put(ROW.THREEOFAKIND, scoreTotalDice(numbers));
73
+                } else {
74
+                    scores.put(ROW.THREEOFAKIND, 0); 
75
+                }
71 76
                 break;
72 77
             case FOUROFAKIND:
73
-                scores.put(ROW.FOUROFAKIND, calculateScore(ROW.FOUROFAKIND, cup);
78
+                if(checkOfaKind(numbers, 4)) {
79
+                    scores.put(ROW.FOUROFAKIND, scoreTotalDice(numbers));
80
+                } else {
81
+                    scores.put(ROW.FOUROFAKIND, 0);
82
+                }
74 83
                 break;
75 84
             case FULLHOUSE:
76
-                scores.put(ROW.FULLHOUSE, calculateScore(ROW.FULLHOUSE, cup);
85
+                if(checkOfaKind(numbers,5) || checkFullHouse(numbers)) {
86
+                    scores.put(ROW.FULLHOUSE, 25);
87
+                } else {
88
+                    scores.put(ROW.FULLHOUSE, 0);
89
+                }
77 90
                 break;
78 91
             case SMALLSTRAIGHT:
79
-                scores.put(ROW.SMALLSTRAIGHT, calculateScore(ROW.SMALLSTRAIGHT, cup);
92
+                if(checkSmallStraight()) {
93
+                    scores.put(ROW.SMALLSTRAIGHT, 30);
94
+                } else {
95
+                    scores.put(ROW.SMALLSTRAIGHT, 0);
96
+                }
80 97
                 break;
81 98
             case LARGESTRAIGHT:
82
-                scores.put(ROW.LARGESTRAIGHT, calculateScore(ROW.LARGESTRAIGHT, cup);
99
+                if(checkLargeStraight()) {
100
+                    scores.put(ROW.LARGESTRAIGHT, 40);
101
+                } else {
102
+                    scores.put(ROW.LARGESTRAIGHT, 0);
103
+                }
83 104
                 break;
84 105
             case YAHTZEE:
85
-                scores.put(ROW.YAHTZEE, calculateScore(ROW.YAHTZEE, cup);
106
+                if(checkOfaKind(numbers, 5)) {
107
+                    scores.put(ROW.YAHTZEE, 50);
108
+                } else {
109
+                    scores.put(ROW.YAHTZEE, 0);
110
+                }
86 111
                 break;
87 112
             case CHANCE:
88
-                scores.put(ROW.CHANCE, calculateScore(ROW.CHANCE, cup);
113
+                scores.put(ROW.CHANCE, scoreTotalDice(numbers));
89 114
                 break;
90 115
         }
91 116
 
92 117
     }
93 118
 
94
-    public void calculateScore(ROW row, Dice[] cup){
119
+    private boolean checkFullHouse(ArrayList<Integer> numbers) {
120
+
121
+        boolean check2 = checkOfaKind(numbers, 2);
122
+        boolean check3 = checkOfaKind(numbers, 3);
123
+
124
+        return (check2 && check3);
125
+    }
126
+
127
+    private boolean checkSmallStraight() {
128
+
129
+        return false;
130
+    }
131
+
132
+    private boolean checkLargeStraight() {
133
+
134
+        return false;
135
+    }
136
+
137
+    private boolean checkOfaKind(ArrayList<Integer> numbers, int numb) {
138
+
139
+        int[] counts = new int[6];
95 140
 
141
+        for (int i = 0; i < numbers.size(); i++)
142
+            counts[numbers.get(i) - 1]++;
143
+
144
+        for (int i: counts) {
145
+            if (i == numb) return true;
146
+        }
147
+        return false;
148
+    }
149
+
150
+    private int scoreNumbers(ArrayList<Integer> numbers, int numb){
151
+
152
+        int count = 0;
153
+        for(Integer i : numbers) {
154
+            if(i == numb) {
155
+                count++;
156
+            }
157
+        }
158
+        return count * numb;
159
+    }
160
+
161
+    private int scoreTotalDice(ArrayList<Integer> numbers) {
162
+
163
+        int score = 0;
164
+        for(Integer i : numbers) {
165
+            score += i;
166
+        }
167
+        return score;
96 168
     }
97 169
 }

+ 4
- 0
src/test/java/io/zipcoder/casino/ScoreSheetTest.java Visa fil

@@ -0,0 +1,4 @@
1
+package io.zipcoder.casino;
2
+
3
+public class ScoreSheetTest {
4
+}