Poker evaluator

Few months ago I've been working in a project where I had to validate poker hands (based on 5 cards) and also calculate what the chance is for all possible hands depending on their rank from highest card to straight flush and then show percentages for each possible future hand type.

Well, it looked to me a bit simple before starting but once I took a quick look on the web and I started building up the logic of it, I just realized that it is something really, really complicated. A poker hand of 5 cards can have lots of different combinations which mean that it might need lots of validations and conditions depending on each hand rank (I'm not speaking about few, it may be more than hundreds).

Bitwise operators

How many times have you heard about bitwise operators and people saying that they are only useful in very few scenarios, and also that you may use them never? This project is a good example to demonstrate how powerful they are in terms of performance but also (the most important thing) to clarify that the biggest problem people have is that they always try to use bitwise operators on top of the logic they already have done which is completely wrong, it needs to be the other way round, we must build our logic based on these bitwise operators from the very beginning and then the source code of our program will look completely different of what it should be normally (different logic), it is true it might be a bit harder to read later on and It may contents some lookup tables but it will run far much FASTER and that is what we need in some cases.

The algorithm

There was a guy in America called Kevin Suffecool who made the first step, he's approach was quite simple (and powerful), basically he has capsulated everything related with a deck's card in a 32 bits number (in action script a uint) and then generate some lookup tables for making all validations and find out the hand rank based on 5 cards. His program was originally made in C++ and I just grabbed it and translate it to Action Script 3, it took me a while but here it is, when I run it for the first time it took only 125ms to calculate all possible hands combinations using 5 cards of a deck, a total of 2,598,960 hands! take a look in wikipedia to understand more what I'm talking about, here is the link poker probability. This was definitely the very first time I saw an algorithm running that fast in action script and then my first feeling was that Adobe has definitely done some improvements on it (The flash player running AS3 rocks!), something like this in AS2 won't work.

This algorithm use some pre-generated numbers (lookup tables) that were generated according with the program logic, if you want to take a look on one of these tables see this file, but do not spend too much on it (look at the logic how it was generated), it is completely pointless to understand these numbers or try to figure out if there is any possible pattern on them, the last time I took a look at that table was to demonstrate to my wife how busy I was in that particular moment :)

Here it is working in Flash... have a go! just press EVALUATE to see the lighting shines.

The sourcecode is attached in this article however you may find an improved version (latest version) at yoambulante.com download sourcecode - feel free to ask any question about it. I'm currently working on a 7 card evaluator based on same approach, it is not being easy... it has lots of loops and validations.