; Hold'em Calculator ; By: NaquadaBomb ; ; Ranking System ; ============== ; Straight Flush 7453 through 7462 (Range: 10) ; Four of a Kind 7297 through 7452 (Range: 156) ; Full House 7141 through 7296 (Range: 156) ; Flush 5864 through 7140 (Range: 1277) ; Straight 5854 through 5863 (Range: 10) ; Three of a Kind 4996 through 5853 (Range: 858) ; Two Pair 4138 through 4995 (Range: 858) ; Pair 1278 through 4137 (Range: 2860) ; High Card 1 through 1277 (Range: 1277) ; ** High Card and Flush have the EXACT same counting pattern ; ** If passed only two arguments, four of a kind and full house ; ** Starting with a three of a kind (going lower), not all values are reachable ; because texas hold'em consists of seven total cards, even though only five count ; ** Uses hash tables to speed up by only doing a counting loop once ; Initialize the hash tables on *:start:noop $count3k $count2Pair $countHc $countPair $count156 ; For straight comparisons alias -l c return $iif($1 == $calc($2 + 1), $true, $false) alias -l cs return $iif(($c($1, $2) && $c($2, $3) && $c($3, $4) && $c($4, $5)), $true, $false) alias -l for { if (%for < $1) || (%for == $null) { inc %for | return $true } set %for 0 | return $false } alias -l forbreak { set %for 0 | return break } alias -l forreturn { set %for 0 | return return $1 } alias -l array { if ($isid) return $arrayid($getname($1), $2, $3) if ($1 == -a) { set -u0 %Table $getname($2) hadd -m %Table $calc($arrayid(%Table, 0) + 1) $3- } } alias -l getname return $iif($1, Array. $+ $1, $null) alias -l arrayid { if ($3 isnum) return $hfind($1, $2, $3).data if (!$hget($1)) return $iif($2 == 0, 0, $null) if ($2 == 0) return $hget($1, 0).item return $hget($1, $2) } ; For four of a kind and a full house alias -l count156 { if (!$array(C156, 0)) { var %aa = 2, %bb, %count = 0 while (%aa <= 14) { set %bb 2 while (%bb <= 14) { if (%aa == %bb) { inc %bb continue } array -a C156 %aa %bb inc %bb } inc %aa } } else return $array(C156, $1 $2, 1) } alias -l countPair { if (!$array(Pairs, 0)) { var %aa = 2, %bb, %cc, %dd while (%aa <= 14) { set %bb 2 while (%bb <= 14) { if (%bb == %aa) { inc %bb continue } set %cc 2 while (%cc < %bb) { if (%cc == %aa) { inc %cc continue } set %dd 2 while (%dd < %cc) { if (%dd == %aa) { inc %dd continue } if (%bb != %cc) && (%cc != %dd) && (%bb != %dd) array -a Pairs %aa %bb %cc %dd inc %dd } inc %cc } inc %bb } inc %aa } } else return $array(Pairs, $1 $2 $3 $4, 1) } alias -l countHc { if (!$array(Hc, 0)) { var %aa = 7, %bb, %cc, %dd, %ee while (%aa <= 14) { set %bb 5 while (%bb <= 13) && (%bb < %aa) { set %cc 4 while (%cc <= 12) && (%cc < %bb) { set %dd 3 while (%dd <= 11) && (%dd < %cc) { set %ee 2 while (%ee <= 9) && (%ee < %dd) { if ($cs(%aa, %bb, %cc, %dd, %ee)) { } else if (%aa == 14) && (%bb == 5) && (%cc == 4) && (%dd == 3) && (%ee == 2) { } else array -a Hc %aa %bb %cc %dd %ee inc %ee } inc %dd } inc %cc } inc %bb } inc %aa } } else return $array(Hc, $1 $2 $3 $4 $5, 1) } alias -l count2Pair { if (!$array(2Pair, 0)) { var %aa = 2, %bb, %cc while (%aa <= 14) { set %bb 2 while (%bb <= 14) && (%bb < %aa) { set %cc 2 while (%cc <= 14) { if (%cc != %bb) && (%cc != %aa) array -a 2Pair %aa %bb %cc inc %cc } inc %bb } inc %aa } } else return $array(2Pair, $1 $2 $3, 1) } alias -l count3k { if (!$array(3Kind, 0)) { var %aa = 2, %bb, %cc, %dd while (%aa <= 14) { set %bb 2 while (%bb <= 14) { set %cc 2 while (%cc <= 14) && (%cc < %bb) { if (%aa != %bb) && (%aa != %cc) array -a 3Kind %aa %bb %cc inc %cc } inc %bb } inc %aa } } else return $array(3Kind, $1 $2 $3, 1) } alias -l isDuplicate return $iif($findtok($1, $2, 0, 32) > 1, $true, $false) alias -l suits return $gettok(C S H D, $1, 32) alias -l isFlush { if ($prop == any) { var %i 1 | while (%i <= 4) && ($count($1, $suits(%i)) < 5) inc %i if (%i == 4) return $false return $true } if ($flushType($1)) return $true return $false } alias -l gs return $getSuit($getCard($1, $2)) alias -l flushType { var %test if ($prop == any) set %test $1 else set %test $gs($1, 1) $gs($1, 2) $gs($1, 3) $gs($1, 4) $gs($1, 5) while $for(4) { if ($count(%test, $suits(%for)) >= 5) $forreturn($suits(%for)) } return $false } alias -l getCardCount return $numtok($1, 32) alias -l getCard return $gettok($1, $2, 32) alias -l getCards return $gettok($1, $+($2, -, $3), 32) alias -l getValue return $left($1, -1) alias -l getSuit return $right($1, 1) alias -l getStrength { var %ch 2 3 4 5 6 7 8 9 10 J Q K A if ($2 != $null) tokenize 32 $getValue($getCard($1, $2)) if ($prop == lowace) return $iif($findtok(A 2 3 4 5 6 7 8 9 10 J Q K, $1, 32), $v1, 0) if ($prop == plus) return $iif($findtok(%ch, $1, 32), $calc($v1 + 1), 0) return $iif($findtok(%ch, $1, 32), $v1, 0) } alias -l gsp return $getStrength($1, $2).plus alias -l getCount return $matchtok($1, $2, 0, 32) alias -l stripSuits return $remove($1, C, S, H, D) alias -l sc return $swapCards($1, $2, $3) alias -l swapCards { var %card1 = $getCard($1, $2) var %card2 = $getCard($1, $3) return $instok($deltok($instok($deltok($1, $2, 32), %card2, $2, 32), $3, 32), %card1, $3, 32) } alias -l moveToFront { var %result = $1, %to, %stripSuit = $stripSuits($1), %i = $iif($3 isnum, $3, 1) while ($getCard(%stripSuit, %i) == $2) inc %i set %to %i inc %i while (%i <= 7) { if ($getCard(%stripSuit, %i) == $2) { set %result $swapCards(%result, %i, %to) inc %to } inc %i } return %result } alias -l sortDescending { var %largest, %largestValue, %result = $1, %card, %i, %j, %strength1, %strength2, %suit set %i $iif($2, $2, 1) while (%i < 7) { if ($3) set %strength1 $getStrength(%result, %i).lowace else set %strength1 $getStrength(%result, %i) set %suit $getSuit($getCard(%result, %i)) if ($prop) && (%suit == $prop) set %strength1 $calc(%strength1 * 10) set %largest 0 | set %largestValue 0 set %j $calc(%i + 1) while (%j <= 7) { if ($3) set %strength2 $getStrength(%result, %j).lowace else set %strength2 $getStrength(%result, %j) set %suit $getSuit($getCard(%result, %j)) if ($prop) && (%suit == $prop) set %strength2 $calc(%strength2 * 10) if (%strength2 > %largestValue) { set %largest %j set %largestValue %strength2 } inc %j } if (%largest > 0) && (%largestValue > %strength1) set %result $swapCards(%result, %i, %largest) inc %i } return %result } alias -l sortMultiples { var %strength, %card, %i, %to, %start = 1, %result = $1, %stripSuit = $stripSuits($1) var %k = 1, %FourSort = $false, %ThreeSort = $false, %TwoSortTop = $false, %TwoSortBottom = $false while $for(7) { set %card $getValue($getCard($1, %for)) set %strength $getStrength(%card) if ($getCount(%stripSuit, %card) == 4) { set %FourSort %card | $forbreak } else if ($getCount(%stripSuit, %card) == 3) { if (%ThreeSort) && (%strength > $getStrength(%ThreeSort)) { set %ThreeSort %card } else if (!%ThreeSort) { set %ThreeSort %card } } else if ($getCount(%stripSuit, %card) == 2) { if (!%TwoSortTop) set %TwoSortTop %card else if (%strength > $getStrength(%TwoSortTop)) { set %TwoSortBottom %TwoSortTop | set %TwoSortTop %card } else if (%card != %TwoSortTop) && (%TwoSortBottom) && (%strength > $getStrength(%TwoSortBottom)) { set %TwoSortBottom %card } else if (%card != %TwoSortTop) && (!%TwoSortBottom) { set %TwoSortBottom %card } } } if (%FourSort) && ($prop == four) return $true if (%ThreeSort) && (%TwoSortTop) && ($prop == full) return $true if (%ThreeSort) && ($prop == three) return $true if (%TwoSortBottom) && ($prop == 2pair) return $true if (%TwoSortTop) && ($prop == pair) return $true if ($prop) return $false if (%FourSort) return $sortDescending($moveToFront(%result, %FourSort), 5) if (%ThreeSort) { set %result $moveToFront(%result, %ThreeSort) if (%TwoSortTop) return $sortDescending($moveToFront(%result, %TwoSortTop, 4), 6) return $sortDescending(%result, 4) } if (%TwoSortTop) { set %result $moveToFront(%result, %TwoSortTop) if (%TwoSortBottom) return $sortDescending($moveToFront(%result, %TwoSortBottom, 3), 5) return $sortDescending(%result, 3) } } alias -l moveStraightToFront { var %result $1 if ($isStraightInFront($1)) || ($isStraightInFront($1).lowace) return %result if ($isStraightInFront($getCards($1, 2, 7))) || ($isStraightInFront($getCards($1, 2, 7)).lowace) { set %result $sc($sc($sc($sc($sc(%result, 1, 2), 2, 3), 3, 4), 4, 5), 5, 6) } if ($isStraightInFront($getCards($1, 3, 7))) || ($isStraightInFront($getCards($1, 3, 7)).lowace) { set %result $sc($sc($sc($sc($sc(%result, 2, 3), 3, 4), 4, 5), 5, 6), 6, 7) set %result $sc($sc($sc($sc($sc(%result, 1, 2), 2, 3), 3, 4), 4, 5), 5, 6) } return %result } alias -l sortStraight return $isStraight($1, $prop).sort alias -l isStraight { var %sort $moveStraightToFront($sortDescending($1). [ $+ [ $2 ] ]) var %sortLow $moveStraightToFront($sortDescending($1, 1, $true). [ $+ [ $2 ] ]) if ($prop == sort) { if ($isStraightInFront(%sort)) return %sort if ($isStraightInFront(%sortLow).lowace) return %sortLow } else { if ($isStraightInFront(%sort)) return $true if ($isStraightInFront(%sortLow).lowace) return $true } return $false } alias -l isStraightInFront { set -u0 %cardA $calc($getStrength($1, 1). [ $+ [ $prop ] ] - 1) set -u0 %cardB $getStrength($1, 2). [ $+ [ $prop ] ] if (%cardA == %cardB) { set %cardA $calc($getStrength($1, 2). [ $+ [ $prop ] ] - 1) set %cardB $getStrength($1, 3). [ $+ [ $prop ] ] if (%cardA == %cardB) { set %cardA $calc($getStrength($1, 3). [ $+ [ $prop ] ] - 1) set %cardB $getStrength($1, 4). [ $+ [ $prop ] ] if (%cardA == %cardB) { set %cardA $calc($getStrength($1, 4). [ $+ [ $prop ] ] - 1) set %cardB $getStrength($1, 5). [ $+ [ $prop ] ] if (%cardA == %cardB) return $true } } } return $false } alias holdem { var %flushSort = $false, %sort, %card, %suit, %value, %sortlow, %c1, %c2, %c3, %c4, %c5 ; Syntax Checking if ($getCardCount($1) != 7) return while $for(7) { set %card $getCard($1, %for) set %value $getValue(%card) set %suit $getSuit(%card) if ($isDuplicate($1, %card)) || (!$istok(D H S C, %suit, 32)) $forreturn if (%value !isnum 2-10) && (!$istok(J Q K A, %value, 32)) $forreturn } ; Straight Flush 7453 through 7462 (Range: 10) if ($isFlush($1).any) { set %flushSort $flushType($1).any set %sort $sortStraight($1). [ $+ [ %flushSort ] ] if ($isStraight(%sort, %flushsort) && $isFlush(%sort)) { if ($prop == getSorted) return %sort return $calc(7449 + $getStrength(%sort, 1)) } } ; Four of a Kind 7297 through 7452 (Range: 156) ; ** 13(four of a kind) * 12 (other kickers) if ($sortMultiples($1).four) { set %sort $sortMultiples($1) if ($prop == getSorted) return %sort return $calc(7296 + $count156($calc($getStrength(%sort, 1) + 1), $calc($getStrength(%sort, 5) + 1))) } ; Full House 7141 through 7296 (Range: 156) ; ** 13(3 of a kind) * 12 (different pairs each) if ($sortMultiples($1).full) { set %sort $sortMultiples($1) if ($prop == getSorted) return %sort return $calc(7140 + $count156($gsp(%sort, 1), $gsp(%sort, 4))) } ; Flush 5864 through 7140 (Range: 1277) set %flushSort $flushType($1).any if (%flushSort) { set %sort $sortDescending($1). [ $+ [ %flushSort ] ] if ($prop == getSorted) return %sort while $for(5) { set %c $+ %for $gsp(%sort, %for) } return $calc(5863 + $countHc(%c1, %c2, %c3, %c4, %c5)) } ; Straight 5854 through 5863 (Range: 10) if ($isStraight($1)) { set %sort $sortStraight($1) if ($prop == getSorted) return %sort return $calc(5849 + $gsp(%sort, 1)) } ; Three of a Kind 4996 through 5853 (Range: 858) if ($sortMultiples($1).three) { set %sort $sortMultiples($1) if ($prop == getSorted) return %sort return $calc(4995 + $count3k($gsp(%sort, 1), $gsp(%sort, 4), $gsp(%sort, 5))) } ; Two Pair 4138 through 4995 (Range: 858) if ($sortMultiples($1).2pair) { set %sort $sortMultiples($1) if ($prop == getSorted) return %sort return $calc(4137 + $count2Pair($gsp(%sort, 1), $gsp(%sort, 3), $gsp(%sort, 5))) } ; Pair 1278 through 4137 (Range: 2860) if ($sortMultiples($1).pair) { set %sort $sortMultiples($1) if ($prop == getSorted) return %sort while $for(5) { set %c $+ %for $gsp(%sort, %for) } return $calc(1277 + $countPair(%c1, %c3, %c4, %c5)) } ; High Card 1 through 1277 (Range: 1277) set %sort $sortDescending($1) if ($prop == getSorted) return %sort while $for(5) { set %c $+ %for $gsp(%sort, %for) } return $countHc(%c1, %c2, %c3, %c4, %c5) }