'High Card', 'num' => 0), array('name' => 'One Pair', 'num' => 0), array('name' => 'Two Pair', 'num' => 0), array('name' => 'Three of a Kind', 'num' => 0), array('name' => 'Straight', 'num' => 0), array('name' => 'Flush', 'num' => 0), array('name' => 'Full House', 'num' => 0), array('name' => 'Four of a Kind', 'num' => 0), array('name' => 'Straight Flush', 'num' => 0), ); $winners = array( array('name' => 'High Card', 'num' => 0), array('name' => 'One Pair', 'num' => 0), array('name' => 'Two Pair', 'num' => 0), array('name' => 'Three of a Kind', 'num' => 0), array('name' => 'Straight', 'num' => 0), array('name' => 'Flush', 'num' => 0), array('name' => 'Full House', 'num' => 0), array('name' => 'Four of a Kind', 'num' => 0), array('name' => 'Straight Flush', 'num' => 0), ); srand((float)microtime()*1000000); function shuffleDeck() { global $deck; $deck = array(); for ($rank = 2; $rank < 15; $rank++) { for ($suit = 0; $suit < 4; $suit++) { $deck[] = array($rank, $suit); // add a card to the deck } } } function dealCard() { global $deck; $card = array_splice($deck, array_rand($deck), 1); return $card[0]; } function printCard($card) { $s = ''; // rank switch ($card[0]) { case 14: $s = 'A'; break; case 13: $s = 'K'; break; case 12: $s = 'Q'; break; case 11: $s = 'J'; break; default: $s = $card[0]; break; } // suit switch ($card[1]) { case 0: $s .= 'c'; break; case 1: $s .= 'd'; break; case 2: $s .= 'h'; break; case 3: $s .= 's'; break; // should never get here default: $s .= 'e'; break; } return $s; } function printHands() { global $hands; global $NUM_PLAYERS; for ($i = 0; $i < $NUM_PLAYERS; $i++) { echo " "; for ($j = 0; $j < 10; $j++) { echo printCard($hands[$i]->c[$j])." "; } echo "\n"; } echo "\n"; } ?>

A henway simulation with players and trials

rank ]['num']++; } // for each player if ($g < $PRINT_HANDS) { printHands(); } // calculate winners $maxRank = 0; for ($p = 0; $p < $NUM_PLAYERS; $p++) { $maxRank = max($maxRank, $hands[$p]->rank); } $winners[$maxRank]['num']++; } // for each game ?>
Game Players
1
1

= 0; $r--) { ?>
Rank # of Wins /
# of Occurances
Chance of
Occurance
Chance of
Winning
Chance of
being the
winning hand
/ NAN% NAN% NAN%
c = $tempArray; $this->rankHand(); } function rankHand() { $lastRank = -1; $straightCount = 1; $maxStraightCount = 1; $straightStart = 0; // fill index 2 through 14 with value 0 $rankCount = array_fill(2, 15, 0); // fill index 0 through 3 with value 0 $suitCount = array_fill(0, 4, 0); for ($i = 0; $i <10; $i++) { $currentRank = $this->c[$i][0]; $rankCount[ $currentRank ]++; $suitCount[ $this->c[$i][1] ]++; // check for a straight // cards are sorted in decending order // if we have a double-straight, the straightStart will be the lower // straight if ($lastRank - 1 == $currentRank) { $straightCount++; $maxStraightCount = max($maxStraightCount, $straightCount); // do we have a Ace-2-3-4-5 straight? if ($currentRank == 2 && $straightCount >= 4 && $this->c[0][0] == 14) { $straightCount++; $maxStraightCount = max($maxStraightCount, $straightCount); } } elseif ($lastRank == $currentRank) { // do nothing } else { $straightCount = 1; $straightStart = $i; } $lastRank = $currentRank; } arsort($rankCount); reset($rankCount); arsort($suitCount); reset($suitCount); $prevNum = 0; $this->rank = 0; while (list ($rank, $num) = each ($rankCount)) { if ($num == 4) { // four of a kind $this->rank = max(7, $this->rank); break; } if ($prevNum == 3) { if ($num >=2) { // full house $this->rank = max(6, $this->rank); break; } else { // unless we have a straight or flush, we have 3 of a kind break; } } if ($num == 3) { $prevNum = 3; $this->rank = max(3, $this->rank); continue; } if ($prevNum == 2) { if ($num == 2) { // unless we have a straight or flush, we have 2 pair $this->rank = max(2, $this->rank); break; } else { // unless we have a straight or flush, we have 1 pair break; } } if ($num == 2) { $prevNum = 2; $this->rank = max(1, $this->rank); continue; } // no pair $this->rank = max(0, $this->rank); break; } // while we go through the ranks list ($suit, $num) = each ($suitCount); if ($num >=5) { $this->rank = max(5, $this->rank); } elseif ($maxStraightCount >= 5) { $this->rank = max(4, $this->rank); } // check for straight flushes. This will miss double-flush hands // where the lower suit contains the straight flush. if ($num >=5 && $maxStraightCount >= 5) { $tempHand = array(); for ($i = 0; $i < 10; $i++) { // get all the cards of the flush suit if ($this->c[$i][1] == $suit) { $tempHand[] = $this->c[$i][0]; // only need the rank } } // do we have a straight within the flush? $lastRank = -1; $straightFlushCount = 1; $straightFlushStart = 0; for ($j = 0; $j < count($tempHand); $j++) { $currentRank = $tempHand[$j]; if ($lastRank - 1 == $currentRank) { $straightFlushCount++; // do we have a Ace-2-3-4-5 straight? if ($currentRank == 2 && $straightFlushCount >= 4 && $tempHand[0] == 14) { $straightFlushCount++; } } else { if ($straightFlushCount >= 5) { break; } $straightFlushCount = 1; $straightFlushStart = $i; } $lastRank = $currentRank; } // for each card in the flush if ($straightFlushCount >= 5) { $this->rank = 8; } } // straight flush check } // rankHand() } // Hand class class Card { var $rank; // rank of the card 2-14 var $suit; // suit 0=clubs 1=diamonds 2=hearts 3=spades function Card ($rank, $suit) { $this->rank = $rank; $this->suit = $suit; } } // Card class ?>