poker card game revisited (code included)
flupke
flupke at nonexistingdomain.com
Thu Jun 16 16:44:03 EDT 2005
John Hazen wrote:
> [Erik Max Francis]
>
>>>>Searching for straights and flushes is much better done by masks.
>
>
> Interesting. I've been thinking about playing with this stuff too, but
> hadn't considered masks. So each card has a representation:
>
> n bits for rank, then m bits for suit.
>
> 00000000000010 0001 = 2 clubs
> 01000000000000 1000 = K spades
> ...
>
> [flupke]
>
>>>As for straights, if i understand correctly, you make all possible
>>>straights of the cards in the hand and then see if one matches?
>
>
> Yeah, I originally thought that's what you meant, too. But if you take
> the scheme above, and sort the cards before you merge them into the
> hand-bits aggregate, then you can just have *one* mask for straights,
> and shift it down by a bit each time you check. So the best straight
> mask (A high) would be (ignoring suit bits):
>
> 10000000000000 01000000000000 00100000000000 00010000000000 00001000000000
>
> Then this could be shifted right for a K-high straight:
>
> 01000000000000 00100000000000 00010000000000 00001000000000 00000100000000
>
> I guess that means that an Ace has to have the following representation,
> since it can be both at the high and low end of a straight:
>
> 10000000000001 0100 = A hearts
>
> But this may mess with bit-counting shortcuts for other calculations...
>
> This is interesting to think about. Thanks for the pointer. I'm also
> going to look at pokersource, though I may put that off until I at least
> partially re-invent the wheel for learning purposes.
>
> -John
I haven't had to much time to play around with it either and i agree
it's fascinating. However my code now detects high hands properly (need
to do low hands too and check the wildcards some more. I really need to
build a test suit) and i find it quite readable code.
Why would you want to start messing with bits in python? It feels like a
pure c++ approach. Check the way the straight is checked in the code
posted here. It's very readable and i doubt you can make it as readable
using bitmasks.
I do think that bitmasks are going to be faster but i could program the
hand checking stuff fairly quick something that would require a lot more
work when doing the same with bitmasks. Anyway, i'm still interested in
that approach and i will try it whenever i've got the time.
Haven't seen a lot of python code that uses bitmasks though. Maybe the
pokersource C/C++ code can be wrapped in python? :)
Regards,
Benedict
More information about the Python-list
mailing list