1 ! Copyright (c) 2009 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors ascii binary-search combinators kernel locals math
4 math.bitwise math.order poker.arrays sequences splitting ;
7 ! The algorithm used is based on Cactus Kev's Poker Hand Evaluator:
8 ! http://www.suffecool.net/poker/evaluator.html
12 ! Bitfield Format for Card Values:
14 ! +-------------------------------------+
15 ! | xxxbbbbb bbbbbbbb ssssrrrr xxpppppp |
16 ! +-------------------------------------+
17 ! xxxAKQJT 98765432 CDHSrrrr xxpppppp
18 ! +-------------------------------------+
19 ! | 00001000 00000000 01001011 00100101 | King of Diamonds
20 ! | 00000000 00001000 00010011 00000111 | Five of Spades
21 ! | 00000010 00000000 10001001 00011101 | Jack of Clubs
23 ! p = prime number value of rank (deuce = 2, trey = 3, four = 5, ..., ace = 41)
24 ! r = rank of card (deuce = 0, trey = 1, four = 2, ..., ace = 12)
25 ! s = bit turned on depending on suit of card
26 ! b = bit turned on depending on rank of card
27 ! x = bit turned off, not used
48 CONSTANT: STRAIGHT_FLUSH 1
49 CONSTANT: FOUR_OF_A_KIND 2
50 CONSTANT: FULL_HOUSE 3
53 CONSTANT: THREE_OF_A_KIND 6
58 CONSTANT: RANK_STR { "2" "3" "4" "5" "6" "7" "8" "9" "T" "J" "Q" "K" "A" }
60 CONSTANT: VALUE_STR { "" "Straight Flush" "Four of a Kind" "Full House" "Flush"
61 "Straight" "Three of a Kind" "Two Pair" "One Pair" "High Card" }
63 : card-rank-prime ( rank -- n )
64 RANK_STR index { 2 3 5 7 11 13 17 19 23 29 31 37 41 } nth ;
66 : card-rank ( rank -- n )
83 : card-suit ( suit -- n )
91 : card-rank-bit ( rank -- n )
92 RANK_STR index 1 swap shift ;
94 : card-bitfield ( rank rank suit rank -- n )
102 :: (>ckf) ( rank suit -- n )
103 rank rank suit rank card-bitfield ;
107 >upper 1 cut (>ckf) ;
109 : flush? ( cards -- ? )
110 HEX: F000 [ bitand ] reduce 0 = not ;
112 : rank-bits ( cards -- q )
113 0 [ bitor ] reduce -16 shift ;
115 : lookup ( cards table -- value )
116 [ rank-bits ] dip nth ;
118 : unique5? ( cards -- ? )
119 unique5-table lookup 0 > ;
121 : map-product ( seq quot -- n )
122 [ 1 ] 2dip [ dip * ] curry [ swap ] prepose each ; inline
124 : prime-bits ( cards -- q )
125 [ HEX: FF bitand ] map-product ;
127 : hand-value ( cards -- value )
129 { [ dup flush? ] [ flushes-table lookup ] }
130 { [ dup unique5? ] [ unique5-table lookup ] }
132 prime-bits products-table sorted-index
137 : >card-rank ( card -- str )
138 -8 shift HEX: F bitand RANK_STR nth ;
140 : >card-suit ( card -- str )
142 { [ dup 15 bit? ] [ drop "C" ] }
143 { [ dup 14 bit? ] [ drop "D" ] }
144 { [ dup 13 bit? ] [ drop "H" ] }
154 M: hand <=> [ value>> ] compare ;
156 over hand? [ [ value>> ] bi@ = ] [ 2drop f ] if ;
158 : <hand> ( str -- hand )
159 " " split [ >ckf ] map
160 dup hand-value hand boa ;
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 : >value ( hand -- str )
176 hand-rank VALUE_STR nth ;
178 : >cards ( hand -- str )
180 [ >card-rank ] [ >card-suit ] bi append