Speed up best-hands a bit using reduce and add a test
parent
d9e4f6e9cb
commit
f465a013d7
|
@ -116,3 +116,10 @@ PRIVATE>
|
|||
[ <combo> [ choose [0,b) ] keep ] dip
|
||||
'[ _ apply-combination @ ] each ; inline
|
||||
|
||||
: map-combinations ( seq k quot -- )
|
||||
[ <combo> [ choose [0,b) ] keep ] dip
|
||||
'[ _ apply-combination @ ] map ; inline
|
||||
|
||||
: reduce-combinations ( seq k identity quot -- result )
|
||||
[ -rot ] dip each-combination ; inline
|
||||
|
||||
|
|
|
@ -26,3 +26,5 @@ IN: poker.tests
|
|||
|
||||
[ t ] [ "7C 5D 4H 3S 2C" "7D 5D 4D 3C 2S" [ <hand> ] bi@ = ] unit-test
|
||||
[ f ] [ "7C 5D 4H 3S 2C" "7D 5D 4D 3C 2S" [ <hand> ] bi@ eq? ] unit-test
|
||||
|
||||
[ 190 ] [ "AS KD JC KH 2D 2S KH" best-hand value>> ] unit-test
|
||||
|
|
|
@ -179,7 +179,7 @@ PRIVATE>
|
|||
|
||||
TUPLE: hand
|
||||
{ cards sequence }
|
||||
{ value integer } ;
|
||||
{ value integer initial: 9999 } ;
|
||||
|
||||
M: hand <=> [ value>> ] compare ;
|
||||
M: hand equal?
|
||||
|
@ -189,8 +189,8 @@ M: hand equal?
|
|||
parse-cards dup hand-value hand boa ;
|
||||
|
||||
: best-hand ( str -- hand )
|
||||
parse-cards 5 all-combinations
|
||||
[ dup hand-value hand boa ] map infimum ;
|
||||
parse-cards 5 hand new
|
||||
[ dup hand-value hand boa min ] reduce-combinations ;
|
||||
|
||||
: >cards ( hand -- str )
|
||||
cards>> [ card>string ] map " " join ;
|
||||
|
|
Loading…
Reference in New Issue