Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Re: Poker hand evaluator



Tip: Looking for answers? Try searching our database.



You are accessing this site in a read-only mode. For full access to all member benefits, including message posting, please login or register. Registration is completely free, simple, and takes only a few seconds.

Login | Free WebmasterKB.com registration | Whole discussion thread

The message you are replying to and its parents are listed in the reverse order with the most recent posts first. This might not be the whole discussion thread. To read all the messages in this thread please click here.

Re: Poker hand evaluator

Johannes Baagoe11 Mar 2010 09:51
Scott Sauyet :

> There are definitely some interesting questions about appropriate
> representation in JS, where 52- bit structures are not easy to manage
> and where bit manipulations are generally not at all optimized.

I agree.

> Maybe if I find some time over the weekend, I'll see what I can
> create.

That would be nice.

>> how much speed can we still tweak our of them ?

> I don't know.  Let's see, shall we?

I'm all in :)

> these two are distinct tasks:

> (1) Find the value of the best 5-card hand that can be made from
> these seven cards.  (2) Find the best 5-card hand that can be made
> from these seven cards.

> But one of the most straightforward ways to do the former involves the
> latter as an intermediate step.  And I think most solutions will have
> an abstract version of the latter embedded in them.  I'd love to see
> a purely mathematical version which doesn't do this in practice, but
> I'm not holding my breath.  What sorts of systems are you imagining
> that don't in some sense find a five-card hand to compare?

They do obviously find a five-card hand, the best one of the 21
possibilities, but not simply by generating the 21 permutations and
evaluating them all. A rough outline:

First, a pass over the seven cards count the number of cards in each
rank and each suit, and keeps a track of the exact seven cards in
case one suit has five cards or more, which means a flush.

If there is a flush, all cards of other suits are meaningless - with
only seven cards, there is no way there could also be a quad or a full
house, and a flush beats anything else. So the only thing to do is
to find the 5 highest cards in that suit, and to check whether there
is an uninterrupted run of five consecutive ranks, making a straight
flush. All that takes is pass through the rank counts, starting from
the top, that is, the aces.

If there is no flush (about 97% of all cases), the only thing that
matters is the number of cards by rank. A provisional version of
the algorithm steps through the rank counts starting from the aces
and keeps count of all the information that is needed - similar to
what is done for the flushes, but rather more involved. The final,
optimised version simply looks in a big table which has been built
by the provisional version and returns the result.

Signature

Johannes


Scott Sauyet10 Mar 2010 20:12
> Scott Sauyet :
>> Johannes Baagoe :
[quoted text clipped - 3 lines]
>
> I have no better reason than a taste for challenges.

That's a good enough reason for me!  There are definitely some
interesting questions about appropriate representation in JS, where 52-
bit structures are not easy to manage and where bit manipulations are
generally not at all optimized.

Maybe if I find some time over the weekend, I'll see what I can
create.

>> If issues of speed are not that important, this would not be hard
>> to code, either in the compare-two-hands version or the score-a-hand
[quoted text clipped - 5 lines]
> love to write one, for that matter, but I am not satisfied by any of
> my efforts so far.

It seems to me that any obvious solution will end up doing one of two
things.  Either it will score each 5-card sub-hand of the 7 cards you
have, or it will successively search for 5-card hands for each score
type starting with straight-flush and working down to 7-high.

>> But it sounds like you're looking for something optimized for
>> speed. I don't think many ES implementation are optimized for numeric
>> calculations of that sort.
>
> Quite, but how much speed can we still tweak our of them ?

I don't know.  Let's see, shall we?

>> There are only twenty-one 5-card hands that can be made from a
>> 7-card hand, so it would be reasonable to simply do a maximum score
[quoted text clipped - 3 lines]
>
> No, and besides, I would consider *that* to be cheating.

Why is that?  Granted, these two are distinct tasks:

   (1) Find the value of the best 5-card hand that can be made from
these seven cards.
   (2) Find the best 5-card hand that can be made from these seven
cards.

