Parcourir la source

Poker: Update test to exactly match the canonical data and add version file (#1432)

* Poker: Update test to exactly match the canonical data and add version file

* Change PokerTest.java,Card.java and Hand.java in order to match with canonical data and add version file.

* Correct Card.java in order to match with canonical data.

* Change indentation and correct mistakes

* Add import to PokerTest

* Fix indentation and correct mistakes

* Change variable's name from snake case to camel case

* Fix variable's name
Mpatziakoudi Katerina il y a 6 ans
Parent
révision
c1f46300da

+ 5
- 2
exercises/poker/.meta/src/reference/java/Card.java Voir le fichier

@@ -16,10 +16,13 @@ class Card {
16 16
     }
17 17
 
18 18
     private int parseRank(String card) {
19
-        return "..23456789TJQKA".indexOf(card.charAt(0));
19
+    	if (card.substring(0, 2).equals("10")) {
20
+    		return 10;
21
+    	}
22
+    	return "..23456789TJQKA".indexOf(card.charAt(0));
20 23
     }
21 24
 
22 25
     private int parseSuit(String card) {
23
-        return ".HSDC".indexOf(card.charAt(1));
26
+        return ".HSDC".indexOf(card.charAt(card.length() - 1));
24 27
     }
25 28
 }

+ 51
- 12
exercises/poker/.meta/src/reference/java/Hand.java Voir le fichier

@@ -38,10 +38,9 @@ class Hand {
38 38
         }
39 39
         return frequencyMap;
40 40
     }
41
-
41
+    
42 42
     private int scoreHand(List<Card> cards) {
43
-        List<Card> cardsByRank = cards
44
-                .stream()
43
+        List<Card> cardsByRank = cards.stream()
45 44
                 .sorted(Comparator.comparing(Card::getRank))
46 45
                 .unordered()
47 46
                 .collect(Collectors.toList());
@@ -63,7 +62,10 @@ class Hand {
63 62
                 .stream()
64 63
                 .map(Map.Entry::getValue)
65 64
                 .collect(Collectors.toList());
66
-        List<Integer> suits = cards.stream().map(Card::getSuit).collect(Collectors.toList());
65
+        List<Integer> suits = cards
66
+                .stream()
67
+                .map(Card::getSuit)
68
+                .collect(Collectors.toList());
67 69
 
68 70
         return calculatedScore(frequencyMap, cardsByRank, ranks, rankCounts, suits);
69 71
     }
@@ -74,9 +76,15 @@ class Hand {
74 76
             ranks = Arrays.asList(5, 4, 3, 2, 1);
75 77
         }
76 78
 
77
-        boolean flush = suits.stream().distinct().count() == 1;
78
-        boolean straight = ranks.stream().distinct().count() == 5 && ranks.get(0) - ranks.get(4) == 4;
79
+        boolean flush = suits
80
+                .stream()
81
+                .distinct()
82
+                .count() == 1;
83
+        boolean straight = ranks.stream().distinct().count() == 5 
84
+            && ranks.get(0) - ranks.get(4) == 4;
85
+        
79 86
         Iterator<Integer> iteratorOverFrequencies = frequencyMap.keySet().iterator();
87
+        
80 88
         int highestFrequency = iteratorOverFrequencies.next();
81 89
 
82 90
         if (straight && flush) {
@@ -86,24 +94,55 @@ class Hand {
86 94
             return 700 + cardsByRank.get(0).getRank();
87 95
         }
88 96
         if (rankCounts.equals(Arrays.asList(3, 2))) {
89
-            return 600 + cardsByRank.get(0).getRank();
97
+            int triplet = 0;
98
+            int pair = 0;
99
+            for (Integer key : frequencyMap.keySet()) {
100
+                if (frequencyMap.get(key) == 2) {
101
+                    pair = (int) key;
102
+                }
103
+                if (frequencyMap.get(key) == 3) {
104
+                    triplet = 3 * (int) key;
105
+                }
106
+            }
107
+            return 600 + 3 * triplet + pair;
90 108
         }
91 109
         if (flush) {
92 110
             return 500 + highestFrequency;
93 111
         }
94 112
         if (straight) {
95
-            return 400 + highestFrequency;
113
+            int maxValue = Collections.max(ranks);
114
+            return 400 + maxValue;
96 115
         }
97 116
         if (rankCounts.equals(Arrays.asList(3, 1, 1))) {
98
-            return 300 + cardsByRank.get(0).getRank();
117
+            List<Integer> uniqueCards = new ArrayList<Integer>();
118
+            int triplet = 0;
119
+            for (Integer key : frequencyMap.keySet()) {
120
+                if (frequencyMap.get(key) == 1) {
121
+                    uniqueCards.add((int) key);
122
+                }
123
+                if (frequencyMap.get(key) == 3) {
124
+                    triplet = 3 * (int) key;
125
+                }
126
+            }
127
+            return 300 + triplet + Collections.max(uniqueCards);
99 128
         }
100 129
         if (rankCounts.equals(Arrays.asList(2, 2, 1))) {
101
-            return 200 + Math.max(highestFrequency, iteratorOverFrequencies.next());
130
+            int productsOfFrequencyAndValue = 0;
131
+            for (Integer key : frequencyMap.keySet()) {
132
+                int frequencyKey = (int) key;
133
+                int frequencyValue = frequencyMap.get(key);
134
+                productsOfFrequencyAndValue += frequencyKey * frequencyValue;
135
+            }
136
+            return 200 + productsOfFrequencyAndValue + 2 * Math.max(highestFrequency, iteratorOverFrequencies.next());
102 137
         }
103 138
         if (rankCounts.equals(Arrays.asList(2, 1, 1, 1))) {
104 139
             return 100 + highestFrequency;
105 140
         }
106 141
         ranks.sort(Comparator.naturalOrder());
107
-        return ranks.get(0);
142
+        int result = 0;
143
+        for (int i = 0; i < ranks.size(); i++) {
144
+            result += ranks.get(0) * (i + 1);
145
+        }
146
+        return result + ranks.get(ranks.size() - 1);
108 147
     }
109
-}
148
+}               

