value." ".$theHand[$i]->theSuit."
"; if($theHand[$i]->theSuit == "clubs") $numClubs++; if($theHand[$i]->theSuit == "diams") $numDiamonds++; if($theHand[$i]->theSuit == "hearts") $numHearts++; if($theHand[$i]->theSuit == "spades") $numSpades++; } if(($numClubs == 5) or ($numDiamonds == 5) or ($numHearts == 5) or ($numSpades == 5)) return True; return False; } //pair, 2 pair, 3 of a kind, straight, flush, full house, four of a kind, //straight flush, royal flush function analyzeHand($theHand){ //sort hand by rank to allow easier calculation of pairs, etc. sort($theHand); //default -> high card $result = $theHand[6]->theRank . " high"; //check for a pair if(($theHand[0]->theRank == $theHand[1]->theRank) or ($theHand[1]->theRank == $theHand[2]->theRank) or ($theHand[2]->theRank == $theHand[3]->theRank) or ($theHand[3]->theRank == $theHand[4]->theRank) or ($theHand[4]->theRank == $theHand[5]->theRank) or ($theHand[5]->theRank == $theHand[6]->theRank)) $result = "pair"; //check for 2 pair if((($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[1]->theRank == $theHand[2]->theRank)) or (($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[2]->theRank == $theHand[3]->theRank)) or (($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[3]->theRank == $theHand[4]->theRank)) or (($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank)) or (($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[5]->theRank == $theHand[6]->theRank)) or (($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[2]->theRank == $theHand[3]->theRank)) or (($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[3]->theRank == $theHand[4]->theRank)) or (($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank)) or (($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[5]->theRank == $theHand[6]->theRank)) or (($theHand[2]->theRank == $theHand[3]->theRank) and ($theHand[3]->theRank == $theHand[4]->theRank)) or (($theHand[2]->theRank == $theHand[3]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank)) or (($theHand[2]->theRank == $theHand[3]->theRank) and ($theHand[5]->theRank == $theHand[6]->theRank)) or (($theHand[3]->theRank == $theHand[4]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank)) or (($theHand[3]->theRank == $theHand[4]->theRank) and ($theHand[5]->theRank == $theHand[6]->theRank)) or (($theHand[4]->theRank == $theHand[5]->theRank) and ($theHand[5]->theRank == $theHand[6]->theRank))) $result = "2pair"; //check for 3 of a kind if((($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[1]->theRank == $theHand[2]->theRank)) or (($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[2]->theRank == $theHand[3]->theRank)) or (($theHand[2]->theRank == $theHand[3]->theRank) and ($theHand[3]->theRank == $theHand[4]->theRank)) or (($theHand[3]->theRank == $theHand[4]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank)) or (($theHand[4]->theRank == $theHand[5]->theRank) and ($theHand[5]->theRank == $theHand[6]->theRank))) $result = "3ofakind"; //check for straight if(((($theHand[0]->value == ($theHand[1]->value)-1) and ($theHand[1]->value == ($theHand[2]->value)-1) and ($theHand[2]->value == ($theHand[3]->value)-1) and ($theHand[3]->value == ($theHand[4]->value)-1)) or (0)) or ((($theHand[1]->value == ($theHand[2]->value)-1) and ($theHand[2]->value == ($theHand[3]->value)-1) and ($theHand[3]->value == ($theHand[4]->value)-1) and ($theHand[4]->value == ($theHand[5]->value)-1)) or (0)) or ((($theHand[2]->value == ($theHand[3]->value)-1) and ($theHand[3]->value == ($theHand[4]->value)-1) and ($theHand[4]->value == ($theHand[5]->value)-1) and ($theHand[5]->value == ($theHand[6]->value)-1)) or (0))) $result = "straight"; //check for flush if(isFlush(array(0,1,2,3,4,5,6),$theHand)) $result = "flush"; //check for full house if((($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[2]->theRank == $theHand[3]->theRank) and ($theHand[3]->theRank == $theHand[4]->theRank)) or (($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[3]->theRank == $theHand[4]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank)) or (($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank) and ($theHand[5]->theRank == $theHand[6]->theRank)) or (($theHand[3]->theRank == $theHand[4]->theRank) and ($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[1]->theRank == $theHand[2]->theRank)) or (($theHand[4]->theRank == $theHand[5]->theRank) and ($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[1]->theRank == $theHand[2]->theRank)) or (($theHand[5]->theRank == $theHand[6]->theRank) and ($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[1]->theRank == $theHand[2]->theRank)) or (($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[3]->theRank == $theHand[4]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank)) or (($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank) and ($theHand[5]->theRank == $theHand[6]->theRank)) or (($theHand[4]->theRank == $theHand[5]->theRank) and ($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[2]->theRank == $theHand[3]->theRank)) or (($theHand[5]->theRank == $theHand[6]->theRank) and ($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[2]->theRank == $theHand[3]->theRank)) or (($theHand[2]->theRank == $theHand[3]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank) and ($theHand[5]->theRank == $theHand[6]->theRank)) or (($theHand[5]->theRank == $theHand[6]->theRank) and ($theHand[2]->theRank == $theHand[3]->theRank) and ($theHand[3]->theRank == $theHand[4]->theRank))) $result = "fullhouse"; //check for 4 of a kind if((($theHand[0]->theRank == $theHand[1]->theRank) and ($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[2]->theRank == $theHand[3]->theRank)) or (($theHand[1]->theRank == $theHand[2]->theRank) and ($theHand[2]->theRank == $theHand[3]->theRank) and ($theHand[3]->theRank == $theHand[4]->theRank)) or (($theHand[2]->theRank == $theHand[3]->theRank) and ($theHand[3]->theRank == $theHand[4]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank)) or (($theHand[3]->theRank == $theHand[4]->theRank) and ($theHand[4]->theRank == $theHand[5]->theRank) and ($theHand[5]->theRank == $theHand[6]->theRank))) $result = "4ofakind"; //check for straight flush if((((($theHand[0]->value == ($theHand[1]->value)-1) and ($theHand[1]->value == ($theHand[2]->value)-1) and ($theHand[2]->value == ($theHand[3]->value)-1) and ($theHand[3]->value == ($theHand[4]->value)-1)) or (0)) and isFlush(array(0,1,2,3,4),$theHand)) or (((($theHand[1]->value == ($theHand[2]->value)-1) and ($theHand[2]->value == ($theHand[3]->value)-1) and ($theHand[3]->value == ($theHand[4]->value)-1) and ($theHand[4]->value == ($theHand[5]->value)-1)) or (0)) and isFlush(array(1,2,3,4,5),$theHand)) or (((($theHand[2]->value == ($theHand[3]->value)-1) and ($theHand[3]->value == ($theHand[4]->value)-1) and ($theHand[4]->value == ($theHand[5]->value)-1) and ($theHand[5]->value == ($theHand[6]->value)-1)) or (0)) and isFlush(array(2,3,4,5,6),$theHand)) ) $result = "straightflush"; //check for royal flush if(((($theHand[0]->theRank == '10') and ($theHand[1]->theRank == 'J') and ($theHand[2]->theRank == 'Q') and ($theHand[3]->theRank == 'K') and ($theHand[4]->theRank == 'A')) and isFlush(array(0,1,2,3,4),$theHand)) or ((($theHand[1]->theRank == '10') and ($theHand[2]->theRank == 'J') and ($theHand[3]->theRank == 'Q') and ($theHand[4]->theRank == 'K') and ($theHand[5]->theRank == 'A')) and isFlush(array(1,2,3,4,5),$theHand)) or ((($theHand[2]->theRank == '10') and ($theHand[3]->theRank == 'J') and ($theHand[4]->theRank == 'Q') and ($theHand[5]->theRank == 'K') and ($theHand[6]->theRank == 'A')) and isFlush(array(2,3,4,5,6),$theHand))) $result = "royalflush"; return $result; } //shuffle order of cards (otherwise this won't be very interesting) function shuffleDeck(&$theDeck){ srand((float)microtime() * 1000000); shuffle($theDeck); for($i=0 ; $i<52 ; $i++) $theDeck[$i]->inPlay = False; } //deal hand function dealHand(&$myDeck){ $myHand = array(); while(count($myHand) < 2){ $i = array_rand($myDeck); $newCard = $myDeck[$i]; if(!$newCard->inPlay){ array_push($myHand, $newCard); $myDeck[$i]->inPlay = True; } } return $myHand; } //deal table cards function dealTableCards(&$myDeck){ $tableCards = array(); while(count($tableCards) < 5){ $i = array_rand($myDeck); $newCard = $myDeck[$i]; if(!$newCard->inPlay){ array_push($tableCards, $newCard); $myDeck[$i]->inPlay = True; } } return $tableCards; } //print hand function printHand($myHand){ print "Your cards:
"; print ""; for($i=0 ; $i < count($myHand) ; $i++){ print ""; } print "
theSuit=="hearts")) print "\"redcard\""; else print "\"blackcard\""; print ">".$myHand[$i]->theRank."&".$myHand[$i]->theSuit.";
"; } //print table cards function printTableCards($tableCards){ print "Table cards:
"; print ""; for($i=0 ; $i < count($tableCards) ; $i++){ print ""; } print "
theSuit=="hearts")) print "\"redcard\""; else print "\"blackcard\""; print ">".$tableCards[$i]->theRank."&".$tableCards[$i]->theSuit.";
"; } function printDeck($myDeck){ for($i=0 ; $i<52 ; $i++) print $myDeck[$i]->theRank."&".$myDeck[$i]->theSuit."; "; print "
"; } $cardRanks = array(0 => "2", 1 => "3", 2 => "4", 3 => "5", 4 => "6", 5 => "7", 6 => "8", 7 => "9", 8 => "10", 9 => "J", 10 => "Q", 11 => "K", 12 => "A"); $cardSuits = array(0 => "clubs", 1 => "diams", 2 => "hearts", 3 => "spades"); class card{ var $value; //to check for straights var $theRank; var $theSuit; var $inPlay = False; } //initialize deck of cards by assigning rank and suit to each card $myDeck = array(); for($i=0 ; $i<52 ; $i++){ $myDeck[$i] = new card(); $myDeck[$i]->value = $i%13; $myDeck[$i]->theRank = $cardRanks[($i%13)]; $myDeck[$i]->theSuit = $cardSuits[($i/13)]; } $numHands = 0; if(($_GET["hand"] == "any") or (!array_key_exists("hand",$_GET))){ $myHand = array(); $myTableCards = array(); shuffleDeck($myDeck); $myHand = dealHand($myDeck); $myTableCards = dealTableCards($myDeck); //figure out how good our hand is $combinedHand = array_merge($myHand,$myTableCards); $result = analyzeHand($combinedHand); $numHands++; } else{ do{ $myHand = array(); $myTableCards = array(); shuffleDeck($myDeck); $myHand = dealHand($myDeck); $myTableCards = dealTableCards($myDeck); //figure out how good our hand is $combinedHand = array_merge($myHand,$myTableCards); $result = analyzeHand($combinedHand); $numHands++; } while($result != $_GET["hand"]); } //now that we have the hand we want print it out //sort($combinedHand); //print_r($combinedHand); print "
"; printTableCards($myTableCards); printHand($myHand); ?>
"Pair", "2pair" => "2 Pair", "3ofakind" => "3 of a Kind", "straight" => "Straight", "flush" => "Flush", "fullhouse" => "Full House", "4ofakind" => "4 of a Kind", "straightflush" => "Straight Flush", "royalflush" => "Royal Flush"); if(!array_key_exists($result,$hands)) print "Result: " . $result . "
"; else print "Result: " . $hands[$result] . "
"; print "Hands required: " . $numHands; ?>

Hand Type:
Any> Flush>
Pair> Full House>
2 Pair> 4 of a Kind>
3 of a Kind> Straight Flush>
Straight> Royal Flush>