1 ! Copyright (c) 2009 Aaron Schaefer, Doug Coleman. 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: arrays ascii assocs combinators fry kernel lexer locals
5 math math.bitwise math.combinatorics math.order math.statistics
6 poker.arrays random sequences sequences.extras
7 sequences.product splitting strings ;
10 ! The algorithm used is based on Cactus Kev's Poker Hand Evaluator with
11 ! the Senzee Perfect Hash Optimization:
12 ! http://www.suffecool.net/poker/evaluator.html
13 ! http://www.senzee5.com/2006/06/some-perfect-hash.html
17 ! Bitfield Format for Card Values:
19 ! +-------------------------------------+
20 ! | xxxbbbbb bbbbbbbb ssssrrrr xxpppppp |
21 ! +-------------------------------------+
22 ! xxxAKQJT 98765432 CDHSrrrr xxpppppp
23 ! +-------------------------------------+
24 ! | 00001000 00000000 01001011 00100101 | King of Diamonds
25 ! | 00000000 00001000 00010011 00000111 | Five of Spades
26 ! | 00000010 00000000 10001001 00011101 | Jack of Clubs
28 ! p = prime number value of rank (deuce = 2, trey = 3, four = 5, ..., ace = 41)
29 ! r = rank of card (deuce = 0, trey = 1, four = 2, ..., ace = 12)
30 ! s = bit turned on depending on suit of card
31 ! b = bit turned on depending on rank of card
32 ! x = bit turned off, not used
53 CONSTANT: STRAIGHT_FLUSH 0
54 CONSTANT: FOUR_OF_A_KIND 1
55 CONSTANT: FULL_HOUSE 2
58 CONSTANT: THREE_OF_A_KIND 5
63 CONSTANT: SUIT_STR { "C" "D" "H" "S" }
65 CONSTANT: RANK_STR { "2" "3" "4" "5" "6" "7" "8" "9" "T" "J" "Q" "K" "A" }
67 CONSTANT: VALUE_STR { "Straight Flush" "Four of a Kind" "Full House" "Flush"
68 "Straight" "Three of a Kind" "Two Pair" "One Pair" "High Card" }
70 : card-rank-prime ( rank -- n )
71 RANK_STR index { 2 3 5 7 11 13 17 19 23 29 31 37 41 } nth ;
73 : card-rank ( rank -- n )
90 : card-suit ( suit -- n )
98 : card-rank-bit ( rank -- n )
99 RANK_STR index 1 swap shift ;
101 : card-bitfield ( rank rank suit rank -- n )
106 { card-rank-prime 0 }
109 :: (>ckf) ( rank suit -- n )
110 rank rank suit rank card-bitfield ;
113 GENERIC: >ckf ( string -- n )
115 M: string >ckf >upper 1 cut (>ckf) ;
118 : parse-cards ( string -- seq )
119 " " split [ >ckf ] map ;
121 : flush? ( cards -- ? )
122 0xF000 [ bitand ] reduce 0 = not ;
124 : rank-bits ( cards -- q )
125 0 [ bitor ] reduce -16 shift ;
127 : lookup ( cards table -- value )
128 [ rank-bits ] dip nth ;
130 : prime-bits ( cards -- q )
131 [ 0xFF 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 0x1FF 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 -- string )
152 -8 shift 0xF bitand RANK_STR nth ;
154 : >card-suit ( card -- string )
156 { [ dup 15 bit? ] [ drop "C" ] }
157 { [ dup 14 bit? ] [ drop "D" ] }
158 { [ dup 13 bit? ] [ drop "H" ] }
162 : value>rank ( value -- 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 ( n -- string )
176 [ >card-rank ] [ >card-suit ] bi append ;
181 RANK_STR SUIT_STR 2array
182 [ concat >ckf ] V{ } product-map-as randomize ;
184 : best-holdem-hand ( hand -- n cards )
185 5 [ [ hand-value ] [ ] bi ] { } map>assoc-combinations
188 : value>string ( n -- string )
189 value>rank VALUE_STR nth ;
191 : hand>card-names ( hand -- string )
192 [ card>string ] map ;
194 : string>value ( string -- value )
195 parse-cards best-holdem-hand drop ;
197 ERROR: no-card card deck ;
199 : draw-specific-card ( card deck -- card )
201 2dup index [ swap remove-nth! drop ] [ no-card ] if* ;
203 : start-hands ( seq -- seq' deck )
204 <deck> [ '[ [ _ draw-specific-card ] map ] map ] keep ;
206 :: holdem-hand% ( hole1 deck community n -- x )
207 community length 5 swap - 2 + :> #samples
210 deck #samples sample :> sampled
211 sampled 2 cut :> ( hole2 community2 )
212 hole1 community community2 3append :> hand1
213 hole2 community community2 3append :> hand2
214 hand1 hand2 [ best-holdem-hand 2array ] compare +lt+ =
217 :: compare-holdem-hands ( holes deck n -- seq )
219 holes deck 5 sample '[
220 [ _ append best-holdem-hand drop ] keep
221 ] { } map>assoc infimum second
222 ] replicate histogram ;
224 : (best-omaha-hand) ( seq -- pair )
226 [ 2 all-combinations ] [ 3 all-combinations ] bi*
227 2array [ concat [ best-holdem-hand drop ] keep ] { } product-map>assoc ;
229 : best-omaha-hand ( seq -- n cards ) (best-omaha-hand) infimum first2 ;
231 :: compare-omaha-hands ( holes deck n -- seq )
233 holes deck 5 sample '[
234 [ _ append best-omaha-hand drop ] keep
235 ] { } map>assoc infimum second
236 ] replicate histogram ;
238 ERROR: bad-suit-symbol ch ;
240 : symbol>suit ( ch -- ch' )
251 } ?at [ bad-suit-symbol ] unless ;
253 : card> ( string -- card )
254 1 over [ symbol>suit ] change-nth >ckf ;
256 : value>hand-name ( value -- string )
257 value>rank VALUE_STR nth ;
259 : string>hand-name ( string -- string' )
260 string>value value>hand-name ;
263 "}" [ card> ] map-tokens suffix! ;