From 6b486dbf3df6faef186e4228ce7d3f5c3ddc76fb Mon Sep 17 00:00:00 2001 From: Matt Huggins Date: Sun, 17 Dec 2023 09:53:23 -0600 Subject: [PATCH] fix: evaluate full houses comprised of trips twice --- src/__tests__/evaluate.test.ts | 14 ++++++++++++++ src/__tests__/odds.test.ts | 10 +++++----- src/evaluate.ts | 9 ++++++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/__tests__/evaluate.test.ts b/src/__tests__/evaluate.test.ts index d460a5a..c5eed3e 100644 --- a/src/__tests__/evaluate.test.ts +++ b/src/__tests__/evaluate.test.ts @@ -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, diff --git a/src/__tests__/odds.test.ts b/src/__tests__/odds.test.ts index 6ac2606..4028d59 100644 --- a/src/__tests__/odds.test.ts +++ b/src/__tests__/odds.test.ts @@ -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 }, ]); }); @@ -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 }, ]); }); diff --git a/src/evaluate.ts b/src/evaluate.ts index 8647efd..43a80a0 100644 --- a/src/evaluate.ts +++ b/src/evaluate.ts @@ -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) {