Parcourir la source

Use instance functions and improve code readability

Zaldrick il y a 9 ans
Parent
révision
6b6e207401

+ 0
- 346
config.json~ Voir le fichier

@@ -1,346 +0,0 @@
1
-{
2
-  "slug": "java",
3
-  "language": "Java",
4
-  "repository": "https://github.com/exercism/xjava",
5
-  "active": true,
6
-  "exercises": [
7
-    {
8
-      "slug": "hello-world",
9
-      "difficulty": 1,
10
-      "topics": []
11
-    },
12
-    {
13
-      "slug": "rna-transcription",
14
-      "difficulty": 2,
15
-      "topics": []
16
-    },
17
-    {
18
-      "slug": "pangram",
19
-      "difficulty": 3,
20
-      "topics": []
21
-    },
22
-    {
23
-      "slug": "hamming",
24
-      "difficulty": 3,
25
-      "topics": []
26
-    },
27
-    {
28
-      "slug": "gigasecond",
29
-      "difficulty": 3,
30
-      "topics": []
31
-    },
32
-    {
33
-      "slug": "space-age",
34
-      "difficulty": 3,
35
-      "topics": []
36
-    },
37
-    {
38
-      "slug": "acronym",
39
-      "difficulty": 3,
40
-      "topics": []
41
-    },
42
-    {
43
-      "slug": "scrabble-score",
44
-      "difficulty": 3,
45
-      "topics": []
46
-    },
47
-    {
48
-      "slug": "raindrops",
49
-      "difficulty": 3,
50
-      "topics": []
51
-    },
52
-    {
53
-      "slug": "difference-of-squares",
54
-      "difficulty": 3,
55
-      "topics": []
56
-    },
57
-    {
58
-      "slug": "secret-handshake",
59
-      "difficulty": 3,
60
-      "topics": []
61
-    },
62
-    {
63
-      "slug": "sum-of-multiples",
64
-      "difficulty": 4,
65
-      "topics": []
66
-    },
67
-    {
68
-      "slug": "luhn",
69
-      "difficulty": 4,
70
-      "topics": []
71
-    },
72
-    {
73
-      "slug": "matrix",
74
-      "difficulty": 4,
75
-      "topics": []
76
-    },
77
-    {
78
-      "slug": "triangle",
79
-      "difficulty": 4,
80
-      "topics": []
81
-    },
82
-    {
83
-      "slug": "largest-series-product",
84
-      "difficulty": 4,
85
-      "topics": []
86
-    },
87
-    {
88
-      "slug": "sieve",
89
-      "difficulty": 4,
90
-      "topics": []
91
-    },
92
-    {
93
-      "slug": "twelve-days",
94
-      "difficulty": 4,
95
-      "topics": []
96
-    },
97
-    {
98
-      "slug": "nth-prime",
99
-      "difficulty": 4,
100
-      "topics": []
101
-    },
102
-    {
103
-      "slug": "diamond",
104
-      "difficulty": 4,
105
-      "topics": []
106
-    },
107
-    {
108
-      "slug": "flatten-array",
109
-      "difficulty": 5,
110
-      "topics": []
111
-    },
112
-    {
113
-      "slug": "pig-latin",
114
-      "difficulty": 5,
115
-      "topics": []
116
-    },
117
-    {
118
-      "slug": "phone-number",
119
-      "difficulty": 5,
120
-      "topics": []
121
-    },
122
-    {
123
-      "slug": "accumulate",
124
-      "difficulty": 5,
125
-      "topics": []
126
-    },
127
-    {
128
-      "slug": "nucleotide-count",
129
-      "difficulty": 5,
130
-      "topics": []
131
-    },
132
-    {
133
-      "slug": "word-count",
134
-      "difficulty": 5,
135
-      "topics": []
136
-    },
137
-    {
138
-      "slug": "robot-name",
139
-      "difficulty": 5,
140
-      "topics": []
141
-    },
142
-    {
143
-      "slug": "prime-factors",
144
-      "difficulty": 5,
145
-      "topics": []
146
-    },
147
-    {
148
-      "slug": "allergies",
149
-      "difficulty": 5,
150
-      "topics": []
151
-    },
152
-    {
153
-      "slug": "bob",
154
-      "difficulty": 5,
155
-      "topics": []
156
-    },
157
-    {
158
-      "slug": "pascals-triangle",
159
-      "difficulty": 5,
160
-      "topics": []
161
-    },
162
-    {
163
-      "slug": "bracket-push",
164
-      "difficulty": 5,
165
-      "topics": []
166
-    },
167
-    {
168
-      "slug": "series",
169
-      "difficulty": 5,
170
-      "topics": []
171
-    },
172
-    {
173
-      "slug": "atbash-cipher",
174
-      "difficulty": 5,
175
-      "topics": []
176
-    },
177
-    {
178
-      "slug": "strain",
179
-      "difficulty": 5,
180
-      "topics": []
181
-    },
182
-    {
183
-      "slug": "roman-numerals",
184
-      "difficulty": 6,
185
-      "topics": []
186
-    },
187
-    {
188
-      "slug": "beer-song",
189
-      "difficulty": 6,
190
-      "topics": []
191
-    },
192
-    {
193
-      "slug": "queen-attack",
194
-      "difficulty": 6,
195
-      "topics": []
196
-    },
197
-    {
198
-      "slug": "etl",
199
-      "difficulty": 6,
200
-      "topics": []
201
-    },
202
-    {
203
-      "slug": "linked-list",
204
-      "difficulty": 6,
205
-      "topics": []
206
-    },
207
-    {
208
-      "slug": "grade-school",
209
-      "difficulty": 6,
210
-      "topics": []
211
-    },
212
-    {
213
-      "slug": "robot-simulator",
214
-      "difficulty": 6,
215
-      "topics": []
216
-    },
217
-    {
218
-      "slug": "binary-search",
219
-      "difficulty": 6,
220
-      "topics": []
221
-    },
222
-    {
223
-      "slug": "minesweeper",
224
-      "difficulty": 6,
225
-      "topics": []
226
-    },
227
-    {
228
-      "slug": "wordy",
229
-      "difficulty": 6,
230
-      "topics": []
231
-    },
232
-    {
233
-      "slug": "all-your-base",
234
-      "difficulty": 6,
235
-      "topics": []
236
-    },
237
-    {
238
-      "slug": "anagram",
239
-      "difficulty": 7,
240
-      "topics": []
241
-    },
242
-    {
243
-      "slug": "sublist",
244
-      "difficulty": 7,
245
-      "topics": []
246
-    },
247
-    {
248
-      "slug": "simple-linked-list",
249
-      "difficulty": 7,
250
-      "topics": []
251
-    },
252
-    {
253
-      "slug": "binary-search-tree",
254
-      "difficulty": 7,
255
-      "topics": []
256
-    },
257
-    {
258
-      "slug": "rectangles",
259
-      "difficulty": 7,
260
-      "topics": []
261
-    },
262
-    {
263
-      "slug": "meetup",
264
-      "difficulty": 7,
265
-      "topics": []
266
-    },
267
-    {
268
-      "slug": "crypto-square",
269
-      "difficulty": 7,
270
-      "topics": []
271
-    },
272
-    {
273
-      "slug": "poker",
274
-      "difficulty": 7,
275
-      "topics": [
276
-        "Parsing",
277
-        "Sorting",
278
-        "Games"
279
-      ]
280
-    },
281
-    {
282
-      "slug": "simple-cipher",
283
-      "difficulty": 8,
284
-      "topics": []
285
-    },
286
-    {
287
-      "slug": "palindrome-products",
288
-      "difficulty": 8,
289
-      "topics": []
290
-    },
291
-    {
292
-      "slug": "pythagorean-triplet",
293
-      "difficulty": 9,
294
-      "topics": []
295
-    },
296
-    {
297
-      "slug": "custom-set",
298
-      "difficulty": 10,
299
-      "topics": []
300
-    },
301
-    {
302
-      "slug": "clock",
303
-      "difficulty": 1,
304
-      "topics": []
305
-    },
306
-    {
307
-      "slug": "perfect-numbers",
308
-      "difficulty": 1,
309
-      "topics": []
310
-    },
311
-    {
312
-      "slug": "change",
313
-      "difficulty": 1,
314
-      "topics": []
315
-    },
316
-    {
317
-      "slug": "list-ops",
318
-      "difficulty": 1,
319
-      "topics": []
320
-    },
321
-    {
322
-      "slug": "ocr-numbers",
323
-      "difficulty": 1,
324
-      "topics": []
325
-    }
326
-  ],
327
-  "deprecated": [
328
-    "binary",
329
-    "hexadecimal",
330
-    "octal",
331
-    "trinary"
332
-  ],
333
-  "ignored": [
334
-    "_template",
335
-    ".gradle",
336
-    "build",
337
-    "docs",
338
-    "gradle",
339
-    "scripts"
340
-  ],
341
-  "foregone": [
342
-    "leap",
343
-    "grains",
344
-    "say"
345
-  ]
346
-}

