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
|