]> gitweb.factorcode.org Git - factor.git/blob - extra/poker/poker.factor
Merge branch 'master' of git://github.com/william42/factor
[factor.git] / extra / poker / poker.factor
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: accessors arrays ascii assocs binary-search combinators
5 fry kernel locals math math.bitwise math.combinatorics
6 math.order math.statistics poker.arrays random sequences
7 sequences.product splitting grouping lexer strings ;
8 IN: poker
9
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
14
15 <PRIVATE
16
17 ! Bitfield Format for Card Values:
18
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
27
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
33
34 CONSTANT: CLUB     8
35 CONSTANT: DIAMOND  4
36 CONSTANT: HEART    2
37 CONSTANT: SPADE    1
38
39 CONSTANT: DEUCE  0
40 CONSTANT: TREY   1
41 CONSTANT: FOUR   2
42 CONSTANT: FIVE   3
43 CONSTANT: SIX    4
44 CONSTANT: SEVEN  5
45 CONSTANT: EIGHT  6
46 CONSTANT: NINE   7
47 CONSTANT: TEN    8
48 CONSTANT: JACK   9
49 CONSTANT: QUEEN  10
50 CONSTANT: KING   11
51 CONSTANT: ACE    12
52
53 CONSTANT: STRAIGHT_FLUSH   0
54 CONSTANT: FOUR_OF_A_KIND   1
55 CONSTANT: FULL_HOUSE       2
56 CONSTANT: FLUSH            3
57 CONSTANT: STRAIGHT         4
58 CONSTANT: THREE_OF_A_KIND  5
59 CONSTANT: TWO_PAIR         6
60 CONSTANT: ONE_PAIR         7
61 CONSTANT: HIGH_CARD        8
62
63 CONSTANT: SUIT_STR { "C" "D" "H" "S" }
64
65 CONSTANT: RANK_STR { "2" "3" "4" "5" "6" "7" "8" "9" "T" "J" "Q" "K" "A" }
66
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" }
69
70 : card-rank-prime ( rank -- n )
71     RANK_STR index { 2 3 5 7 11 13 17 19 23 29 31 37 41 } nth ;
72
73 : card-rank ( rank -- n )
74     {
75         { "2" [ DEUCE ] }
76         { "3" [ TREY  ] }
77         { "4" [ FOUR  ] }
78         { "5" [ FIVE  ] }
79         { "6" [ SIX   ] }
80         { "7" [ SEVEN ] }
81         { "8" [ EIGHT ] }
82         { "9" [ NINE  ] }
83         { "T" [ TEN   ] }
84         { "J" [ JACK  ] }
85         { "Q" [ QUEEN ] }
86         { "K" [ KING  ] }
87         { "A" [ ACE   ] }
88     } case ;
89
90 : card-suit ( suit -- n )
91     {
92         { "C" [ CLUB    ] }
93         { "D" [ DIAMOND ] }
94         { "H" [ HEART   ] }
95         { "S" [ SPADE   ] }
96     } case ;
97
98 : card-rank-bit ( rank -- n )
99     RANK_STR index 1 swap shift ;
100
101 : card-bitfield ( rank rank suit rank -- n )
102     {
103         { card-rank-bit 16 }
104         { card-suit 12 }
105         { card-rank 8 }
106         { card-rank-prime 0 }
107     } bitfield ;
108
109 :: (>ckf) ( rank suit -- n )
110     rank rank suit rank card-bitfield ;
111
112 #! Cactus Kev Format
113 GENERIC: >ckf ( string -- n )
114
115 M: string >ckf >upper 1 cut (>ckf) ;
116 M: integer >ckf ;
117
118 : parse-cards ( string -- seq )
119     " " split [ >ckf ] map ;
120
121 : flush? ( cards -- ? )
122     HEX: F000 [ bitand ] reduce 0 = not ;
123
124 : rank-bits ( cards -- q )
125     0 [ bitor ] reduce -16 shift ;
126
127 : lookup ( cards table -- value )
128     [ rank-bits ] dip nth ;
129
130 : map-product ( seq quot -- n )
131     [ 1 ] 2dip [ dip * ] curry [ swap ] prepose each ; inline
132
133 : prime-bits ( cards -- q )
134     [ HEX: FF bitand ] map-product ;
135
136 : perfect-hash-find ( q -- value )
137     #! magic to convert a hand's unique identifying bits to the
138     #! proper index for fast lookup in a table of hand values
139     HEX: E91AAA35 +
140     dup -16 shift bitxor
141     dup   8 shift w+
142     dup  -4 shift bitxor
143     [ -8 shift HEX: 1FF bitand adjustments-table nth ]
144     [ dup 2 shift w+ -19 shift ] bi
145     bitxor values-table nth ;
146
147 : hand-value ( cards -- value )
148     dup flush? [ flushes-table lookup ] [
149         dup unique5-table lookup dup 0 > [ nip ] [
150             drop prime-bits perfect-hash-find
151         ] if
152     ] if ;
153
154 : >card-rank ( card -- string )
155     -8 shift HEX: F bitand RANK_STR nth ;
156
157 : >card-suit ( card -- string )
158     {
159         { [ dup 15 bit? ] [ drop "C" ] }
160         { [ dup 14 bit? ] [ drop "D" ] }
161         { [ dup 13 bit? ] [ drop "H" ] }
162         [ drop "S" ]
163     } cond ;
164
165 : value>rank ( value -- rank )
166     {
167         { [ dup 6185 > ] [ drop HIGH_CARD ] }        ! 1277 high card
168         { [ dup 3325 > ] [ drop ONE_PAIR ] }         ! 2860 one pair
169         { [ dup 2467 > ] [ drop TWO_PAIR ] }         !  858 two pair
170         { [ dup 1609 > ] [ drop THREE_OF_A_KIND ] }  !  858 three-kind
171         { [ dup 1599 > ] [ drop STRAIGHT ] }         !   10 straights
172         { [ dup 322 > ]  [ drop FLUSH ] }            ! 1277 flushes
173         { [ dup 166 > ]  [ drop FULL_HOUSE ] }       !  156 full house
174         { [ dup 10 > ]   [ drop FOUR_OF_A_KIND ] }   !  156 four-kind
175         [ drop STRAIGHT_FLUSH ]                      !   10 straight-flushes
176     } cond ;
177
178 : card>string ( n -- string )
179     [ >card-rank ] [ >card-suit ] bi append ;
180
181 PRIVATE>
182
183 : <deck> ( -- deck )
184     RANK_STR SUIT_STR 2array
185     [ concat >ckf ] V{ } product-map-as randomize ;
186
187 : best-holdem-hand ( hand -- n cards )
188     5 [ [ hand-value ] [ ] bi ] { } map>assoc-combinations
189     infimum first2 ;
190
191 : value>string ( n -- string )
192     value>rank VALUE_STR nth ;
193
194 : hand>card-names ( hand -- string )
195     [ card>string ] map ;
196
197 : string>value ( string -- value )
198     parse-cards best-holdem-hand drop ;
199
200 ERROR: no-card card deck ;
201
202 : draw-specific-card ( card deck -- card )
203     [ >ckf ] dip
204     2dup index [ swap remove-nth! drop ] [ no-card ] if* ;
205
206 : start-hands ( seq -- seq' deck )
207     <deck> [ '[ [ _ draw-specific-card ] map ] map ] keep ;
208
209 :: holdem-hand% ( hole1 deck community n -- x )
210     community length 5 swap - 2 + :> #samples
211     n [
212         drop
213         deck #samples sample :> sampled
214         sampled 2 cut :> ( hole2 community2 )
215         hole1 community community2 3append :> hand1
216         hole2 community community2 3append :> hand2
217         hand1 hand2 [ best-holdem-hand 2array ] bi@ <=> +lt+ =
218     ] count ;
219
220 :: compare-holdem-hands ( holes deck n -- seq )
221     n [
222         holes deck 5 sample '[
223             [ _ append best-holdem-hand drop ] keep
224         ] { } map>assoc infimum second
225     ] replicate histogram ;
226
227 : (best-omaha-hand) ( seq -- pair )
228     4 cut
229     [ 2 all-combinations ] [ 3 all-combinations ] bi*
230     2array [ concat [ best-holdem-hand drop ] keep ] { } product-map>assoc ;
231
232 : best-omaha-hand ( seq -- n cards ) (best-omaha-hand) infimum first2 ;
233
234 :: compare-omaha-hands ( holes deck n -- seq )
235     n [
236         holes deck 5 sample '[
237             [ _ append best-omaha-hand drop ] keep
238         ] { } map>assoc infimum second
239     ] replicate histogram ;
240
241 ERROR: bad-suit-symbol ch ;
242
243 : symbol>suit ( ch -- ch' )
244     ch>upper
245     H{
246         { CHAR: ♠ CHAR: S }
247         { CHAR: ♦ CHAR: D }
248         { CHAR: ♥ CHAR: H }
249         { CHAR: ♣ CHAR: C }
250         { CHAR: S CHAR: S }
251         { CHAR: D CHAR: D }
252         { CHAR: H CHAR: H }
253         { CHAR: C CHAR: C }
254     } ?at [ bad-suit-symbol ] unless ;
255
256 : card> ( string -- card )
257     1 over [ symbol>suit ] change-nth >ckf ;
258
259 : value>hand-name ( value -- string )
260     value>rank VALUE_STR nth ;
261
262 : string>hand-name ( string -- string' )
263     string>value value>hand-name ;
264
265 SYNTAX: HAND{
266     "}" parse-tokens [ card> ] { } map-as suffix! ;