Skip to content

Commit 625fecd

Browse files
committed
Updating reference answers to pass newly added tests
1 parent 99eef27 commit 625fecd

File tree

5 files changed

+122
-102
lines changed

5 files changed

+122
-102
lines changed

exercises/practice/poker/.meta/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"katmpatz",
1212
"kytrinyx",
1313
"lemoncurry",
14+
"MartinDekanovsky",
1415
"mirkoperillo",
1516
"msomji",
1617
"muzimuzhi",

exercises/practice/poker/.meta/src/reference/java/Card.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

exercises/practice/poker/.meta/src/reference/java/Hand.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 120 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,132 @@
1-
import java.util.*;
2-
import java.util.stream.Collectors;
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
34

45
class Poker {
5-
private final List<String> bestHands;
66

7-
Poker(List<String> hands) {
8-
this.bestHands = evaluateBestHands(hands);
7+
private List<String> hands;
8+
private List<Integer> numericalValues;
9+
private List<Integer> counts;
10+
11+
public Poker(List<String> hands) {
12+
this.hands = hands;
913
}
1014

11-
List<String> getBestHands() {
15+
public List<String> getBestHands() {
16+
List<String> bestHands = new ArrayList<>();
17+
bestHands.add(hands.get(0));
18+
19+
for (int i = 1; i < hands.size(); i++) {
20+
if (getHandRank(hands.get(i)) > getHandRank(bestHands.get(0))) {
21+
bestHands.set(0, hands.get(i));
22+
} else if (getHandRank(hands.get(i)) == getHandRank(bestHands.get(0))) {
23+
getHandRank(bestHands.get(0));
24+
List<Integer> firstHand = counts;
25+
26+
getHandRank(hands.get(i));
27+
List<Integer> secondHand = counts;
28+
29+
if (firstHand.equals(secondHand)) {
30+
bestHands.add(hands.get(i));
31+
} else {
32+
for (int j = 4; j >= 2; j--) {
33+
if (firstHand.contains(j) && secondHand.contains(j)) {
34+
if (firstHand.lastIndexOf(j) < secondHand.lastIndexOf(j)) {
35+
bestHands.set(0, hands.get(i));
36+
break;
37+
} else if (firstHand.lastIndexOf(j) > secondHand.lastIndexOf(j)) {
38+
break;
39+
} else if (firstHand.lastIndexOf(j) == secondHand.lastIndexOf(j) && j == 2) {
40+
if (firstHand.indexOf(j) < secondHand.indexOf(j)) {
41+
bestHands.set(0, hands.get(i));
42+
}
43+
}
44+
}
45+
}
46+
for (int k = firstHand.size() - 1; k >= 0; k--) {
47+
if (firstHand.get(k) <= 1 && secondHand.get(k) <= 1) {
48+
if (firstHand.get(k) < secondHand.get(k)) {
49+
bestHands.set(0, hands.get(i));
50+
break;
51+
} else if (firstHand.get(k) > secondHand.get(k)) {
52+
break;
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
1259
return bestHands;
1360
}
1461

15-
private List<String> evaluateBestHands(List<String> hands) {
16-
List<Hand> parsedHands = hands.stream()
17-
.map(Hand::new)
18-
.collect(Collectors.toList());
62+
public int getHandRank(String hand) {
63+
String[] cards = hand.split(" ");
64+
List<String> values = new ArrayList<>();
65+
List<String> suits = new ArrayList<>();
66+
for (String card : cards) {
67+
if (card.length() == 2) {
68+
values.add(card.substring(0, 1));
69+
suits.add(card.substring(1));
70+
} else {
71+
values.add(card.substring(0, 2));
72+
suits.add(card.substring(2));
73+
}
74+
}
75+
76+
for (int i = 0; i < values.size(); i++) {
77+
switch (values.get(i)) {
78+
case "J" -> values.set(i, "11");
79+
case "Q" -> values.set(i, "12");
80+
case "K" -> values.set(i, "13");
81+
case "A" -> {
82+
if (values.contains("2") && values.contains("3") && values.contains("4") && values.contains("5")) {
83+
values.set(i, "1");
84+
} else {
85+
values.set(i, "14");
86+
}
87+
}
88+
}
89+
}
1990

20-
int maxScore = parsedHands.stream()
21-
.mapToInt(Hand::calculateScore)
22-
.max()
23-
.orElse(0);
91+
numericalValues = new ArrayList<>();
92+
for (String value : values) {
93+
numericalValues.add(Integer.valueOf(value));
94+
}
95+
Collections.sort(numericalValues);
2496

25-
return parsedHands.stream()
26-
.filter(hand -> hand.calculateScore() == maxScore)
27-
.map(Hand::getOriginal)
28-
.collect(Collectors.toList());
97+
List<Integer> possibleValues = new ArrayList<>();
98+
counts = new ArrayList<>();
99+
for (int i = 1; i <= 14; i++) {
100+
counts.add(i - 1, countOccurrences(i));
101+
possibleValues.add(i);
102+
}
103+
104+
boolean isStraight = Collections.indexOfSubList(possibleValues, numericalValues) != -1;
105+
106+
boolean isFlush = suits.stream().distinct().count() == 1;
107+
108+
if (isStraight & isFlush) {
109+
return 8; // straight flush
110+
} else if (counts.contains(4)) {
111+
return 7; // four of a kind
112+
} else if (counts.contains(3) && counts.contains(2)) {
113+
return 6; // full house
114+
} else if (isFlush) {
115+
return 5; // flush
116+
} else if (isStraight) {
117+
return 4; // straight
118+
} else if (counts.contains(3)) {
119+
return 3; // three of a kind
120+
} else if (counts.stream().filter(value -> value == 2).count() == 2) {
121+
return 2; // two pair
122+
} else if (counts.contains(2)) {
123+
return 1; // pair
124+
} else {
125+
return 0; // high card
126+
}
29127
}
128+
129+
public int countOccurrences(int valueToFind) {
130+
return (int) numericalValues.stream().filter(value -> value.equals(valueToFind)).count();
131+
}
30132
}

exercises/practice/poker/src/test/java/PokerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void samePair() {
7777
String pairOf4Lower = "4H 4S AH JC 3D";
7878
String pairOf4Higher = "4C 4D AS 5D 6C";
7979
assertThat(new Poker(Arrays.asList(pairOf4Lower, pairOf4Higher)).getBestHands())
80-
.containsExactly(pairOf4Higher);
80+
.containsExactly(pairOf4Lower);
8181
}
8282

8383
@Disabled("Remove to run test")

0 commit comments

Comments
 (0)