Skip to content

Commit f63f64d

Browse files
authored
Merge pull request exercism#384 from Zaldrick/poker
poker : add to track
2 parents 683b7dc + 29a7738 commit f63f64d

8 files changed

Lines changed: 350 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,15 @@
429429

430430
]
431431
},
432+
{
433+
"slug": "poker",
434+
"difficulty": 7,
435+
"topics": [
436+
"Parsing",
437+
"Sorting",
438+
"Games"
439+
]
440+
},
432441
{
433442
"slug": "clock",
434443
"difficulty": 7,

‎exercises/poker/build.gradle‎

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

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,4 @@ include 'two-fer'
7575
include 'word-count'
7676
include 'word-search'
7777
include 'wordy'
78+
include 'poker'

0 commit comments

Comments
 (0)