ソースを参照

poker: sort settings.gradle and tidy up

Fix formatting and Intellij warnings. Remove starter implementation as
this is after the first 20 exercises. Move poker in settings.gradle so
that it's sorted alphabetically.
Frida Tveit 9 年 前
コミット
066c38c049

+ 20
- 20
exercises/poker/src/example/java/Card.java ファイルの表示

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

+ 97
- 91
exercises/poker/src/example/java/Hand.java ファイルの表示

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

+ 27
- 20
exercises/poker/src/example/java/Poker.java ファイルの表示

@@ -1,27 +1,34 @@
1
-import java.util.ArrayList;
2
-import java.util.Comparator;
3
-import java.util.List;
1
+import java.util.*;
4 2
 import java.util.stream.Collectors;
5 3
 
6
-public class Poker {
7
-	private final List<String> bestHands;
4
+class Poker {
5
+    private final List<String> bestHands;
8 6
 
9
-	public Poker(final List<String> hands) {
10
-		bestHands = bestHands(hands);
11
-	}
7
+    Poker(final List<String> hands) {
8
+        bestHands = bestHands(hands);
9
+    }
12 10
 
13
-	public List<String> getBestHands() {
14
-		return bestHands;
15
-	}
11
+    List<String> getBestHands() {
12
+        return bestHands;
13
+    }
16 14
 
17
-	public List<String> bestHands(final List<String> hands) {
18
-		ArrayList<Hand> scoredHands = new ArrayList<Hand>();
15
+    private List<String> bestHands(final List<String> hands) {
16
+        ArrayList<Hand> scoredHands = new ArrayList<>();
19 17
 
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
-	}
18
+        for (String s : hands) {
19
+            scoredHands.add(new Hand(s));
20
+        }
21
+        Optional<Integer> maxScoreIfAny = scoredHands
22
+                .stream()
23
+                .map(Hand::getScore)
24
+                .max(Comparator.naturalOrder());
25
+        return maxScoreIfAny
26
+                .map(maxScore -> scoredHands
27
+                        .stream()
28
+                        .filter(h -> h.getScore() == maxScore)
29
+                        .map(Hand::getInput)
30
+                        .collect(Collectors.toList()))
31
+                .orElseGet(Collections::emptyList);
32
+
33
+    }
27 34
 }

+ 0
- 0
exercises/poker/src/main/java/.keep ファイルの表示


+ 0
- 2
exercises/poker/src/main/java/Poker.java ファイルの表示

@@ -1,2 +0,0 @@
1
-public class Poker {
2
-}

+ 15
- 16
exercises/poker/src/test/java/PokerTest.java ファイルの表示

@@ -1,16 +1,15 @@
1
-import static org.junit.Assert.assertEquals;
1
+import org.junit.Ignore;
2
+import org.junit.Test;
2 3
 
3 4
 import java.util.Arrays;
4 5
 
5
-import org.junit.Test;
6
-
7
-import org.junit.Ignore;
6
+import static org.junit.Assert.assertEquals;
8 7
 
9 8
 public class PokerTest {
10 9
     @Test
11 10
     public void oneHand() {
12 11
         final String hand = "4S 5S 7H 8D JC";
13
-        assertEquals(Arrays.asList(hand),new Poker(Arrays.asList(hand)).getBestHands());
12
+        assertEquals(Arrays.asList(hand), new Poker(Arrays.asList(hand)).getBestHands());
14 13
     }
15 14
 
16 15
     @Ignore("Remove to run test")
@@ -18,7 +17,7 @@ public class PokerTest {
18 17
     public void nothingVsOnePair() {
19 18
         final String nothing = "4S 5H 6S 8D JH";
20 19
         final String pairOf4 = "2S 4H 6S 4D JH";
21
-        assertEquals(Arrays.asList(pairOf4),new Poker(Arrays.asList(nothing, pairOf4)).getBestHands());
20
+        assertEquals(Arrays.asList(pairOf4), new Poker(Arrays.asList(nothing, pairOf4)).getBestHands());
22 21
     }
23 22
 
24 23
     @Ignore("Remove to run test")
@@ -26,7 +25,7 @@ public class PokerTest {
26 25
     public void twoPairs() {
27 26
         final String pairOf2 = "4S 2H 6S 2D JH";
28 27
         final String pairOf4 = "2S 4H 6S 4D JH";
29
-        assertEquals(Arrays.asList(pairOf4), new Poker(Arrays.asList(pairOf2,pairOf4)).getBestHands());
28
+        assertEquals(Arrays.asList(pairOf4), new Poker(Arrays.asList(pairOf2, pairOf4)).getBestHands());
30 29
     }
31 30
 
32 31
     @Ignore("Remove to run test")
@@ -34,7 +33,7 @@ public class PokerTest {
34 33
     public void onePairVsDoublePair() {
35 34
         final String pairOf8 = "2S 8H 6S 8D JH";
36 35
         final String doublePair = "4S 5H 4S 8D 5H";
37
-        assertEquals(Arrays.asList(doublePair),  new Poker(Arrays.asList(pairOf8, doublePair)).getBestHands());
36
+        assertEquals(Arrays.asList(doublePair), new Poker(Arrays.asList(pairOf8, doublePair)).getBestHands());
38 37
     }
39 38
 
40 39
     @Ignore("Remove to run test")
@@ -50,7 +49,7 @@ public class PokerTest {
50 49
     public void doublePairVsThree() {
51 50
         final String doublePair2And8 = "2S 8H 2S 8D JH";
52 51
         final String threeOf4 = "4S 5H 4S 8D 4H";
53
-        assertEquals(Arrays.asList(threeOf4 ), new Poker(Arrays.asList(doublePair2And8, threeOf4)).getBestHands());
52
+        assertEquals(Arrays.asList(threeOf4), new Poker(Arrays.asList(doublePair2And8, threeOf4)).getBestHands());
54 53
     }
55 54
 
56 55
     @Ignore("Remove to run test")
@@ -74,7 +73,7 @@ public class PokerTest {
74 73
     public void twoStraights() {
75 74
         final String straightTo8 = "4S 6H 7S 8D 5H";
76 75
         final String straightTo9 = "5S 7H 8S 9D 6H";
77
-        assertEquals(Arrays.asList(straightTo9 ), new Poker(Arrays.asList(straightTo8, straightTo9)).getBestHands());
76
+        assertEquals(Arrays.asList(straightTo9), new Poker(Arrays.asList(straightTo8, straightTo9)).getBestHands());
78 77
 
79 78
         final String straightTo1 = "AS QH KS TD JH";
80 79
         final String straightTo5 = "4S AH 3S 2D 5H";
@@ -86,7 +85,7 @@ public class PokerTest {
86 85
     public void straightVsFlush() {
87 86
         final String straightTo8 = "4S 6H 7S 8D 5H";
88 87
         final String flushTo7 = "2S 4S 5S 6S 7S";
89
-        assertEquals(Arrays.asList(flushTo7 ), new Poker(Arrays.asList(straightTo8, flushTo7)).getBestHands());
88
+        assertEquals(Arrays.asList(flushTo7), new Poker(Arrays.asList(straightTo8, flushTo7)).getBestHands());
90 89
     }
91 90
 
92 91
     @Ignore("Remove to run test")
@@ -102,7 +101,7 @@ public class PokerTest {
102 101
     public void flushVsFull() {
103 102
         final String flushTo8 = "3H 6H 7H 8H 5H";
104 103
         final String full = "4S 5H 4S 5D 4H";
105
-        assertEquals(Arrays.asList(full ), new Poker(Arrays.asList(full, flushTo8)).getBestHands());
104
+        assertEquals(Arrays.asList(full), new Poker(Arrays.asList(full, flushTo8)).getBestHands());
106 105
     }
107 106
 
108 107
     @Ignore("Remove to run test")
@@ -110,7 +109,7 @@ public class PokerTest {
110 109
     public void twoFulls() {
111 110
         final String fullOf4By9 = "4H 4S 4D 9S 9D";
112 111
         final String fullOf5By8 = "5H 5S 5D 8S 8D";
113
-        assertEquals(Arrays.asList(fullOf5By8 ), new Poker(Arrays.asList(fullOf4By9, fullOf5By8)).getBestHands());
112
+        assertEquals(Arrays.asList(fullOf5By8), new Poker(Arrays.asList(fullOf4By9, fullOf5By8)).getBestHands());
114 113
     }
115 114
 
116 115
     @Ignore("Remove to run test")
@@ -118,7 +117,7 @@ public class PokerTest {
118 117
     public void fullVsSquare() {
119 118
         final String full = "4S 5H 4S 5D 4H";
120 119
         final String squareOf3 = "3S 3H 2S 3D 3H";
121
-        assertEquals(Arrays.asList(squareOf3 ), new Poker(Arrays.asList(full, squareOf3)).getBestHands());
120
+        assertEquals(Arrays.asList(squareOf3), new Poker(Arrays.asList(full, squareOf3)).getBestHands());
122 121
     }
123 122
 
124 123
     @Ignore("Remove to run test")
@@ -134,7 +133,7 @@ public class PokerTest {
134 133
     public void squareVsStraightFlush() {
135 134
         final String squareOf5 = "4S 5H 5S 5D 5H";
136 135
         final String straightFlushTo9 = "5S 7S 8S 9S 6S";
137
-        assertEquals(Arrays.asList(straightFlushTo9 ), new Poker(Arrays.asList(squareOf5, straightFlushTo9)).getBestHands());
136
+        assertEquals(Arrays.asList(straightFlushTo9), new Poker(Arrays.asList(squareOf5, straightFlushTo9)).getBestHands());
138 137
     }
139 138
 
140 139
     @Ignore("Remove to run test")
@@ -142,7 +141,7 @@ public class PokerTest {
142 141
     public void twoStraightFlushes() {
143 142
         final String straightFlushTo8 = "4H 6H 7H 8H 5H";
144 143
         final String straightFlushTo9 = "5S 7S 8S 9S 6S";
145
-        assertEquals(Arrays.asList(straightFlushTo9 ), new Poker(Arrays.asList(straightFlushTo8, straightFlushTo9)).getBestHands());
144
+        assertEquals(Arrays.asList(straightFlushTo9), new Poker(Arrays.asList(straightFlushTo8, straightFlushTo9)).getBestHands());
146 145
     }
147 146
 
148 147
     @Ignore("Remove to run test")

+ 1
- 1
exercises/settings.gradle ファイルの表示

@@ -47,6 +47,7 @@ include 'pascals-triangle'
47 47
 include 'perfect-numbers'
48 48
 include 'phone-number'
49 49
 include 'pig-latin'
50
+include 'poker'
50 51
 include 'prime-factors'
51 52
 include 'pythagorean-triplet'
52 53
 include 'queen-attack'
@@ -77,4 +78,3 @@ include 'two-fer'
77 78
 include 'word-count'
78 79
 include 'word-search'
79 80
 include 'wordy'
80
-include 'poker'