Skip to content

Commit

Permalink
Sync Poker (#2878)
Browse files Browse the repository at this point in the history
This updates the Poker to be in sync with the problem specifications.
  • Loading branch information
jagdish-15 authored Dec 30, 2024
1 parent ea11011 commit 84c4902
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 209 deletions.
4 changes: 2 additions & 2 deletions exercises/practice/poker/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"aadityakulkarni",
"FridaTveit",
"hgvanpariya",
"jagdish-15",
"jmrunkle",
"katmpatz",
"kytrinyx",
"lemoncurry",
"MartinDekanovsky",
"mirkoperillo",
"msomji",
"muzimuzhi",
Expand All @@ -27,8 +29,6 @@
"src/test/java/PokerTest.java"
],
"example": [
".meta/src/reference/java/Card.java",
".meta/src/reference/java/Hand.java",
".meta/src/reference/java/Poker.java"
],
"invalidator": [
Expand Down
28 changes: 0 additions & 28 deletions exercises/practice/poker/.meta/src/reference/java/Card.java

This file was deleted.

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

This file was deleted.

140 changes: 119 additions & 21 deletions exercises/practice/poker/.meta/src/reference/java/Poker.java
Original file line number Diff line number Diff line change
@@ -1,34 +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(final List<String> hands) {
bestHands = bestHands(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> bestHands(final List<String> hands) {
ArrayList<Hand> scoredHands = new ArrayList<>();
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");
}
}
}
}

numericalValues = new ArrayList<>();
for (String value : values) {
numericalValues.add(Integer.valueOf(value));
}
Collections.sort(numericalValues);

for (String s : hands) {
scoredHands.add(new Hand(s));
List<Integer> possibleValues = new ArrayList<>();
counts = new ArrayList<>();
for (int i = 1; i <= 14; i++) {
counts.add(i - 1, countOccurrences(i));
possibleValues.add(i);
}
Optional<Integer> maxScoreIfAny = scoredHands
.stream()
.map(Hand::getScore)
.max(Comparator.naturalOrder());
return maxScoreIfAny
.map(maxScore -> scoredHands
.stream()
.filter(h -> h.getScore() == maxScore)
.map(Hand::getInput)
.collect(Collectors.toList()))
.orElseGet(Collections::emptyList);

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();
}
}
Loading

0 comments on commit 84c4902

Please sign in to comment.