Browse Source

poker : add to track

Zaldrick 9 years ago
parent
commit
7ddbc8a6f1

+ 18
- 0
exercises/poker/build.gradle View File

1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+  mavenCentral()
7
+}
8
+
9
+dependencies {
10
+  testCompile "junit:junit:4.12"
11
+}
12
+
13
+test {
14
+  testLogging {
15
+    exceptionFormat = 'full'
16
+    events = ["passed", "failed", "skipped"]
17
+  }
18
+}

+ 137
- 0
exercises/poker/src/example/java/Poker.java View File

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

+ 12
- 0
exercises/poker/src/main/java/Poker.java View File

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

+ 181
- 0
exercises/poker/src/test/java/PokerTest.java View File

1
+import static org.junit.Assert.*;
2
+import java.util.Arrays;
3
+import org.junit.Ignore;
4
+import org.junit.Test;
5
+
6
+public class PokerTest
7
+{
8
+    @Test
9
+    public void One_hand()
10
+    {
11
+        final String hand = "4S 5S 7H 8D JC";
12
+        assertEquals(Arrays.asList(hand),Poker.BestHands(Arrays.asList(hand)));
13
+    }
14
+
15
+    @Test
16
+    @Ignore
17
+    public void Nothing_vs_one_pair()
18
+    {
19
+        final String nothing = "4S 5H 6S 8D JH";
20
+        final String pairOf4 = "2S 4H 6S 4D JH";
21
+        assertEquals(Arrays.asList(pairOf4), Poker.BestHands(Arrays.asList(nothing, pairOf4)));
22
+    }
23
+
24
+    @Test
25
+    @Ignore
26
+    public void Two_pairs()
27
+    {
28
+        final String pairOf2 = "4S 2H 6S 2D JH";
29
+        final String pairOf4 = "2S 4H 6S 4D JH";
30
+        assertEquals(Arrays.asList(pairOf4), Poker.BestHands(Arrays.asList(pairOf2,pairOf4)));
31
+    }
32
+
33
+    @Test
34
+    @Ignore
35
+    public void One_pair_vs_double_pair()
36
+    {
37
+        final String pairOf8 = "2S 8H 6S 8D JH";
38
+        final String doublePair = "4S 5H 4S 8D 5H";
39
+        assertEquals(Arrays.asList(doublePair), Poker.BestHands(Arrays.asList(pairOf8, doublePair)));
40
+    }
41
+
42
+    @Test
43
+    @Ignore
44
+    public void Two_double_pairs()
45
+    {
46
+        final String doublePair2And8 = "2S 8H 2S 8D JH";
47
+        final String doublePair4And5 = "4S 5H 4S 8D 5H";
48
+        assertEquals(Arrays.asList(doublePair2And8), Poker.BestHands(Arrays.asList(doublePair2And8, doublePair4And5)));
49
+    }
50
+
51
+    @Test
52
+    @Ignore
53
+    public void Double_pair_vs_three()
54
+    {
55
+        final String doublePair2And8 = "2S 8H 2S 8D JH";
56
+        final String threeOf4 = "4S 5H 4S 8D 4H";
57
+        assertEquals(Arrays.asList(threeOf4 ), Poker.BestHands(Arrays.asList(doublePair2And8, threeOf4 )));
58
+    }
59
+
60
+    @Test
61
+    @Ignore
62
+    public void Two_threes()
63
+    {
64
+        final String threeOf2 = "2S 2H 2S 8D JH";
65
+        final String threeOf1 = "4S AH AS 8D AH";
66
+        assertEquals(Arrays.asList(threeOf1), Poker.BestHands(Arrays.asList(threeOf2, threeOf1)));
67
+    }
68
+
69
+    @Test
70
+    @Ignore
71
+    public void Three_vs_straight()
72
+    {
73
+        final String threeOf4 = "4S 5H 4S 8D 4H";
74
+        final String straight = "3S 4H 2S 6D 5H";
75
+        assertEquals(Arrays.asList(straight), Poker.BestHands(Arrays.asList(threeOf4, straight )));
76
+    }
77
+
78
+    @Test
79
+    @Ignore
80
+    public void Two_straights()
81
+    {
82
+        final String straightTo8 = "4S 6H 7S 8D 5H";
83
+        final String straightTo9 = "5S 7H 8S 9D 6H";
84
+        assertEquals(Arrays.asList(straightTo9 ), Poker.BestHands(Arrays.asList(straightTo8, straightTo9 )));
85
+
86
+        final String straightTo1 = "AS QH KS TD JH";
87
+        final String straightTo5 = "4S AH 3S 2D 5H";
88
+        assertEquals(Arrays.asList(straightTo1), Poker.BestHands(Arrays.asList(straightTo1, straightTo5)));
89
+    }
90
+
91
+    @Test
92
+    @Ignore
93
+    public void Straight_vs_flush()
94
+    {
95
+        final String straightTo8 = "4S 6H 7S 8D 5H";
96
+        final String flushTo7 = "2S 4S 5S 6S 7S";
97
+        assertEquals(Arrays.asList(flushTo7 ), Poker.BestHands(Arrays.asList(straightTo8, flushTo7 )));
98
+    }
99
+
100
+    @Test
101
+    @Ignore
102
+    public void Two_flushes()
103
+    {
104
+        final String flushTo8 = "3H 6H 7H 8H 5H";
105
+        final String flushTo7 = "2S 4S 5S 6S 7S";
106
+        assertEquals(Arrays.asList(flushTo8), Poker.BestHands(Arrays.asList(flushTo8, flushTo7)));
107
+    }
108
+
109
+    @Test
110
+    @Ignore
111
+    public void Flush_vs_full()
112
+    {
113
+        final String flushTo8 = "3H 6H 7H 8H 5H";
114
+        final String full = "4S 5H 4S 5D 4H";
115
+        assertEquals(Arrays.asList(full ), Poker.BestHands(Arrays.asList(full, flushTo8)));
116
+    }
117
+
118
+    @Test
119
+    @Ignore
120
+    public void Two_fulls()
121
+    {
122
+        final String fullOf4By9 = "4H 4S 4D 9S 9D";
123
+        final String fullOf5By8 = "5H 5S 5D 8S 8D";
124
+        assertEquals(Arrays.asList(fullOf5By8 ), Poker.BestHands(Arrays.asList(fullOf4By9, fullOf5By8)));
125
+    }
126
+
127
+    @Test
128
+    @Ignore
129
+    public void Full_vs_square()
130
+    {
131
+        final String full = "4S 5H 4S 5D 4H";
132
+        final String squareOf3 = "3S 3H 2S 3D 3H";
133
+        assertEquals(Arrays.asList(squareOf3 ), Poker.BestHands(Arrays.asList(full, squareOf3 )));
134
+    }
135
+
136
+    @Test
137
+    @Ignore
138
+    public void Two_squares()
139
+    {
140
+        final String squareOf2 = "2S 2H 2S 8D 2H";
141
+        final String squareOf5 = "4S 5H 5S 5D 5H";
142
+        assertEquals(Arrays.asList(squareOf5), Poker.BestHands(Arrays.asList(squareOf2, squareOf5)));
143
+    }
144
+
145
+    @Test
146
+    @Ignore
147
+    public void Square_vs_straight_flush()
148
+    {
149
+        final String squareOf5 = "4S 5H 5S 5D 5H";
150
+        final String straightFlushTo9 = "5S 7S 8S 9S 6S";
151
+        assertEquals(Arrays.asList(straightFlushTo9 ), Poker.BestHands(Arrays.asList(squareOf5, straightFlushTo9 )));
152
+    }
153
+
154
+    @Test
155
+    @Ignore
156
+    public void Two_straight_flushes()
157
+    {
158
+        final String straightFlushTo8 = "4H 6H 7H 8H 5H";
159
+        final String straightFlushTo9 = "5S 7S 8S 9S 6S";
160
+        assertEquals(Arrays.asList(straightFlushTo9 ), Poker.BestHands(Arrays.asList(straightFlushTo8, straightFlushTo9)));
161
+    }
162
+
163
+    @Test
164
+    @Ignore
165
+    public void Three_hand_with_tie()
166
+    {
167
+        final String spadeStraightTo9 = "9S 8S 7S 6S 5S";
168
+        final String diamondStraightTo9 = "9D 8D 7D 6D 5D";
169
+        final String threeOf4 = "4D 4S 4H QS KS";
170
+        assertEquals(Arrays.asList(spadeStraightTo9, diamondStraightTo9), Poker.BestHands(Arrays.asList(spadeStraightTo9, diamondStraightTo9, threeOf4)));
171
+    }
172
+
173
+    @Test
174
+    @Ignore
175
+    public void Straight_to_5_against_a_pair_of_jacks()
176
+    {
177
+        final String straightTo5 = "2S 4D 5C 3S AS";
178
+        final String twoJacks = "JD 8D 7D JC 5D";
179
+        assertEquals(Arrays.asList(straightTo5), Poker.BestHands(Arrays.asList(straightTo5, twoJacks)));
180
+    }
181
+}