Ross Driedger
Software Developer
London, Ontario, Canadaross (at) earz (dot) ca
Poker Hand Evaluation
This project is a version of the Poker Hand Evaluator that I use as a project for C++ programming students. The full version of this is a command line program, but this is a JavaScript representation of what the original does. This version is not interactive. It deals each players hand, and any community cards if required by the rules of the specific game. It then finds the best combination of cards for each player, then compares all players' best hands to find the winning hand or hands.
The user can choose between:
- 5 Card Draw
- Texas Hold 'em
- 7 Card Stud
- Omaha High
There is an option for which hand evaluator to use. The first evaluator is a basic engine that compares sets of five cards, checking to see if on is a Straight Flush (Royal Flush being a special case), then, if not, Four of a Kind, if not, a Full House and so on until the rank of the hands have been identified. This evaluator depends on the five cards being sorted in pip order; I call it the Sorted Evaluator.
The next kind of evaluator is based on some number theory. Any positive integer has one and only one combination of prime numbers that are that number's factor, sometimes called prime factorization. Each possible pip value is associated with a unique prime number:
Pip Value | Hash Value |
---|---|
"2" | 2 |
"3" | 3 |
"4" | 5 |
"5" | 7 |
"6" | 11 |
"7" | 13 |
"8" | 17 |
"9" | 19 |
"10" | 23 |
"Jack" | 29 |
"Queen" | 31 |
"King" | 37 |
"Ace" | 41 |
Flush | 43 |
If a hand is a flush, the prime value for that is included. All numbers are multiplied together to find a hash value for that particular hand. For example, the H2, D3, H4, S5 and C6 would hash to: 2 * 3 * 5 * 7 * 11 = 2310. The S2, S5, S7, SQ, SA would evaluate to 2 * 7 * 13 * 31 * 41 * 43 (for the flush) = 9946846. To compare two hands, all the engine does is compare the ranks of the two hands' hash values.
The trick to this engine is to pre-build the tables based on the possible poker hands.