But one of the most straightforward ways to do the former involves the
latter as an intermediate step.  And I think most solutions will have
an abstract version of the latter embedded in them.  I'd love to see a
purely mathematical version which doesn't do this in practice, but I'm
not holding my breath.  What sorts of systems are you imagining that
don't in some sense find a five-card hand to compare?

 -- Scott

Johannes Baagoe10 Mar 2010 05:26
Scott Sauyet :

> Johannes Baagoe :

>> Has anybody written a fast Texas hold'em hand evaluator in
>> ECMAScript?

> I never have, and would not choose ES to code one without good reason.

I have no better reason than a taste for challenges.

> If issues of speed are not that important, this would not be hard
> to code, either in the compare-two-hands version or the score-a-hand
> version.

I thought so, too, until I tried :) It is a lot trickier with 7 cards
than with 5. While my present focus is on speed, I would still love to
see a short and truly elegant solution, even if it is slow. I would
love to write one, for that matter, but I am not satisfied by any of
my efforts so far.

> But it sounds like you're looking for something optimized for
> speed. I don't think many ES implementation are optimized for numeric
> calculations of that sort.

Quite, but how much speed can we still tweak our of them ?

> There are only twenty-one 5-card hands that can be made from a
> 7-card hand, so it would be reasonable to simply do a maximum score
> on those sub-hands to make a 7-card hand evaluator out of a 5-card
> hand one. But again, this means 21 runs of the 5-card hand evaluator,
> clearly not a technique optimized for speed.

No, and besides, I would consider *that* to be cheating.

Signature

Johannes


Scott Sauyet09 Mar 2010 21:45
> Has anybody written a fast Texas hold'em hand evaluator in ECMAScript?

I never have, and would not choose ES to code one without good reason.

If issues of speed are not that important, this would not be hard to
code, either in the compare-two-hands version or the score-a-hand
version.

But it sounds like you're looking for something optimized for speed.
I don't think many ES implementation are optimized for numeric
calculations of that sort.

> (NB: this is Texas hold'em, with 7 cards. Naive assumptions from 5 card
> poker do not necessarily apply. There may be a flush and a straight,
> but no straight flush. Also, there may be both 4 of a kind and 3 of
> a kind, etc. What is wanted is the value of the best 5 cards subset.)

There are only twenty-one 5-card hands that can be made from a 7-card
hand, so it would be reasonable to simply do a maximum score on those
sub-hands to make a 7-card hand evaluator out of a 5-card hand one.
But again, this means 21 runs of the 5-card hand evaluator, clearly
not a technique optimized for speed.

 -- Scott

Johannes Baagoe09 Mar 2010 15:22
Has anybody written a fast Texas hold'em hand evaluator in ECMAScript?

What is wanted is a function, say handEval(hand), which, given a
suitable representation of a hand of 7 cards, returns a value that
accurately represents the strength of the hand. That is,
handEval(hand0) > handEval(hand1) iff hand0 beats hand1,
handEval(hand0) == handEval(hand1) iff hand0 and hand1 tie, and
handEval(hand0) < handEval(hand1) iff hand1 beats hand0.

It would be nice, too, if the return value could easily indicate what
kind of hand it is - high card, pair, two pairs, etc, preferably with
further information to break ties within each kind.

And it would be a further bonus if the evaluator could retain most
of its state when only a few cards change from one hand to another,
in order to gain time when looping through all the possible hands
with a given, fixed subset.

The problem has received a lot of attention in other languages, e.g.,
http://www.codingthewheel.com/archives/poker-hand-evaluator-roundup
But the best solutions can hardly be implemented in ECMAScript - a
123 megabyte lookup table is unpractical. On the other hand, Objects
or sparse Arrays should be reasonably fast and space efficient,
so smaller lookup tables could be fine. How fast can one get?

Any ideas?

(NB: this is Texas hold'em, with 7 cards. Naive assumptions from 5 card
poker do not necessarily apply. There may be a flush and a straight,
but no straight flush. Also, there may be both 4 of a kind and 3 of
a kind, etc. What is wanted is the value of the best 5 cards subset.)

Signature

Johannes


Quick links:

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage




©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.