The fastest poker hands evaluator ever

date: 01 Oct, 2009, actionscript: 3 FP10, complexity: complexity level 4.09 of 5complexity level 4.09 of 5complexity level 4.09 of 5complexity level 4.09 of 5complexity level 4.09 of 5, feedback (votes): users score 4.98 of 5 (287 votes)users score 4.98 of 5 (287 votes)users score 4.98 of 5 (287 votes)users score 4.98 of 5 (287 votes)users score 4.98 of 5 (287 votes)

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 actionscript 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 Actionscript 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 actionscript 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.

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.

date: 05 Dec 2011 - 00:00:00, published by alex.nino
Hi! thanks for your feedback, it is all fine, it is just the way how it shows the results, it counts up how many hands you got in total and gives you results based on occurrences, it is not a percentage prabability (which is easy to find out with this data), so in the case of two pairs what it is saying is that if you have an extra King you will get tree of a kind, and so on... so that's why is not 100% of all hands you got a pair, you can get more than a pair! i hope it clarifies.
date: 04 Dec 2011 - 15:02:48, published by Mariosti
A typeo, i meant to write "or it should be 1/8 if it counts the probability of another pair happening."
date: 04 Dec 2011 - 15:00:49, published by Mariosti
Just checking i set up kc,jc,qc and ks, and there's clearly a mistake in the result 8, that is: "8 One pair, possible: 40 hands, percentage: 83.33333333333334%" It should either be 100% because we already have one pair, or it should be 1/6 if it counts the probability of another pair happening.
date: 10 Jan 2011 - 12:11:36, published by Iron
How far with the 7 card evaluator, I am currently working on a poker game currently stuck on this part