+ 1
- 0
exercises/poker/.meta/version.txt Voir le fichier

@@ -0,0 +1 @@
1
+1.1.0

+ 133
- 56
exercises/poker/src/test/java/PokerTest.java Voir le fichier

@@ -2,6 +2,7 @@ import org.junit.Ignore;
2 2
 import org.junit.Test;
3 3
 
4 4
 import java.util.Arrays;
5
+import java.util.Collections;
5 6
 
6 7
 import static org.junit.Assert.assertEquals;
7 8
 
@@ -9,99 +10,174 @@ public class PokerTest {
9 10
     @Test
10 11
     public void oneHand() {
11 12
         String hand = "4S 5S 7H 8D JC";
12
-        assertEquals(Arrays.asList(hand), new Poker(Arrays.asList(hand)).getBestHands());
13
+        assertEquals(Collections.singletonList(hand), new Poker(Collections.singletonList(hand)).getBestHands());
14
+    }
15
+
16
+    @Ignore("Remove to run test")
17
+    @Test
18
+    public void highestCardWins() {
19
+        String highest8 = "4D 5S 6S 8D 3C";
20
+        String highest10 = "2S 4C 7S 9H 10H";
21
+        String highestJ = "3S 4S 5D 6H JH";
22
+        assertEquals(Collections.singletonList(highestJ), new Poker(Arrays.asList(highest8, highest10, highestJ)).getBestHands());
23
+    }
24
+
25
+    @Ignore("Remove to run test")
26
+    @Test
27
+    public void tieHasMultipleWinners() {
28
+        String highest8 = "4D 5S 6S 8D 3C";
29
+        String highest10 = "2S 4C 7S 9H 10H";
30
+        String highestJh = "3S 4S 5D 6H JH";
31
+        String highestJd = "3H 4H 5C 6C JD";
32
+        assertEquals(Arrays.asList(highestJh, highestJd),
33
+                new Poker(Arrays.asList(highest8, highest10, highestJh, highestJd)).getBestHands());
34
+    }
35
+
36
+    @Ignore("Remove to run test")
37
+    @Test
38
+    public void sameHighCards() {
39
+        String nextHighest3 = "3S 5H 6S 8D 7H";
40
+        String nextHighest2 = "2S 5D 6D 8C 7S";
41
+        assertEquals(Collections.singletonList(nextHighest3), new Poker(Arrays.asList(nextHighest3, nextHighest2)).getBestHands());
13 42
     }
14 43
 
15 44
     @Ignore("Remove to run test")
16 45
     @Test
17 46
     public void nothingVsOnePair() {
18
-        String nothing = "4S 5H 6S 8D JH";
47
+        String nothing = "4S 5H 6C 8D KH";
19 48
         String pairOf4 = "2S 4H 6S 4D JH";
20
-        assertEquals(Arrays.asList(pairOf4), new Poker(Arrays.asList(nothing, pairOf4)).getBestHands());
49
+        assertEquals(Collections.singletonList(pairOf4), new Poker(Arrays.asList(nothing, pairOf4)).getBestHands());
21 50
     }
22 51
 
23 52
     @Ignore("Remove to run test")
24 53
     @Test
25 54
     public void twoPairs() {
26 55
         String pairOf2 = "4S 2H 6S 2D JH";
27
-        String pairOf4 = "2S 4H 6S 4D JH";
28
-        assertEquals(Arrays.asList(pairOf4), new Poker(Arrays.asList(pairOf2, pairOf4)).getBestHands());
56
+        String pairOf4 = "2S 4H 6C 4D JD";
57
+        assertEquals(Collections.singletonList(pairOf4), new Poker(Arrays.asList(pairOf2, pairOf4)).getBestHands());
29 58
     }
30 59
 
31 60
     @Ignore("Remove to run test")
32 61
     @Test
33 62
     public void onePairVsDoublePair() {
34 63
         String pairOf8 = "2S 8H 6S 8D JH";
35
-        String doublePair = "4S 5H 4S 8D 5H";
36
-        assertEquals(Arrays.asList(doublePair), new Poker(Arrays.asList(pairOf8, doublePair)).getBestHands());
64
+        String doublePair = "4S 5H 4C 8C 5C";
65
+        assertEquals(Collections.singletonList(doublePair), new Poker(Arrays.asList(pairOf8, doublePair)).getBestHands());
37 66
     }
38 67
 
39 68
     @Ignore("Remove to run test")
40 69
     @Test
41 70
     public void twoDoublePairs() {
42
-        String doublePair2And8 = "2S 8H 2S 8D JH";
43
-        String doublePair4And5 = "4S 5H 4S 8D 5H";
44
-        assertEquals(Arrays.asList(doublePair2And8), new Poker(Arrays.asList(doublePair2And8, doublePair4And5)).getBestHands());
71
+        String doublePair2And8 = "2S 8H 2D 8D 3H";
72
+        String doublePair4And5 = "4S 5H 4C 8S 5D";
73
+        assertEquals(Collections.singletonList(doublePair2And8),
74
+                new Poker(Arrays.asList(doublePair2And8, doublePair4And5)).getBestHands());
75
+    }
76
+
77
+    @Ignore("Remove to run test")
78
+    @Test
79
+    public void sameHighestPair() {
80
+        String doublePair2AndQ = "2S QS 2C QD JH";
81
+        String doublePairJAndQ = "JD QH JS 8D QC";
82
+        assertEquals(Collections.singletonList(doublePairJAndQ),
83
+                new Poker(Arrays.asList(doublePairJAndQ, doublePair2AndQ)).getBestHands());
84
+    }
85
+
86
+    @Ignore("Remove to run test")
87
+    @Test
88
+    public void identicallyRankedPairs() {
89
+        String kicker8 = "JD QH JS 8D QC";
90
+        String kicker2 = "JS QS JC 2D QD";
91
+        assertEquals(Collections.singletonList(kicker8), new Poker(Arrays.asList(kicker8, kicker2)).getBestHands());
45 92
     }
46 93
 
47 94
     @Ignore("Remove to run test")
48 95
     @Test
49 96
     public void doublePairVsThree() {
50
-        String doublePair2And8 = "2S 8H 2S 8D JH";
51
-        String threeOf4 = "4S 5H 4S 8D 4H";
52
-        assertEquals(Arrays.asList(threeOf4), new Poker(Arrays.asList(doublePair2And8, threeOf4)).getBestHands());
97
+        String doublePair2And8 = "2S 8H 2H 8D JH";
98
+        String threeOf4 = "4S 5H 4C 8S 4H";
99
+        assertEquals(Collections.singletonList(threeOf4), new Poker(Arrays.asList(doublePair2And8, threeOf4)).getBestHands());
53 100
     }
54 101
 
55 102
     @Ignore("Remove to run test")
56 103
     @Test
57 104
     public void twoThrees() {
58
-        String threeOf2 = "2S 2H 2S 8D JH";
59
-        String threeOf1 = "4S AH AS 8D AH";
60
-        assertEquals(Arrays.asList(threeOf1), new Poker(Arrays.asList(threeOf2, threeOf1)).getBestHands());
105
+        String threeOf2 = "2S 2H 2C 8D JH";
106
+        String threeOf1 = "4S AH AS 8C AD";
107
+        assertEquals(Collections.singletonList(threeOf1), new Poker(Arrays.asList(threeOf2, threeOf1)).getBestHands());
108
+    }
109
+
110
+    @Ignore("Remove to run test")
111
+    @Test
112
+    public void sameThreesMultipleDecks() {
113
+        String remainingCard7 = "4S AH AS 7C AD";
114
+        String remainingCard8 = "4S AH AS 8C AD";
115
+        assertEquals(Collections.singletonList(remainingCard8),
116
+                new Poker(Arrays.asList(remainingCard7, remainingCard8)).getBestHands());
61 117
     }
62 118
 
63 119
     @Ignore("Remove to run test")
64 120
     @Test
65 121
     public void threeVsStraight() {
66
-        String threeOf4 = "4S 5H 4S 8D 4H";
67
-        String straight = "3S 4H 2S 6D 5H";
68
-        assertEquals(Arrays.asList(straight), new Poker(Arrays.asList(threeOf4, straight)).getBestHands());
122
+        String threeOf4 = "4S 5H 4C 8D 4H";
123
+        String straight = "3S 4D 2S 6D 5C";
124
+        assertEquals(Collections.singletonList(straight), new Poker(Arrays.asList(threeOf4, straight)).getBestHands());
125
+    }
126
+
127
+    @Ignore("Remove to run test")
128
+    @Test
129
+    public void AcesCanEndAStraight() {
130
+        String hand = "4S 5H 4C 8D 4H";
131
+        String straightEndsA = "10D JH QS KD AC";
132
+        assertEquals(Collections.singletonList(straightEndsA), new Poker(Arrays.asList(hand, straightEndsA)).getBestHands());
133
+    }
134
+
135
+    @Ignore("Remove to run test")
136
+    @Test
137
+    public void AcesCanStartAStraight() {
138
+        String hand = "4S 5H 4C 8D 4H";
139
+        String straightStartA = "4D AH 3S 2D 5C";
140
+        assertEquals(Collections.singletonList(straightStartA), new Poker(Arrays.asList(hand, straightStartA)).getBestHands());
69 141
     }
70 142
 
71 143
     @Ignore("Remove to run test")
72 144
     @Test
73 145
     public void twoStraights() {
74
-        String straightTo8 = "4S 6H 7S 8D 5H";
146
+        String straightTo8 = "4S 6C 7S 8D 5H";
75 147
         String straightTo9 = "5S 7H 8S 9D 6H";
76
-        assertEquals(Arrays.asList(straightTo9), new Poker(Arrays.asList(straightTo8, straightTo9)).getBestHands());
148
+        assertEquals(Collections.singletonList(straightTo9), new Poker(Arrays.asList(straightTo8, straightTo9)).getBestHands());
149
+    }
77 150
 
78
-        String straightTo1 = "AS QH KS TD JH";
79
-        String straightTo5 = "4S AH 3S 2D 5H";
80
-        assertEquals(Arrays.asList(straightTo1), new Poker(Arrays.asList(straightTo1, straightTo5)).getBestHands());
151
+    @Ignore("Remove to run test")
152
+    @Test
153
+    public void theLowestStraightStartsWithAce() {
154
+        String straight = "2H 3C 4D 5D 6H";
155
+        String straightStartA = "4S AH 3S 2D 5H";
156
+        assertEquals(Collections.singletonList(straight), new Poker(Arrays.asList(straight, straightStartA)).getBestHands());
81 157
     }
82 158
 
83 159
     @Ignore("Remove to run test")
84 160
     @Test
85 161
     public void straightVsFlush() {
86
-        String straightTo8 = "4S 6H 7S 8D 5H";
162
+        String straightTo8 = "4C 6H 7D 8D 5H";
87 163
         String flushTo7 = "2S 4S 5S 6S 7S";
88
-        assertEquals(Arrays.asList(flushTo7), new Poker(Arrays.asList(straightTo8, flushTo7)).getBestHands());
164
+        assertEquals(Collections.singletonList(flushTo7), new Poker(Arrays.asList(straightTo8, flushTo7)).getBestHands());
89 165
     }
90 166
 
91 167
     @Ignore("Remove to run test")
92 168
     @Test
93 169
     public void twoFlushes() {
94
-        String flushTo8 = "3H 6H 7H 8H 5H";
170
+        String flushTo8 = "4H 7H 8H 9H 6H";
95 171
         String flushTo7 = "2S 4S 5S 6S 7S";
96
-        assertEquals(Arrays.asList(flushTo8), new Poker(Arrays.asList(flushTo8, flushTo7)).getBestHands());
172
+        assertEquals(Collections.singletonList(flushTo8), new Poker(Arrays.asList(flushTo8, flushTo7)).getBestHands());
97 173
     }
98 174
 
99 175
     @Ignore("Remove to run test")
100 176
     @Test
101 177
     public void flushVsFull() {
102 178
         String flushTo8 = "3H 6H 7H 8H 5H";
103
-        String full = "4S 5H 4S 5D 4H";
104
-        assertEquals(Arrays.asList(full), new Poker(Arrays.asList(full, flushTo8)).getBestHands());
179
+        String full = "4S 5H 4C 5D 4H";
180
+        assertEquals(Collections.singletonList(full), new Poker(Arrays.asList(full, flushTo8)).getBestHands());
105 181
     }
106 182
 
107 183
     @Ignore("Remove to run test")
@@ -109,55 +185,56 @@ public class PokerTest {
109 185
     public void twoFulls() {
110 186
         String fullOf4By9 = "4H 4S 4D 9S 9D";
111 187
         String fullOf5By8 = "5H 5S 5D 8S 8D";
112
-        assertEquals(Arrays.asList(fullOf5By8), new Poker(Arrays.asList(fullOf4By9, fullOf5By8)).getBestHands());
188
+        assertEquals(Collections.singletonList(fullOf5By8), new Poker(Arrays.asList(fullOf4By9, fullOf5By8)).getBestHands());
113 189
     }
114 190
 
115 191
     @Ignore("Remove to run test")
116 192
     @Test
117
-    public void fullVsSquare() {
118
-        String full = "4S 5H 4S 5D 4H";
119
-        String squareOf3 = "3S 3H 2S 3D 3H";
120
-        assertEquals(Arrays.asList(squareOf3), new Poker(Arrays.asList(full, squareOf3)).getBestHands());
193
+    public void twoFullssameThripletMultipleDecks() {
194
+        String fullOf5By9 = "5H 5S 5D 9S 9D";
195
+        String fullOf5By8 = "5H 5S 5D 8S 8D";
196
+        assertEquals(Collections.singletonList(fullOf5By9), new Poker(Arrays.asList(fullOf5By9, fullOf5By8)).getBestHands());
121 197
     }
122 198
 
123 199
     @Ignore("Remove to run test")
124 200
     @Test
125
-    public void twoSquares() {
126
-        String squareOf2 = "2S 2H 2S 8D 2H";
127
-        String squareOf5 = "4S 5H 5S 5D 5H";
128
-        assertEquals(Arrays.asList(squareOf5), new Poker(Arrays.asList(squareOf2, squareOf5)).getBestHands());
201
+    public void fullVsSquare() {
202
+        String full = "4S 5H 4D 5D 4H";
203
+        String squareOf3 = "3S 3H 2S 3D 3C";
204
+        assertEquals(Collections.singletonList(squareOf3), new Poker(Arrays.asList(full, squareOf3)).getBestHands());
129 205
     }
130 206
 
131 207
     @Ignore("Remove to run test")
132 208
     @Test
133
-    public void squareVsStraightFlush() {
134
-        String squareOf5 = "4S 5H 5S 5D 5H";
135
-        String straightFlushTo9 = "5S 7S 8S 9S 6S";
136
-        assertEquals(Arrays.asList(straightFlushTo9), new Poker(Arrays.asList(squareOf5, straightFlushTo9)).getBestHands());
209
+    public void twoSquares() {
210
+        String squareOf2 = "2S 2H 2C 8D 2D";
211
+        String squareOf5 = "4S 5H 5S 5D 5C";
212
+        assertEquals(Collections.singletonList(squareOf5), new Poker(Arrays.asList(squareOf2, squareOf5)).getBestHands());
137 213
     }
138 214
 
139 215
     @Ignore("Remove to run test")
140 216
     @Test
141
-    public void twoStraightFlushes() {
142
-        String straightFlushTo8 = "4H 6H 7H 8H 5H";
143
-        String straightFlushTo9 = "5S 7S 8S 9S 6S";
144
-        assertEquals(Arrays.asList(straightFlushTo9), new Poker(Arrays.asList(straightFlushTo8, straightFlushTo9)).getBestHands());
217
+    public void sameSquaresMultipleDecks() {
218
+        String kicker2 = "3S 3H 2S 3D 3C";
219
+        String kicker4 = "3S 3H 4S 3D 3C";
220
+        assertEquals(Collections.singletonList(kicker4), new Poker(Arrays.asList(kicker2, kicker4)).getBestHands());
145 221
     }
146 222
 
147 223
     @Ignore("Remove to run test")
148 224
     @Test
149
-    public void threeHandWithTie() {
150
-        String spadeStraightTo9 = "9S 8S 7S 6S 5S";
151
-        String diamondStraightTo9 = "9D 8D 7D 6D 5D";
152
-        String threeOf4 = "4D 4S 4H QS KS";
153
-        assertEquals(Arrays.asList(spadeStraightTo9, diamondStraightTo9), new Poker(Arrays.asList(spadeStraightTo9, diamondStraightTo9, threeOf4)).getBestHands());
225
+    public void squareVsStraightFlush() {
226
+        String squareOf5 = "4S 5H 5S 5D 5C";
227
+        String straightFlushTo9 = "7S 8S 9S 6S 10S";
228
+        assertEquals(Collections.singletonList(straightFlushTo9),
229
+                new Poker(Arrays.asList(squareOf5, straightFlushTo9)).getBestHands());
154 230
     }
155 231
 
156 232
     @Ignore("Remove to run test")
157 233
     @Test
158
-    public void straightTo5AgainstAPairOfJacks() {
159
-        String straightTo5 = "2S 4D 5C 3S AS";
160
-        String twoJacks = "JD 8D 7D JC 5D";
161
-        assertEquals(Arrays.asList(straightTo5), new Poker(Arrays.asList(straightTo5, twoJacks)).getBestHands());
234
+    public void twoStraightFlushes() {
235
+        String straightFlushTo8 = "4H 6H 7H 8H 5H";
236
+        String straightFlushTo9 = "5S 7S 8S 9S 6S";
237
+        assertEquals(Collections.singletonList(straightFlushTo9),
238
+                new Poker(Arrays.asList(straightFlushTo8, straightFlushTo9)).getBestHands());
162 239
     }
163 240
 }