poker card game revisited (code included)

flupke flupke at nonexistingdomain.com
Tue Jun 7 17:07:58 EDT 2005


Erik Max Francis wrote:
> flupke wrote:
> 
<snip>

First of all, my apologies for the double posts. I can only see this 
reply and can't see my original messages. I posted the message from home 
and work and they didn't show up. We use the same isp at home and at 
work so it's probably a problem on their end.

> It looks like you're not approaching this in a systematic manner. 
> Algorithms for determining poker hands are already pretty well-known; 
> there are several open source projects that do it efficiently which you 
> could learn from.

Which projects are you talking about? I only found a library in c to 
evaluat ranks but i didn't find the code to be very understandable.

> When you're evaluating poker hands, you're looking for three basic types 
> of card groups:  matches, straights, and flushes.  The most efficient 
> way to look for matches is with a histogram based on the card rank.  Bin 
> the cards up by rank, and then build a second "histogram" of the counts 
> of those ranks indexing into a list of the ranks which had those cards 
> (sorted by rank, so you can pull off the highest ones).  Now determining 
> all the rank-based hands is easy:  quads have a hit with four matches, a 
> boat has a hit with two three matches (there's no "two trips" so this is 
> a boat at best) or both three and two matches, two pair has two hits 
> with two matches, etc.

With histogram do you mean something like this:
Card hand: 2 clubs, 3 diamonds, 10 of diamonds, 4 of hearts,  3 of hearts

Histogram 1: list [2,3,4,10]
                    2 --------------------- 14	
Histogram 2: list [1,2,1,0,0,0,0,0,1,0,0,0,0]
or
list [1,2,1,1]
so index 0 is count of rank at index 0 of Histogram 1
index 1 is count of rank at index 1 of Histogram 1

> Searching for straights and flushes is much better done by masks.
> Arrange all the possible cards in a huge masks based on both cards and 
> ranks (easy to do in Python with the long type) and then build up rank 
> masks that build up all possible straight combinations (sorted by the 
> highest rank so you can search them in order), and then build up card 
> mask ranks for each suit.  For straights, just iterate through the 
> straight rank masks and see if you find one that's fully occupied; if 
> you do, that's a straight.  For flushes, just iterate through the card 
> suit masks and see if you find one that has more five or more unique 
> cards in it.  You need to iterate through all four and find the one with 
> the highest value (for games with lots of cards, you could have two 
> flushes; you want to count the highest one).  Finally, to look for 
> straight flushes, first apply the suit masks and then turn it into ranks 
> and apply the straight masks.

As for straights, if i understand correctly, you make all possible 
straights of the cards in the hand and then see if one matches?

> Then will all this information you can easily arrange the code to select 
> the best existing hand.  Note that these all use well-established 
> techniques and reveal nothing secret.

Well, it's all new to me :)

Regards,
Benedict



More information about the Python-list mailing list