Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Two Plus Two poker hand evaluator, how do you get the best 5 cards combination out of the 7 that you passed to it?

Is it possible to extract that info from the equivalence value?

I understand that the higher the equivalence value the better. Category and rank can also be extracted from the equivalence value. But is there a way to find out what the best 5 cards combination are from the 7 that you passed to it?

Twoplustwo is the fastest poker hand evaluator around (14-15 million hands evaluated per second). You give your 7 cards to it and it spits out a hand equivalence value. The higher the value, the better is card is.

Here's a great summary on twoplustwo: http://www.codingthewheel.com/archives/poker-hand-evaluator-roundup#2p2

Cached version of the link above: http://web.archive.org/web/20130116102452/http://www.codingthewheel.com/archives/poker-hand-evaluator-roundup

like image 260
ian Avatar asked Mar 29 '12 23:03

ian


People also ask

How many combinations of poker hands are there?

The total number of possible hands can be found by adding the above numbers in third column, for a total of 2,598,960. This means that if there are 52 cards, how many combinations of 5 cards can be drawn (answer 2,598,960 combinations).

Is a hand of cards in poker a permutation or combination?

Poker hands are combinations rather than permutations. This means that the order of the cards does not matter.

How many possible 2 card hands are there where you are dealt a pair?

* 44 = 123,552 possible two pair hands.


2 Answers

(disclaimer: I'm working on a poker software that does, amongst other, hand evaluations)

You give your 7 cards to it and it spits out a hand equivalence value.

there are several evaluators doing that and if I'm not mistaken some of them compute more than a hundred million hands per second (!). These evaluators basically come down to 7 array lookups in a gigantic array and it only takes a few cycles (despite the cache misses) to evaluate a hand. I don't think 14-15 millions / second is anywhere near the fastest. CactusKev's evaluator is 10x faster if I'm not mistaken.

Now to answer your question:

how do you get the best 5 cards combination out of the 7 that you passed to it?

Well it doesn't tell you but once you have the strength of the hand it can become very easy: you don't need to re-invent the wheel.

You can use the strength to simplify your "best five out of seven" computation.

You could also use other libraries, giving your directly the five best card (instead of just their strength) or you can use the strength to find the five best cards yourself.

I'll just give a few examples...

  • You know you have a full house (a.k.a. a "boat"), then you know that you're looking for three cards that have the same rank and then the best pair (if there are two pairs, but you're sure to find at least one, because the evaluator told you you have a boat).

  • You know you have a straight: find five cards that follow each other, starting from the best one (beware of the special wheel case case).

    You could also get a bit fancier for the straight: you could take the strength of every possible straight and compare the strength that the evaluator gives you with these. If it matches, say, a ten-high straight, then simply look for any T, 9, 8, 7 and 6 card (no matter the suit).

  • You know you have "no pair": simply take the five highest card you find

  • etc.

There are only a few different ranks... They could be, for example:

NO_PAIR
ONE_PAIR
TWO_PAIRS
SET
STRAIGHT
FLUSH
FULL_HOUSE
FOUR_OF_A_KIND
STRAIGHT_FLUSH

(you could of course create intermediate "wheel straight" and "wheel straight flush" and "royal flush" cases if you want, etc.)

Once you know the which type of hand your hand is (thanks to the fast evaluator you're using), simply switch to a piece of code that finds the five best out of seven for that particular hand.

I think it's a good way to proceed because you leverage the ultra-fast evaluator and it then greatly simplifies your logic.

At startup, you'd need to compute the strenth once, for example by computing:

HIGHEST_NO_PAIR_HAND = ultraFastEvaluator( "As Kd Qh Jc 9d 5s 2c" );

HIGHEST_FULL_HOUSE = ultraFastEvaluator( "As Ac Ad Kh Ks 8s 2h" );

I'm of course not advocating to use strings here. It's just an example...

You could then, for each hand you want to find the actual five best:

  • compute the strength using the fast evaluator
  • is it <= HIGHEST_NO_PAIR_HAND ?
    • yes: take five highest cards
    • no: is it <= HIGHEST_ONE_PAIR_HAND ? yes: take highest pair + highest three cards no: is it <= HIGHEST_TWO_PAIRS_HAND?
      • etc.

So in my opinion you could reuse an API that directly finds the five best out of seven or entirely rewrite your own, but it's going to faster if you use the fast evaluator's result to then simplify your logic.

EDIT note that there's not necessarily one way to do "five best out of seven". For example with As Ac on a Kc Kd Qh Qs 2c board, both "As Ac Kc Kd Qh" and "As Ac Kc Kd Qs" are "five best" (the last queen's suit doesn't matter).

like image 197
TacticalCoder Avatar answered Sep 22 '22 23:09

TacticalCoder


No, it's not possible to extract that information. The lookup table contains only the equivalence values, which are broken into hand type and rank; no other information is preserved.

If you need to evaluate millions of hands per second and get the winning hand for each, instead of just the rank, you'll need to use a different evaluator. If you only need to extract the winning hand rarely, you could use this evaluator, and resort to a slower method to find the best 5 cards when necessary.

like image 20
Nick Johnson Avatar answered Sep 22 '22 23:09

Nick Johnson