Przeglądaj źródła

code improvements (adding Card & Hand classes)

Zaldrick 9 lat temu
rodzic
commit
fe2139bf0e

+ 25
- 0
exercises/poker/src/example/java/Card.java Wyświetl plik

@@ -0,0 +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
+	}
25
+}

+ 106
- 0
exercises/poker/src/example/java/Hand.java Wyświetl plik

@@ -0,0 +1,106 @@
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
+	}
106
+}

+ 1
- 126
exercises/poker/src/example/java/Poker.java Wyświetl plik

@@ -1,139 +1,14 @@
1 1
 import java.util.ArrayList;
2
-import java.util.Arrays;
3
-import java.util.Collections;
4 2
 import java.util.Comparator;
5
-import java.util.HashMap;
6
-import java.util.Iterator;
7
-import java.util.LinkedHashMap;
8 3
 import java.util.List;
9
-import java.util.Map;
10 4
 import java.util.stream.Collectors;
11 5
 
12 6
 public class Poker {
13
-	private static class Card {
14
-		private int rank;
15
-		private int suit;
16
-
17
-		public Card(String card) {
18
-			rank = parseRank(card);
19
-			suit = parseSuit(card);
20
-		}
21
-
22
-		private int getRank() {
23
-			return rank;
24
-		}
25
-
26
-		private int getSuit() {
27
-			return suit;
28
-		}
29
-
30
-		private static int parseRank(String card) {
31
-			return "..23456789TJQKA".indexOf(card.charAt(0));
32
-		}
33
-
34
-		private static int parseSuit(String card) {
35
-			return ".HSDC".indexOf(card.charAt(1));
36
-		}
37
-	}
38
-
39
-		private static class Hand {
40
-			private String input;
41
-			private int score;
42
-
43
-			private Hand(String hand) {
44
-				this.input = hand;
45
-				this.score = scoreHand(parseCards(hand));
46
-			}
47
-
48
-			private int getScore() {
49
-				return score;
50
-			}
51
-
52
-			private String getInput() {
53
-				return input;
54
-			}
55
-
56
-			private static List<Card> parseCards(String hand) {
57
-				ArrayList<Card> parseCards = new ArrayList<Card>();
58
-				String[] parseString = hand.split(" ");
59
-				for (int i = 0; i < parseString.length; i++) {
60
-					parseCards.add(new Poker.Card(parseString[i]));
61
-				}
62
-				return parseCards;
63
-			}
64
-
65
-			private static int scoreHand(List<Card> cards) {
66
-
67
-				List<Card> cardsByrank = new ArrayList<Card>();
68
-
69
-				cardsByrank = cards.stream().sorted(Comparator.comparing(Card::getRank)).unordered()
70
-						.collect(Collectors.toList());
71
-
72
-				Map<Integer, Integer> frequencymap = new HashMap<Integer, Integer>();
73
-				for (Card c : cards) {
74
-					if (frequencymap.containsKey(c.rank)) {
75
-						frequencymap.put(c.getRank(), frequencymap.get(c.getRank()) + 1);
76
-					} else {
77
-						frequencymap.put(c.getRank(), 1);
78
-					}
79
-				}
80
-
81
-				List<Integer> ranks = frequencymap.entrySet().stream().map(x -> x.getKey())
82
-						.sorted(Comparator.reverseOrder()).collect(Collectors.toList());
83
-
84
-				frequencymap = frequencymap.entrySet().stream()
85
-						.sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(Collectors
86
-								.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
87
-
88
-				List<Integer> rankCounts = frequencymap.entrySet().stream().map(x -> x.getValue())
89
-						.collect(Collectors.toList());
90
-
91
-				List<Integer> suits = cards.stream().map(s -> s.getSuit()).collect(Collectors.toList());
92
-
93
-				if (ranks.equals(Arrays.asList(14, 5, 4, 3, 2))) {
94
-					ranks = Arrays.asList(5, 4, 3, 2, 1);
95
-				}
96
-
97
-				boolean flush = suits.stream().distinct().count() == 1;
98
-				boolean straight = ranks.stream().distinct().count() == 5 && ranks.get(0) - ranks.get(4) == 4;
99
-				Iterator<Integer> itr = frequencymap.keySet().iterator();
100
-				int pairA = itr.next();
101
-
102
-				if (straight && flush) {
103
-					return 800 + frequencymap.keySet().iterator().next();
104
-				}
105
-				if (rankCounts.equals(Arrays.asList(4, 1))) {
106
-					return (700 + (cardsByrank.get(0)).getRank());
107
-				}
108
-				if (rankCounts.equals(Arrays.asList(3, 2))) {
109
-					return 600 + cardsByrank.get(0).getRank();
110
-				}
111
-				if (flush) {
112
-					return 500 + frequencymap.keySet().iterator().next();
113
-				}
114
-				if (straight) {
115
-					return 400 + frequencymap.keySet().iterator().next();
116
-				}
117
-				if (rankCounts.equals(Arrays.asList(3, 1, 1))) {
118
-					return 300 + cardsByrank.get(0).getRank();
119
-				}
120
-				if (rankCounts.equals(Arrays.asList(2, 2, 1))) {
121
-					return 200 + Math.max(pairA, itr.next());
122
-				}
123
-				if (rankCounts.equals(Arrays.asList(2, 1, 1, 1))) {
124
-					return 100 + pairA;
125
-				}
126
-				Collections.sort(ranks, (a, b) -> b.compareTo(a));
127
-				Collections.reverse(ranks);
128
-				return ranks.get(0);
129
-			}
130
-		}
131 7
 			public static List<String> bestHands(List<String> hands) {
132 8
 				ArrayList<Hand> scoredHands = new ArrayList<Hand>();
133 9
 
134
-				for (String s : hands) {
10
+				for (String s : hands)
135 11
 					scoredHands.add(new Hand(s));
136
-				}
137 12
 
138 13
 				int maxscore = scoredHands.stream().map(h -> h.getScore()).max(Comparator.naturalOrder()).get();
139 14
 				return scoredHands.stream().filter(h -> h.getScore() == maxscore).map(h -> h.getInput())

+ 0
- 4
exercises/poker/src/main/java/Poker.java Wyświetl plik

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

+ 19
- 38
exercises/poker/src/test/java/PokerTest.java Wyświetl plik

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