1 ! Copyright (c) 2009 Aaron Schaefer. All rights reserved.
2 ! The contents of this file are licensed under the Simplified BSD License
3 ! A copy of the license is available at http://factorcode.org/license.txt
4 USING: accessors arrays ascii binary-search combinators kernel locals math
5 math.bitwise math.order poker.arrays random sequences sequences.product
9 ! The algorithm used is based on Cactus Kev's Poker Hand Evaluator with
10 ! the Senzee Perfect Hash Optimization:
11 ! http://www.suffecool.net/poker/evaluator.html
12 ! http://www.senzee5.com/2006/06/some-perfect-hash.html
16 ! Bitfield Format for Card Values:
18 ! +-------------------------------------+
19 ! | xxxbbbbb bbbbbbbb ssssrrrr xxpppppp |
20 ! +-------------------------------------+
21 ! xxxAKQJT 98765432 CDHSrrrr xxpppppp
22 ! +-------------------------------------+
23 ! | 00001000 00000000 01001011 00100101 | King of Diamonds
24 ! | 00000000 00001000 00010011 00000111 | Five of Spades
25 ! | 00000010 00000000 10001001 00011101 | Jack of Clubs
27 ! p = prime number value of rank (deuce = 2, trey = 3, four = 5, ..., ace = 41)
28 ! r = rank of card (deuce = 0, trey = 1, four = 2, ..., ace = 12)
29 ! s = bit turned on depending on suit of card
30 ! b = bit turned on depending on rank of card
31 ! x = bit turned off, not used
52 CONSTANT: STRAIGHT_FLUSH 1
53 CONSTANT: FOUR_OF_A_KIND 2
54 CONSTANT: FULL_HOUSE 3
57 CONSTANT: THREE_OF_A_KIND 6
62 CONSTANT: SUIT_STR { "C" "D" "H" "S" }
64 CONSTANT: RANK_STR { "2" "3" "4" "5" "6" "7" "8" "9" "T" "J" "Q" "K" "A" }
66 CONSTANT: VALUE_STR { "" "Straight Flush" "Four of a Kind" "Full House" "Flush"
67 "Straight" "Three of a Kind" "Two Pair" "One Pair" "High Card" }
69 : card-rank-prime ( rank -- n )
70 RANK_STR index { 2 3 5 7 11 13 17 19 23 29 31 37 41 } nth ;
72 : card-rank ( rank -- n )
89 : card-suit ( suit -- n )
97 : card-rank-bit ( rank -- n )
98 RANK_STR index 1 swap shift ;
100 : card-bitfield ( rank rank suit rank -- n )
105 { card-rank-prime 0 }
108 :: (>ckf) ( rank suit -- n )
109 rank rank suit rank card-bitfield ;
113 >upper 1 cut (>ckf) ;
115 : parse-cards ( str -- seq )
116 " " split [ >ckf ] map ;
118 : flush? ( cards -- ? )
119 HEX: F000 [ bitand ] reduce 0 = not ;
121 : rank-bits ( cards -- q )
122 0 [ bitor ] reduce -16 shift ;
124 : lookup ( cards table -- value )
125 [ rank-bits ] dip nth ;
127 : map-product ( seq quot -- n )
128 [ 1 ] 2dip [ dip * ] curry [ swap ] prepose each ; inline
130 : prime-bits ( cards -- q )
131 [ HEX: FF bitand ] map-product ;
133 : perfect-hash-find ( q -- value )
134 #! magic to convert a hand's unique identifying bits to the
135 #! proper index for fast lookup in a table of hand values
140 [ -8 shift HEX: 1FF bitand adjustments-table nth ]
141 [ dup 2 shift w+ -19 shift ] bi
142 bitxor values-table nth ;
144 : hand-value ( cards -- value )
145 dup flush? [ flushes-table lookup ] [
146 dup unique5-table lookup dup 0 > [ nip ] [
147 drop prime-bits perfect-hash-find
151 : >card-rank ( card -- str )
152 -8 shift HEX: F bitand RANK_STR nth ;
154 : >card-suit ( card -- str )
156 { [ dup 15 bit? ] [ drop "C" ] }
157 { [ dup 14 bit? ] [ drop "D" ] }
158 { [ dup 13 bit? ] [ drop "H" ] }
162 : hand-rank ( hand -- rank )
164 { [ dup 6185 > ] [ drop HIGH_CARD ] } ! 1277 high card
165 { [ dup 3325 > ] [ drop ONE_PAIR ] } ! 2860 one pair
166 { [ dup 2467 > ] [ drop TWO_PAIR ] } ! 858 two pair
167 { [ dup 1609 > ] [ drop THREE_OF_A_KIND ] } ! 858 three-kind
168 { [ dup 1599 > ] [ drop STRAIGHT ] } ! 10 straights
169 { [ dup 322 > ] [ drop FLUSH ] } ! 1277 flushes
170 { [ dup 166 > ] [ drop FULL_HOUSE ] } ! 156 full house
171 { [ dup 10 > ] [ drop FOUR_OF_A_KIND ] } ! 156 four-kind
172 [ drop STRAIGHT_FLUSH ] ! 10 straight-flushes
175 : card>string ( card -- str )
176 [ >card-rank ] [ >card-suit ] bi append ;
184 M: hand <=> [ value>> ] compare ;
186 over hand? [ [ value>> ] bi@ = ] [ 2drop f ] if ;
188 : <hand> ( str -- hand )
189 parse-cards dup hand-value hand boa ;
191 : >cards ( hand -- str )
192 cards>> [ card>string ] map " " join ;
194 : >value ( hand -- str )
195 hand-rank VALUE_STR nth ;
201 RANK_STR SUIT_STR 2array [ concat >ckf ] product-map deck boa ;
203 : shuffle ( deck -- deck )
204 [ randomize ] change-cards ;