Skip to content

Commit

Permalink
Updating reference answers to pass newly added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jagdish-15 committed Dec 24, 2024
1 parent 99eef27 commit 625fecd
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 102 deletions.
1 change: 1 addition & 0 deletions exercises/practice/poker/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"katmpatz",
"kytrinyx",
"lemoncurry",
"MartinDekanovsky",
"mirkoperillo",
"msomji",
"muzimuzhi",
Expand Down
26 changes: 0 additions & 26 deletions exercises/practice/poker/.meta/src/reference/java/Card.java

This file was deleted.

57 changes: 0 additions & 57 deletions exercises/practice/poker/.meta/src/reference/java/Hand.java

This file was deleted.

138 changes: 120 additions & 18 deletions exercises/practice/poker/.meta/src/reference/java/Poker.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,132 @@
import java.util.*;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Poker {
private final List<String> bestHands;

Poker(List<String> hands) {
this.bestHands = evaluateBestHands(hands);
private List<String> hands;
private List<Integer> numericalValues;
private List<Integer> counts;

public Poker(List<String> hands) {
this.hands = hands;
}

List<String> getBestHands() {
public List<String> getBestHands() {
List<String> bestHands = new ArrayList<>();
bestHands.add(hands.get(0));

for (int i = 1; i < hands.size(); i++) {
if (getHandRank(hands.get(i)) > getHandRank(bestHands.get(0))) {
bestHands.set(0, hands.get(i));
} else if (getHandRank(hands.get(i)) == getHandRank(bestHands.get(0))) {
getHandRank(bestHands.get(0));
List<Integer> firstHand = counts;

getHandRank(hands.get(i));
List<Integer> secondHand = counts;

if (firstHand.equals(secondHand)) {
bestHands.add(hands.get(i));
} else {
for (int j = 4; j >= 2; j--) {
if (firstHand.contains(j) && secondHand.contains(j)) {
if (firstHand.lastIndexOf(j) < secondHand.lastIndexOf(j)) {
bestHands.set(0, hands.get(i));
break;
} else if (firstHand.lastIndexOf(j) > secondHand.lastIndexOf(j)) {
break;
} else if (firstHand.lastIndexOf(j) == secondHand.lastIndexOf(j) && j == 2) {
if (firstHand.indexOf(j) < secondHand.indexOf(j)) {
bestHands.set(0, hands.get(i));
}
}
}
}
for (int k = firstHand.size() - 1; k >= 0; k--) {
if (firstHand.get(k) <= 1 && secondHand.get(k) <= 1) {
if (firstHand.get(k) < secondHand.get(k)) {
bestHands.set(0, hands.get(i));
break;
} else if (firstHand.get(k) > secondHand.get(k)) {
break;
}
}
}
}
}
}
return bestHands;
}

private List<String> evaluateBestHands(List<String> hands) {
List<Hand> parsedHands = hands.stream()
.map(Hand::new)
.collect(Collectors.toList());
public int getHandRank(String hand) {
String[] cards = hand.split(" ");
List<String> values = new ArrayList<>();
List<String> suits = new ArrayList<>();
for (String card : cards) {
if (card.length() == 2) {
values.add(card.substring(0, 1));
suits.add(card.substring(1));
} else {
values.add(card.substring(0, 2));
suits.add(card.substring(2));
}
}

for (int i = 0; i < values.size(); i++) {
switch (values.get(i)) {
case "J" -> values.set(i, "11");
case "Q" -> values.set(i, "12");
case "K" -> values.set(i, "13");
case "A" -> {
if (values.contains("2") && values.contains("3") && values.contains("4") && values.contains("5")) {
values.set(i, "1");
} else {
values.set(i, "14");
}
}
}
}

int maxScore = parsedHands.stream()
.mapToInt(Hand::calculateScore)
.max()
.orElse(0);
numericalValues = new ArrayList<>();
for (String value : values) {
numericalValues.add(Integer.valueOf(value));
}
Collections.sort(numericalValues);

return parsedHands.stream()
.filter(hand -> hand.calculateScore() == maxScore)
.map(Hand::getOriginal)
.collect(Collectors.toList());
List<Integer> possibleValues = new ArrayList<>();
counts = new ArrayList<>();
for (int i = 1; i <= 14; i++) {
counts.add(i - 1, countOccurrences(i));
possibleValues.add(i);
}

boolean isStraight = Collections.indexOfSubList(possibleValues, numericalValues) != -1;

boolean isFlush = suits.stream().distinct().count() == 1;

if (isStraight & isFlush) {
return 8; // straight flush
} else if (counts.contains(4)) {
return 7; // four of a kind
} else if (counts.contains(3) && counts.contains(2)) {
return 6; // full house
} else if (isFlush) {
return 5; // flush
} else if (isStraight) {
return 4; // straight
} else if (counts.contains(3)) {
return 3; // three of a kind
} else if (counts.stream().filter(value -> value == 2).count() == 2) {
return 2; // two pair
} else if (counts.contains(2)) {
return 1; // pair
} else {
return 0; // high card
}
}

public int countOccurrences(int valueToFind) {
return (int) numericalValues.stream().filter(value -> value.equals(valueToFind)).count();
}
}
2 changes: 1 addition & 1 deletion exercises/practice/poker/src/test/java/PokerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void samePair() {
String pairOf4Lower = "4H 4S AH JC 3D";
String pairOf4Higher = "4C 4D AS 5D 6C";
assertThat(new Poker(Arrays.asList(pairOf4Lower, pairOf4Higher)).getBestHands())
.containsExactly(pairOf4Higher);
.containsExactly(pairOf4Lower);
}

@Disabled("Remove to run test")
Expand Down

0 comments on commit 625fecd

Please sign in to comment.