Paste: ugly 7 hand evaluator

Author: doublec
Mode: factor
Date: Wed, 15 Apr 2009 00:35:21
Plain Text |
CONSTANT: perm7
{
  { 0  1  2  3  4 }
  { 0  1  2  3  5 }
  { 0  1  2  3  6 }
  { 0  1  2  4  5 }
  { 0  1  2  4  6 }
  { 0  1  2  5  6 }
  { 0  1  3  4  5 }
  { 0  1  3  4  6 }
  { 0  1  3  5  6 }
  { 0  1  4  5  6 }
  { 0  2  3  4  5 }
  { 0  2  3  4  6 }
  { 0  2  3  5  6 }
  { 0  2  4  5  6 }
  { 0  3  4  5  6 }
  { 1  2  3  4  5 }
  { 1  2  3  4  6 }
  { 1  2  3  5  6 }
  { 1  2  4  5  6 }
  { 1  3  4  5  6 }
  { 2  3  4  5  6 }
}

:: make-subhand ( i hand -- seq )
  [
    5 [
      i perm7 nth nth hand nth ,
    ] each
  ] { } make ;

:: (eval-7hand) ( i hand best -- best )
  i hand make-subhand hand-value dup best < [
    drop best
  ] unless
  i 20 < [
    [ i 1 + hand ] dip (eval-7hand)
  ] when ;

:: eval-7hand ( seq -- ckf )
  0 seq 9999 (eval-7hand) seq swap hand boa
  ;

! example
"As Kd Jc Kh 2d 2d Kh" 7cards eval-7hand >value
=> "Full House"

Annotation: needs this too

Author: doublec
Mode: factor
Date: Wed, 15 Apr 2009 00:40:58
Plain Text |
: 7cards ( str -- seq )
  " " split [ >ckf ] map ;

Annotation: perm7

Author: randy
Mode: text
Date: Wed, 15 Apr 2009 19:48:23
Plain Text |
kind of stressed (ok very)... tried to generate this perm7 constant. silly? yes. fun? some. anywayz:

400 16806 [a,b] >array [ 7 >base 5 CHAR: 0 pad-head natural-sort >string dup all-unique? [ drop f ] unless ] map sift prune

Annotation: vocab with combinations

Author: elasticdog
Mode: factor
Date: Thu, 7 May 2009 01:50:55
Plain Text |
! math.combinatorics supports combinations now, so you could generate that constant with:

( scratchpad - auto ) 7 iota 5 all-combinations .
{
    { 0 1 2 3 4 }
    { 0 1 2 3 5 }
    { 0 1 2 3 6 }
    { 0 1 2 4 5 }
    { 0 1 2 4 6 }
    { 0 1 2 5 6 }
    { 0 1 3 4 5 }
    { 0 1 3 4 6 }
    { 0 1 3 5 6 }
    { 0 1 4 5 6 }
    { 0 2 3 4 5 }
    { 0 2 3 4 6 }
    { 0 2 3 5 6 }
    { 0 2 4 5 6 }
    { 0 3 4 5 6 }
    { 1 2 3 4 5 }
    { 1 2 3 4 6 }
    { 1 2 3 5 6 }
    { 1 2 4 5 6 }
    { 1 3 4 5 6 }
    { 2 3 4 5 6 }
}

Annotation: even better

Author: elasticdog
Mode: factor
Date: Thu, 7 May 2009 11:42:25
Plain Text |
( scratchpad ) "As Kd Jc Kh 2d 2s Kh" " " split 5 all-combinations [ " " join <hand> ] map infimum

Annotation: in poker vocab now

Author: elasticdog
Mode: factor
Date: Thu, 7 May 2009 15:14:16
Plain Text |
! It's not quite as fast as your hard-coded version, probably due to having to
! generate the combinations, but it will also work for any number of cards (6+)


: parse-cards ( str -- seq )
    " " split [ >ckf ] map ;

: best-hand ( str -- hand )
    parse-cards 5 all-combinations
    [ dup hand-value hand boa ] map infimum ;


( scratchpad - auto ) "As Kd Jc Kh 2d 2s Kh" best-hand dup >value

--- Data stack:
T{ hand f ~array~ 190 }
"Full House"

New Annotation

Summary:
Author:
Mode:
Body: