date: 01 Oct, 2009,
actionscript: **3 FP10**,
complexity: ,
feedback (votes):

- Frankenstein programming
- Leaving the comfort zone
- Let's nuke the planet and fix problems
- Having a pee on a plant
- MetaLiquid (first step)
- One more cocaine story
- Taking an Adobe ACE Exam
- My hometown Cali, Colombia
- The eighth wonder (Oitava maravilha)

- Acoustic recording and play back
- From Fruity Loops to Actionscript
- The word is 'interpolation'
- The mystery of parallel lines
- Elastic Collisions Inspiration...
- The verlet integration!

- All you may need about maths
- Polygonal, Interesting flash articles
- Mike Chambers, Cool blog and news
- Andre Michelle Laboratory
- Joa Ebert - Developer
- Cool ActionScript 3 Tutorials
- BIT-101 ActionScript 3 Contents
- A good actionscript coder GSkinner

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).

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.

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.

A typeo, i meant to write "or it should be 1/8 if it counts the probability of another pair happening."

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.

How far with the 7 card evaluator, I am currently working on a poker game currently stuck on this part

date: 05 Dec 2011 - 00:00:00, published by alex.nino