Etian Chamay
Etian Chamay

Reputation: 195

Find the best cards to complete a partial poker hand

I'm trying to find an efficient solution for the following question:

Terminology:

Rank: 1-13 where 11=Jack,12=Queen,13=King

Suit: H=Hearts,C=Clubs,D=Diamonds,S=Spades

"WishCard": a card that can wear any Rank/Suit according to the player's wish without duplicate cards.

Given 2 to 7 cards, and at least 0 to 7 of them are defined as "WishCard", what are the best cards combination a player may have at a specific given time.

Examples:
Stage: Pre-Flop
Hand[4C,*] -> Hand[4C,4H] | Pair

Stage: Flop
Hand[4C,*], Shared[*,10H,5C] -> Hand[4C,10C], Shared[10D,10H,5C] | 10 Three of a kind

Stage: Turn
Hand[4C,*], Shared[*,10H,5C,1S] -> Hand[4C,1C], Shared[1D,10H,5C,1S] | Ace Three of a kind

Stage: River
Hand[4C,*], Shared[*,10H,5C,1S,6C] -> Hand[4C,8C], Shared[7C,10H,5C,1S,6C] | 4C-8C Straight Flush

I feel like brute forcing it will take for ever. is there a faster way to calculate it?

Upvotes: 0

Views: 92

Answers (1)

btilly
btilly

Reputation: 46399

It depends what you mean by "brute forcing it". But it is messy because there are a lot of possibilities and a lot of logic.

I would preprocess it into a data structure, then write functions that test for specific kinds of hands. Then just look for each kind from best to worst using any trick that comes to mind.

An example of preprocessing would be to build a hash of card type/count. So for example on the turn you get:

{4: 1, 10: 1, 4: 1, 5: 1, 1: 1, 14: 1, "*": 2}

And now the fact that "*" appears 2x means that we'd need another 2 to get 4 of a kind or 1 to get 3 of a kind or nothing to get 2 of a kind. Without having to analyze which exact cards those "*"s turn into. You can now build tests for 4 of a kind, full house, 3 of a kind, 2 pairs, and 1 pair.

For straights you run through the sequence keeping track of how many of the last 5 cards you saw. If ever you come short by the number of *s you have a straight.

For flushes you would return the hands for each flush.

For straight flush you would analyze the flush hands for straights.

And then you need to just pick the high cards.

Upvotes: 2

Related Questions