PHP Texas Hold ‘Em September 18th, 2006

“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”);

// see if a subset of the hand is a flush
function isFlush($theCards, $theHand) {
// count number of each suit to calculate flushes, etc.
$numClubs = 0;
$numDiamonds = 0;
$numHearts = 0;
$numSpades = 0;
foreach ($theCards as $i) {
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
((($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
((($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))))
$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))) 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))) 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))) 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++) { if (($myHand[$i]->theSuit==”diams”) or ($myHand[$i]->theSuit==”hearts”))
$myClass = “redcard”;
else
$myClass = “blackcard”;
printf(“

“, $myClass, $myHand[$i]->theRank.”&”.$myHand[$i]->theSuit.”;”);
}
print “

%s

“;
}

// print table cards
function printTableCards($tableCards){
print “Table cards:
“;
print “

“;
for ($i=0 ; $i < count($tableCards) ; $i++) { if(($tableCards[$i]->theSuit==”diams”) or ($tableCards[$i]->theSuit==”hearts”))
$myClass = “redcard”;
else
$myClass = “blackcard”;
printf(“

“, $myClass, $tableCards[$i]->theRank.”&”.$tableCards[$i]->theSuit.”;”);
}
print “

%s

“;
}

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;
}
?>

value = $i%13;
$myDeck[$i]->theRank = $cardRanks[($i%13)];
$myDeck[$i]->theSuit = $cardSuits[($i/13)];
}

$numHands = 0;
if ((!array_key_exists(“hand”,$_GET)) or (!array_key_exists($_GET[“hand”], $hands))) {
$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
print “
\n”;
printTableCards($myTableCards);
printHand($myHand);
print “
\n”;

if (!array_key_exists($result, $hands))
print “Result: ” . $result . “
“;
else
print “Result: ” . $hands[$result] . “
“;
print “Hands required: ” . $numHands;
?>

Hand Type:

Any /> Pair /> 2 Pair /> 3 of a Kind /> Straight />
Flush /> Full House /> 4 of a Kind /> Straight Flush /> Royal Flush />

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>