Skip to content

Commit

Permalink
fix: evaluate full houses comprised of trips twice
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuggins committed Dec 17, 2023
1 parent 55c5415 commit 6b486db
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
14 changes: 14 additions & 0 deletions src/__tests__/evaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ describe('evaluate', () => {
});
});

it('recognizes full houses that are comprised of trips twice', () => {
expect(
evaluate({
holeCards: ['5d', '5c'],
minimumHoleCards: 0,
maximumHoleCards: 2,
communityCards: ['Kc', '5h', 'Kd', 'Kh'],
}),
).toEqual({
strength: HandStrength.FullHouse,
hand: ['Kc', 'Kd', 'Kh', '5d', '5c'],
});
});

it('recognizes flushes', () => {
expect(evaluate({ holeCards: ['Js', 'Qd', '8s', '4s', '6c', 'Qs', 'As'] })).toEqual({
strength: HandStrength.Flush,
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/odds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('odds', () => {
];

expect(odds(hands, { ...holdemOptions, communityCards: ['Qd', 'Js', '8d'] })).toEqual([
{ wins: 304, ties: 0, total: 1980 },
{ wins: 1676, ties: 0, total: 1980 },
{ wins: 298, ties: 0, total: 1980 },
{ wins: 1682, ties: 0, total: 1980 },
]);
});

Expand All @@ -42,9 +42,9 @@ describe('odds', () => {
];

expect(odds(hands, { ...holdemOptions, communityCards: ['Qd', 'Js', '8h'] })).toEqual([
{ wins: 58, ties: 234, total: 1806 },
{ wins: 56, ties: 234, total: 1806 },
{ wins: 1458, ties: 0, total: 1806 },
{ wins: 58, ties: 228, total: 1806 },
{ wins: 56, ties: 228, total: 1806 },
{ wins: 1464, ties: 0, total: 1806 },
]);
});

Expand Down
9 changes: 8 additions & 1 deletion src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,20 @@ const evaluateHand = (unsortedCards: Card[]): EvaluatedHand => {
return { strength: HandStrength.FourOfAKind, hand: [...quads, ...kickers] };
}

// full house
// full house (via trips and a pair)
const allTrips = getCardsOfLength(duplicates, 3);
const allPairs = getCardsOfLength(duplicates, 2);

if (allTrips.length > 0 && allPairs.length > 0) {
return { strength: HandStrength.FullHouse, hand: [...allTrips[0], ...allPairs[0]] };
}

// full house (via trips twice, which can happen on a board like KKK5 w/ pocket pair 55)
if (allTrips.length >= 2) {
allTrips.sort((a, b) => cardComparator(a[0], b[0]));
return { strength: HandStrength.FullHouse, hand: [...allTrips[0], ...allTrips[1].slice(0, 2)] };
}

// flush
const flushes = getFlushes(cards);
if (flushes.length > 0) {
Expand Down

0 comments on commit 6b486db

Please sign in to comment.