Hi,
I previously posted on this list to get some help regarding evaluation
of poker hands. I tried this first in Python, but it turned out to be
too slow. I therefore decided to give Lisp a try after getting some
excellent feedback. I have now (with the help of feedback from
comp.lang.lisp) written some poker code that sort of works, but since
this is my first attempt at Lisp I'm afraid the implementation is rather
crude. I'm hoping to get some input on the code style, and perhaps also
some hints on efficiency. (I'm just doing this for fun by the way).
The overall idea is to reduce a poker hand to a single number. This is
called a Ness enumeration. This approach is explained quite well here:
http://mywebpages.comcast.net/dness/poker.htm
Given this information I hope the code is self-explanatory.
There are some things in the code I like less than others. The function
eval-hand is the heart of the algorithm. Once the characteristics vector
of the hand is known, I would like to return it immediately. The
handling of straigts is inelegant and probably very inefficient as well.
I don't yet handle the case where straights are five-high properly either.
Here are a couple of examples of running the code:
CL-USER> (eval-hand (mapcar #' card '((0 0) (1 0) (2 0) (3 0) (4 0))))
(9 4 3 2 1 0)
This hand represents a straight flush of spades, six high. (2, 3, 4, 5,
and 6 of spades). The return value is the so called characteristics
vector of the hand. The leading 9 means that the hand is a
straight-flush. (the value of a flush + the value of a straight). Then
the denominations of the cards follow in decreasing order.
CL-USER> (eval-hand (mapcar #' card '((1 1) (1 0) (1 2) (3 0) (4 0))))
(3 1 4 3 12 11)
This one is a three of a kind. There are 3 1's (or rather 3's), then a 6
and 5. The 12 and 11 at the end are filler. We need the filler because
this characteristics vector will be reduced to a base 13 number with the
values of the vector representing the digits, in order to effectively
compare hands.
If you have a fast computer you can try to count all the possible hands
and list the number in each category. I would not try this unless you
can compile the code first. This is from SBCL:
* (load "poker-eval.lisp")
T
* (count-hands)
1302540
1098240
123552
54912
10200
5108
3744
624
40
--
Frode, SIM
"Any fool can write code that a computer can understand.
Good programmers write code that humans can understand"
Frode �ijord <·····@sim.no> writes:
> Here is the code by the way :)
Didn't look much at it, but the use of string= appears perverse -- is there a
reason why you don't just use EQUAL and lists?
Another thing, by posting the code after a "--\n" you make it awkward to quote
it since many clients will not quote sigs.
'as
Alexander Schmolck wrote:
> Frode �ijord <·····@sim.no> writes:
>
>> Here is the code by the way :)
>
> Didn't look much at it, but the use of string= appears perverse -- is there a
> reason why you don't just use EQUAL and lists?
Actually, when I look at it now, I don't really understand why I thought
it was a good idea to make strings and then compare them, instead of
comparing lists directly. So no, there is no (good) reason for this :)
Frode �ijord
Alexander Schmolck wrote:
> Frode �ijord <·····@sim.no> writes:
>
>> Here is the code by the way :)
>
> Didn't look much at it, but the use of string= appears perverse -- is there a
> reason why you don't just use EQUAL and lists?
Actually, when I look at it now, I don't really understand why I thought
it was a good idea to make strings and then compare them, instead of
comparing lists directly. So no, there is no (good) reason for this :)
Frode �ijord
Joel Reymont wrote:
> Frode,
>
> darcs get http://wagerlabs.com/cl-openpoker
I looked at cards.lisp, and it seems the hand evaluation code is much
more complicated than it has to be. Before I started writing the
evaluation code I spent some time researching how to best evaluate poker
hands. The best way to do this is by Ness Enumeration, which is
basically a mapping from the set of comb(52,5) hands into the integers
[0...2598959]. It is very convenient to be able to define a hand by a
single number when comparing hands or computing some statistics :)
Here's a link: http://mywebpages.comcast.net/dness/poker.htm
Frode �ijord
On Wed, 12 Apr 2006 01:59:24 +0200, <·····@sim.no> wrote:
> Joel Reymont wrote:
>> darcs get http://wagerlabs.com/cl-openpoker
>
> I looked at cards.lisp, and it seems the hand evaluation code is much
> more complicated than it has to be.
> evaluation code I spent some time researching how to best evaluate poker
> hands. The best way to do this is by Ness Enumeration, which is
> basically a mapping from the set of comb(52,5) hands into the integers
> [0...2598959]. It is very convenient to be able to define a hand by a
> single number when comparing hands or computing some statistics :)
I agree that a simplification to a single number is rather valuable.
Since you have a lot of familiarity with the technique, can I inquire
as to the nature of the number mapping? Is it such that numbers in
the neighborhood of a hand, say 124424, relate to hands in the
neighborhood of the actual cards, i.e. do full houses with the same
triple map to nearby numbers?
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
GP lisper wrote:
> On Wed, 12 Apr 2006 01:59:24 +0200, <·····@sim.no> wrote:
>> Joel Reymont wrote:
>>> darcs get http://wagerlabs.com/cl-openpoker
>> I looked at cards.lisp, and it seems the hand evaluation code is much
>> more complicated than it has to be.
>
>> evaluation code I spent some time researching how to best evaluate poker
>> hands. The best way to do this is by Ness Enumeration, which is
>> basically a mapping from the set of comb(52,5) hands into the integers
>> [0...2598959]. It is very convenient to be able to define a hand by a
>> single number when comparing hands or computing some statistics :)
>
> I agree that a simplification to a single number is rather valuable.
> Since you have a lot of familiarity with the technique, can I inquire
> as to the nature of the number mapping? Is it such that numbers in
> the neighborhood of a hand, say 124424, relate to hands in the
> neighborhood of the actual cards, i.e. do full houses with the same
> triple map to nearby numbers?
I guess that would depend on what you mean with nearby. It is always the
case that a better hand maps to a higher number, and if a hand is better
than another, its number will be higher. If the numbers are equal, the
hands are equal. The mapping is not dense though, so if what you are
asking is if the highest full house and the next highest full house are
represented by consecutive numbers the answer is no.
Frode �ijord