+ 24
- 24
exercises/poker/src/example/java/Card.java Voir le fichier

@@ -1,25 +1,25 @@
1
-public class Card {
2
-	private int rank;
3
-	private int suit;
4
-
5
-	public Card(String card) {
6
-		rank = parseRank(card);
7
-		suit = parseSuit(card);
8
-	}
9
-
10
-	public int getRank() {
11
-		return rank;
12
-	}
13
-
14
-	public int getSuit() {
15
-		return suit;
16
-	}
17
-
18
-	private static int parseRank(String card) {
19
-		return "..23456789TJQKA".indexOf(card.charAt(0));
20
-	}
21
-
22
-	private static int parseSuit(String card) {
23
-		return ".HSDC".indexOf(card.charAt(1));
24
-	}
1
+public class Card {
2
+	private int rank;
3
+	private int suit;
4
+
5
+	public Card(String card) {
6
+		rank = parseRank(card);
7
+		suit = parseSuit(card);
8
+	}
9
+
10
+	public int getRank() {
11
+		return rank;
12
+	}
13
+
14
+	public int getSuit() {
15
+		return suit;
16
+	}
17
+
18
+	private int parseRank(String card) {
19
+		return "..23456789TJQKA".indexOf(card.charAt(0));
20
+	}
21
+
22
+	private int parseSuit(String card) {
23
+		return ".HSDC".indexOf(card.charAt(1));
24
+	}
25 25
 }

+ 103
- 105
exercises/poker/src/example/java/Hand.java Voir le fichier

@@ -1,106 +1,104 @@
1
-import java.util.ArrayList;
2
-import java.util.Arrays;
3
-import java.util.Collections;
4
-import java.util.Comparator;
5
-import java.util.HashMap;
6
-import java.util.Iterator;
7
-import java.util.LinkedHashMap;
8
-import java.util.List;
9
-import java.util.Map;
10
-import java.util.stream.Collectors;
11
-
12
-public class Hand {
13
-	private String input;
14
-	private int score;
15
-
16
-	public Hand(String hand) {
17
-		this.input = hand;
18
-		this.score = scoreHand(parseCards(hand));
19
-	}
20
-
21
-	public int getScore() {
22
-		return score;
23
-	}
24
-
25
-	public String getInput() {
26
-		return input;
27
-	}
28
-
29
-	private static List<Card> parseCards(String hand) {
30
-		ArrayList<Card> parsedCards = new ArrayList<Card>();
31
-		String[] parseString = hand.split(" ");
32
-		for (int i = 0; i < parseString.length; i++) {
33
-			parsedCards.add(new Card(parseString[i]));
34
-		}
35
-		return parsedCards;
36
-	}
37
-
38
-	private static Map<Integer, Integer> getFrequencyMap(List<Card> cards) {
39
-		Map<Integer, Integer> frequencymap = new HashMap<Integer, Integer>() ;
40
-		for (Card c : cards) {
41
-			if (frequencymap.containsKey(c.getRank())) {
42
-				frequencymap.put(c.getRank(), frequencymap.get(c.getRank()) + 1);
43
-			} else {
44
-				frequencymap.put(c.getRank(), 1);
45
-			}
46
-		}
47
-		return frequencymap;
48
-	}
49
-
50
-	private static int scoreHand(List<Card> cards) {
51
-		List<Card> cardsByRank = new ArrayList<Card>();
52
-
53
-		cardsByRank = cards.stream().sorted(Comparator.comparing(Card::getRank)).unordered()
54
-				.collect(Collectors.toList());
55
-
56
-		Map<Integer, Integer> frequencymap = getFrequencyMap(cards);
57
-		List<Integer> ranks = frequencymap.entrySet().stream().map(x -> x.getKey())
58
-				.sorted(Comparator.reverseOrder()).collect(Collectors.toList());
59
-
60
-		frequencymap = frequencymap.entrySet().stream()
61
-				.sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(Collectors
62
-						.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
63
-
64
-		List<Integer> rankCounts = frequencymap.entrySet().stream().map(x -> x.getValue())
65
-				.collect(Collectors.toList());
66
-
67
-		List<Integer> suits = cards.stream().map(s -> s.getSuit()).collect(Collectors.toList());
68
-
69
-		if (ranks.equals(Arrays.asList(14, 5, 4, 3, 2))) {
70
-			ranks = Arrays.asList(5, 4, 3, 2, 1);
71
-		}
72
-
73
-		boolean flush = suits.stream().distinct().count() == 1;
74
-		boolean straight = ranks.stream().distinct().count() == 5 && ranks.get(0) - ranks.get(4) == 4;
75
-		Iterator<Integer> itr = frequencymap.keySet().iterator();
76
-		int pairA = itr.next();
77
-
78
-		if (straight && flush) {
79
-			return 800 + frequencymap.keySet().iterator().next();
80
-		}
81
-		if (rankCounts.equals(Arrays.asList(4, 1))) {
82
-			return (700 + (cardsByRank.get(0)).getRank());
83
-		}
84
-		if (rankCounts.equals(Arrays.asList(3, 2))) {
85
-			return 600 + cardsByRank.get(0).getRank();
86
-		}
87
-		if (flush) {
88
-			return 500 + frequencymap.keySet().iterator().next();
89
-		}
90
-		if (straight) {
91
-			return 400 + frequencymap.keySet().iterator().next();
92
-		}
93
-		if (rankCounts.equals(Arrays.asList(3, 1, 1))) {
94
-			return 300 + cardsByRank.get(0).getRank();
95
-		}
96
-		if (rankCounts.equals(Arrays.asList(2, 2, 1))) {
97
-			return 200 + Math.max(pairA, itr.next());
98
-		}
99
-		if (rankCounts.equals(Arrays.asList(2, 1, 1, 1))) {
100
-			return 100 + pairA;
101
-		}
102
-		Collections.sort(ranks, (a, b) -> b.compareTo(a));
103
-		Collections.reverse(ranks);
104
-		return ranks.get(0);
105
-	}
1
+import java.util.ArrayList;
2
+import java.util.Arrays;
3
+import java.util.Collections;
4
+import java.util.Comparator;
5
+import java.util.HashMap;
6
+import java.util.Iterator;
7
+import java.util.LinkedHashMap;
8
+import java.util.List;
9
+import java.util.Map;
10
+import java.util.stream.Collectors;
11
+
12
+public class Hand {
13
+	private String input;
14
+	private int score;
15
+
16
+	public Hand(String hand) {
17
+		this.input = hand;
18
+		this.score = scoreHand(parseCards(hand));
19
+	}
20
+
21
+	public int getScore() {
22
+		return score;
23
+	}
24
+
25
+	public String getInput() {
26
+		return input;
27
+	}
28
+
29
+	private List<Card> parseCards(String hand) {
30
+		ArrayList<Card> parsedCards = new ArrayList<Card>();
31
+		String[] parseString = hand.split(" ");
32
+		for (int i = 0; i < parseString.length; i++) {
33
+			parsedCards.add(new Card(parseString[i]));
34
+		}
35
+		return parsedCards;
36
+	}
37
+
38
+	private Map<Integer, Integer> getfrequencyMap(List<Card> cards) {
39
+		Map<Integer, Integer> frequencyMap = new HashMap<Integer, Integer>();
40
+		for (Card c : cards) {
41
+			if (frequencyMap.containsKey(c.getRank())) {
42
+				frequencyMap.put(c.getRank(), frequencyMap.get(c.getRank()) + 1);
43
+			} else {
44
+				frequencyMap.put(c.getRank(), 1);
45
+			}
46
+		}
47
+		return frequencyMap;
48
+	}
49
+
50
+	private int scoreHand(List<Card> cards) {
51
+		List<Card> cardsByRank = cards.stream().sorted(Comparator.comparing(Card::getRank)).unordered()
52
+				.collect(Collectors.toList());
53
+
54
+		Map<Integer, Integer> frequencyMap = getfrequencyMap(cards);
55
+		List<Integer> ranks = frequencyMap.entrySet().stream().map(x -> x.getKey()).sorted(Comparator.reverseOrder())
56
+				.collect(Collectors.toList());
57
+		frequencyMap = frequencyMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
58
+				.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
59
+		List<Integer> rankCounts = frequencyMap.entrySet().stream().map(x -> x.getValue()).collect(Collectors.toList());
60
+		List<Integer> suits = cards.stream().map(s -> s.getSuit()).collect(Collectors.toList());
61
+
62
+		return calculatedScore(frequencyMap, cardsByRank, ranks, rankCounts, suits);
63
+	}
64
+
65
+	private int calculatedScore(Map<Integer, Integer> frequencyMap, List<Card> cardsByRank, List<Integer> ranks,
66
+			List<Integer> rankCounts, List<Integer> suits) {
67
+		if (ranks.equals(Arrays.asList(14, 5, 4, 3, 2))) {
68
+			ranks = Arrays.asList(5, 4, 3, 2, 1);
69
+		}
70
+
71
+		boolean flush = suits.stream().distinct().count() == 1;
72
+		boolean straight = ranks.stream().distinct().count() == 5 && ranks.get(0) - ranks.get(4) == 4;
73
+		Iterator<Integer> iteratorOverFrequencies = frequencyMap.keySet().iterator();
74
+		int highestFrequency = iteratorOverFrequencies.next();
75
+
76
+		if (straight && flush) {
77
+			return 800 + highestFrequency;
78
+		}
79
+		if (rankCounts.equals(Arrays.asList(4, 1))) {
80
+			return 700 + cardsByRank.get(0).getRank();
81
+		}
82
+		if (rankCounts.equals(Arrays.asList(3, 2))) {
83
+			return 600 + cardsByRank.get(0).getRank();
84
+		}
85
+		if (flush) {
86
+			return 500 + highestFrequency;
87
+		}
88
+		if (straight) {
89
+			return 400 + highestFrequency;
90
+		}
91
+		if (rankCounts.equals(Arrays.asList(3, 1, 1))) {
92
+			return 300 + cardsByRank.get(0).getRank();
93
+		}
94
+		if (rankCounts.equals(Arrays.asList(2, 2, 1))) {
95
+			return 200 + Math.max(highestFrequency, iteratorOverFrequencies.next());
96
+		}
97
+		if (rankCounts.equals(Arrays.asList(2, 1, 1, 1))) {
98
+			return 100 + highestFrequency;
99
+		}
100
+		Collections.sort(ranks, (a, b) -> b.compareTo(a));
101
+		Collections.reverse(ranks);
102
+		return ranks.get(0);
103
+	}
106 104
 }

+ 18
- 8
exercises/poker/src/example/java/Poker.java Voir le fichier

@@ -4,14 +4,24 @@ import java.util.List;
4 4
 import java.util.stream.Collectors;
5 5
 
6 6
 public class Poker {
7
-			public static List<String> bestHands(List<String> hands) {
8
-				ArrayList<Hand> scoredHands = new ArrayList<Hand>();
7
+	private final List<String> bestHands;
9 8
 
10
-				for (String s : hands)
11
-					scoredHands.add(new Hand(s));
9
+	public Poker(final List<String> hands) {
10
+		bestHands = bestHands(hands);
11
+	}
12 12
 
13
-				int maxscore = scoredHands.stream().map(h -> h.getScore()).max(Comparator.naturalOrder()).get();
14
-				return scoredHands.stream().filter(h -> h.getScore() == maxscore).map(h -> h.getInput())
15
-						.collect(Collectors.toList());
16
-			}
13
+	public List<String> getBestHands() {
14
+		return bestHands;
15
+	}
16
+
17
+	public List<String> bestHands(final List<String> hands) {
18
+		ArrayList<Hand> scoredHands = new ArrayList<Hand>();
19
+
20
+		for (String s : hands) {
21
+			scoredHands.add(new Hand(s));
22
+		}
23
+		int maxscore = scoredHands.stream().map(h -> h.getScore()).max(Comparator.naturalOrder()).get();
24
+		return scoredHands.stream().filter(h -> h.getScore() == maxscore).map(h -> h.getInput())
25
+				.collect(Collectors.toList());
26
+	}
17 27
 }

+ 7
- 3
exercises/poker/src/main/java/Poker.java Voir le fichier

@@ -1,8 +1,12 @@
1 1
 import java.util.List;
2
-import sun.reflect.generics.reflectiveObjects.NotImplementedException;
3 2
 
4 3
 public class Poker {
5
-	public static List<String> bestHands(List<String> hands) {
6
-			throw new NotImplementedException();
4
+
5
+	Poker(List<String> hands) {
6
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
7
+    }
8
+
9
+	List<String> bestHands() {
10
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
7 11
 	}
8 12
 }

+ 25
- 23
exercises/poker/src/test/java/PokerTest.java Voir le fichier

@@ -1,14 +1,16 @@
1 1
 import static org.junit.Assert.assertEquals;
2
+
2 3
 import java.util.Arrays;
3
-import org.junit.Ignore;
4
+
4 5
 import org.junit.Test;
5 6
 
6
-public class PokerTest
7
-{
7
+import jdk.nashorn.internal.ir.annotations.Ignore;
8
+
9
+public class PokerTest {
8 10
     @Test
9 11
     public void oneHand() {
10 12
         final String hand = "4S 5S 7H 8D JC";
11
-        assertEquals(Arrays.asList(hand),Poker.bestHands(Arrays.asList(hand)));
13
+        assertEquals(Arrays.asList(hand),new Poker(Arrays.asList(hand)).getBestHands());
12 14
     }
13 15
 
14 16
     @Test
@@ -16,7 +18,7 @@ public class PokerTest
16 18
     public void nothingVsOnePair() {
17 19
         final String nothing = "4S 5H 6S 8D JH";
18 20
         final String pairOf4 = "2S 4H 6S 4D JH";
19
-        assertEquals(Arrays.asList(pairOf4), Poker.bestHands(Arrays.asList(nothing, pairOf4)));
21
+        assertEquals(Arrays.asList(pairOf4),new Poker(Arrays.asList(nothing, pairOf4)).getBestHands());
20 22
     }
21 23
 
22 24
     @Test
@@ -24,7 +26,7 @@ public class PokerTest
24 26
     public void twoPairs() {
25 27
         final String pairOf2 = "4S 2H 6S 2D JH";
26 28
         final String pairOf4 = "2S 4H 6S 4D JH";
27
-        assertEquals(Arrays.asList(pairOf4), Poker.bestHands(Arrays.asList(pairOf2,pairOf4)));
29
+        assertEquals(Arrays.asList(pairOf4), new Poker(Arrays.asList(pairOf2,pairOf4)).getBestHands());
28 30
     }
29 31
 
30 32
     @Test
@@ -32,7 +34,7 @@ public class PokerTest
32 34
     public void onePairVsDoublePair() {
33 35
         final String pairOf8 = "2S 8H 6S 8D JH";
34 36
         final String doublePair = "4S 5H 4S 8D 5H";
35
-        assertEquals(Arrays.asList(doublePair), Poker.bestHands(Arrays.asList(pairOf8, doublePair)));
37
+        assertEquals(Arrays.asList(doublePair),  new Poker(Arrays.asList(pairOf8, doublePair)).getBestHands());
36 38
     }
37 39
 
38 40
     @Test
@@ -40,7 +42,7 @@ public class PokerTest
40 42
     public void twoDoublePairs() {
41 43
         final String doublePair2And8 = "2S 8H 2S 8D JH";
42 44
         final String doublePair4And5 = "4S 5H 4S 8D 5H";
43
-        assertEquals(Arrays.asList(doublePair2And8), Poker.bestHands(Arrays.asList(doublePair2And8, doublePair4And5)));
45
+        assertEquals(Arrays.asList(doublePair2And8), new Poker(Arrays.asList(doublePair2And8, doublePair4And5)).getBestHands());
44 46
     }
45 47
 
46 48
     @Test
@@ -48,7 +50,7 @@ public class PokerTest
48 50
     public void doublePairVsThree() {
49 51
         final String doublePair2And8 = "2S 8H 2S 8D JH";
50 52
         final String threeOf4 = "4S 5H 4S 8D 4H";
51
-        assertEquals(Arrays.asList(threeOf4 ), Poker.bestHands(Arrays.asList(doublePair2And8, threeOf4 )));
53
+        assertEquals(Arrays.asList(threeOf4 ), new Poker(Arrays.asList(doublePair2And8, threeOf4)).getBestHands());
52 54
     }
53 55
 
54 56
     @Test
@@ -56,7 +58,7 @@ public class PokerTest
56 58
     public void twoThrees() {
57 59
         final String threeOf2 = "2S 2H 2S 8D JH";
58 60
         final String threeOf1 = "4S AH AS 8D AH";
59
-        assertEquals(Arrays.asList(threeOf1), Poker.bestHands(Arrays.asList(threeOf2, threeOf1)));
61
+        assertEquals(Arrays.asList(threeOf1), new Poker(Arrays.asList(threeOf2, threeOf1)).getBestHands());
60 62
     }
61 63
 
62 64
     @Test
@@ -64,7 +66,7 @@ public class PokerTest
64 66
     public void threeVsStraight() {
65 67
         final String threeOf4 = "4S 5H 4S 8D 4H";
66 68
         final String straight = "3S 4H 2S 6D 5H";
67
-        assertEquals(Arrays.asList(straight), Poker.bestHands(Arrays.asList(threeOf4, straight )));
69
+        assertEquals(Arrays.asList(straight), new Poker(Arrays.asList(threeOf4, straight)).getBestHands());
68 70
     }
69 71
 
70 72
     @Test
@@ -72,11 +74,11 @@ public class PokerTest
72 74
     public void twoStraights() {
73 75
         final String straightTo8 = "4S 6H 7S 8D 5H";
74 76
         final String straightTo9 = "5S 7H 8S 9D 6H";
75
-        assertEquals(Arrays.asList(straightTo9 ), Poker.bestHands(Arrays.asList(straightTo8, straightTo9 )));
77
+        assertEquals(Arrays.asList(straightTo9 ), new Poker(Arrays.asList(straightTo8, straightTo9)).getBestHands());
76 78
 
77 79
         final String straightTo1 = "AS QH KS TD JH";
78 80
         final String straightTo5 = "4S AH 3S 2D 5H";
79
-        assertEquals(Arrays.asList(straightTo1), Poker.bestHands(Arrays.asList(straightTo1, straightTo5)));
81
+        assertEquals(Arrays.asList(straightTo1), new Poker(Arrays.asList(straightTo1, straightTo5)).getBestHands());
80 82
     }
81 83
 
82 84
     @Test
@@ -84,7 +86,7 @@ public class PokerTest
84 86
     public void straightVsFlush() {
85 87
         final String straightTo8 = "4S 6H 7S 8D 5H";
86 88
         final String flushTo7 = "2S 4S 5S 6S 7S";
87
-        assertEquals(Arrays.asList(flushTo7 ), Poker.bestHands(Arrays.asList(straightTo8, flushTo7 )));
89
+        assertEquals(Arrays.asList(flushTo7 ), new Poker(Arrays.asList(straightTo8, flushTo7)).getBestHands());
88 90
     }
89 91
 
90 92
     @Test
@@ -92,7 +94,7 @@ public class PokerTest
92 94
     public void twoFlushes() {
93 95
         final String flushTo8 = "3H 6H 7H 8H 5H";
94 96
         final String flushTo7 = "2S 4S 5S 6S 7S";
95
-        assertEquals(Arrays.asList(flushTo8), Poker.bestHands(Arrays.asList(flushTo8, flushTo7)));
97
+        assertEquals(Arrays.asList(flushTo8), new Poker(Arrays.asList(flushTo8, flushTo7)).getBestHands());
96 98
     }
97 99
 
98 100
     @Test
@@ -100,7 +102,7 @@ public class PokerTest
100 102
     public void flushVsFull() {
101 103
         final String flushTo8 = "3H 6H 7H 8H 5H";
102 104
         final String full = "4S 5H 4S 5D 4H";
103
-        assertEquals(Arrays.asList(full ), Poker.bestHands(Arrays.asList(full, flushTo8)));
105
+        assertEquals(Arrays.asList(full ), new Poker(Arrays.asList(full, flushTo8)).getBestHands());
104 106
     }
105 107
 
106 108
     @Test
@@ -108,7 +110,7 @@ public class PokerTest
108 110
     public void twoFulls() {
109 111
         final String fullOf4By9 = "4H 4S 4D 9S 9D";
110 112
         final String fullOf5By8 = "5H 5S 5D 8S 8D";
111
-        assertEquals(Arrays.asList(fullOf5By8 ), Poker.bestHands(Arrays.asList(fullOf4By9, fullOf5By8)));
113
+        assertEquals(Arrays.asList(fullOf5By8 ), new Poker(Arrays.asList(fullOf4By9, fullOf5By8)).getBestHands());
112 114
     }
113 115
 
114 116
     @Test
@@ -116,7 +118,7 @@ public class PokerTest
116 118
     public void fullVsSquare() {
117 119
         final String full = "4S 5H 4S 5D 4H";
118 120
         final String squareOf3 = "3S 3H 2S 3D 3H";
119
-        assertEquals(Arrays.asList(squareOf3 ), Poker.bestHands(Arrays.asList(full, squareOf3 )));
121
+        assertEquals(Arrays.asList(squareOf3 ), new Poker(Arrays.asList(full, squareOf3)).getBestHands());
120 122
     }
121 123
 
122 124
     @Test
@@ -124,7 +126,7 @@ public class PokerTest
124 126
     public void twoSquares() {
125 127
         final String squareOf2 = "2S 2H 2S 8D 2H";
126 128
         final String squareOf5 = "4S 5H 5S 5D 5H";
127
-        assertEquals(Arrays.asList(squareOf5), Poker.bestHands(Arrays.asList(squareOf2, squareOf5)));
129
+        assertEquals(Arrays.asList(squareOf5), new Poker(Arrays.asList(squareOf2, squareOf5)).getBestHands());
128 130
     }
129 131
 
130 132
     @Test
@@ -132,7 +134,7 @@ public class PokerTest
132 134
     public void squareVsStraightFlush() {
133 135
         final String squareOf5 = "4S 5H 5S 5D 5H";
134 136
         final String straightFlushTo9 = "5S 7S 8S 9S 6S";
135
-        assertEquals(Arrays.asList(straightFlushTo9 ), Poker.bestHands(Arrays.asList(squareOf5, straightFlushTo9 )));
137
+        assertEquals(Arrays.asList(straightFlushTo9 ), new Poker(Arrays.asList(squareOf5, straightFlushTo9)).getBestHands());
136 138
     }
137 139
 
138 140
     @Test
@@ -140,7 +142,7 @@ public class PokerTest
140 142
     public void twoStraightFlushes() {
141 143
         final String straightFlushTo8 = "4H 6H 7H 8H 5H";
142 144
         final String straightFlushTo9 = "5S 7S 8S 9S 6S";
143
-        assertEquals(Arrays.asList(straightFlushTo9 ), Poker.bestHands(Arrays.asList(straightFlushTo8, straightFlushTo9)));
145
+        assertEquals(Arrays.asList(straightFlushTo9 ), new Poker(Arrays.asList(straightFlushTo8, straightFlushTo9)).getBestHands());
144 146
     }
145 147
 
146 148
     @Test
@@ -149,7 +151,7 @@ public class PokerTest
149 151
         final String spadeStraightTo9 = "9S 8S 7S 6S 5S";
150 152
         final String diamondStraightTo9 = "9D 8D 7D 6D 5D";
151 153
         final String threeOf4 = "4D 4S 4H QS KS";
152
-        assertEquals(Arrays.asList(spadeStraightTo9, diamondStraightTo9), Poker.bestHands(Arrays.asList(spadeStraightTo9, diamondStraightTo9, threeOf4)));
154
+        assertEquals(Arrays.asList(spadeStraightTo9, diamondStraightTo9), new Poker(Arrays.asList(spadeStraightTo9, diamondStraightTo9, threeOf4)).getBestHands());
153 155
     }
154 156
 
155 157
     @Test
@@ -157,6 +159,6 @@ public class PokerTest
157 159
     public void straightTo5AgainstAPairOfJacks() {
158 160
         final String straightTo5 = "2S 4D 5C 3S AS";
159 161
         final String twoJacks = "JD 8D 7D JC 5D";
160
-        assertEquals(Arrays.asList(straightTo5), Poker.bestHands(Arrays.asList(straightTo5, twoJacks)));
162
+        assertEquals(Arrays.asList(straightTo5), new Poker(Arrays.asList(straightTo5, twoJacks)).getBestHands());
161 163
     }
162 164
 }

+ 1
- 0
exercises/settings.gradle Voir le fichier

@@ -71,3 +71,4 @@ include 'twelve-days'
71 71
 include 'word-count'
72 72
 include 'word-search'
73 73
 include 'wordy'
74
+include 'poker'