Card=$card;
$this->Suit=$suit;
return(TRUE);
}
}
class myDeck {
var $Deck;
function myDeck() {
$this->Deck = array();
return(TRUE);
}
function newDeck() {
for ($x=1;$x<=4;$x++) {
for ($y=1;$y<=13;$y++) {
array_push($this->Deck, new myCard($y,$x));
}
}
}
function addCard($card,$suit) {
array_push($this->Deck, new myCard($card,$suit));
}
function showCard($pos) {
$card=$this->Deck[$pos]->Card;
$suit=$this->Deck[$pos]->Suit;
switch ($card) {
case 1:
$cardDisp="Ace";
break;
case 13:
$cardDisp="King";
break;
case 12:
$cardDisp="Queen";
break;
case 11:
$cardDisp="Jack";
break;
default:
$cardDisp=$card;
}
switch ($suit) {
case 1:
$suitDisp="Spades";
break;
case 2:
$suitDisp="Hearts";
break;
case 3:
$suitDisp="Diamonds";
break;
case 4:
$suitDisp="Clubs";
break;
}
print "$cardDisp of $suitDisp";
}
function shuffleDeck() {
srand ((float)microtime()*1000000);
shuffle($this->Deck);
}
function showDeck() {
for($x=0;$x<=(sizeof($this->Deck)-1);$x++) {
$this->showCard($x);
print "
\n";
}
}
}
function utime (){
$time = explode( " ", microtime());
$usec = (double)$time[0];
$sec = (double)$time[1];
return $sec + $usec;
}
$start=utime();
?>
print "Results
\n";
$runs=1;
if (isset($_GET['runs'])&&is_numeric($_GET['runs'])&&$_GET['runs']>0) $runs=$_GET['runs'];
for ($mainLoop=1;$mainLoop<=$runs;$mainLoop++) {
$deck = new myDeck();
$deck->newDeck();
$deck->shuffleDeck();
$deckA = new myDeck();
$deckB = new myDeck();
// Deal out all cards, evenly
for ($x=0;$x<=25;$x++) {
$deckA->addCard($deck->Deck[($x*2)]->Card,$deck->Deck[($x*2)]->Suit);
$deckB->addCard($deck->Deck[(($x*2)+1)]->Card,$deck->Deck[(($x*2)+1)]->Suit);
}
print $deckA->totalcards;
$gameon=1;
$numwars=0;
$numrounds=0;
while ($gameon && sizeof($deckA->Deck>0) && sizeof($deckB->Deck)>0) {
$numrounds++;
$cardA=array_pop($deckA->Deck);
$cardB=array_pop($deckB->Deck);
if ($cardA->Card > $cardB->Card) {
array_unshift($deckA->Deck, $cardA, $cardB);
} else if ($cardB->Card > $cardA->Card) {
array_unshift($deckB->Deck, $cardB, $cardA);
} else {
$numwars++;
$waron=1;
$warDeck[0]=$cardA;
$warDeck[1]=$cardB;
$putCard=1;
while ($waron) {
if (sizeof($deckA->Deck) < 4) {
$waron=0;
$gameon=0;
}
if (sizeof($deckB->Deck) < 4) {
$waron=0;
$gameon=0;
}
$warDeck[++$putCard]=array_pop($deckA->Deck);
$warDeck[++$putCard]=array_pop($deckB->Deck);
$warDeck[++$putCard]=array_pop($deckA->Deck);
$warDeck[++$putCard]=array_pop($deckB->Deck);
$warDeck[++$putCard]=array_pop($deckA->Deck);
$warDeck[++$putCard]=array_pop($deckB->Deck);
$cardA=array_pop($deckA->Deck);
$cardB=array_pop($deckB->Deck);
if ($cardA->Card > $cardB->Card) {
$waron=0;$warwin=1;
$warDeck[++$putCard]=$cardA;
$warDeck[++$putCard]=$cardB;
} else if ($cardB->Card > $cardA->Card) {
$waron=0;$warwin=2;
$warDeck[++$putCard]=$cardB;
$warDeck[++$putCard]=$cardA;
} else {
$warDeck[++$putCard]=$cardA;
$warDeck[++$putCard]=$cardB;
if (sizeof($deckA->Deck) < 4) {
$waron=0;
$gameon=0;
}
if (sizeof($deckB->Deck) < 4) {
$waron=0;
$gameon=0;
}
}
}
// Push on the winner
if ($warwin==1) {
for ($z=1;$z=(sizeof($warDeck));$z++) {
array_unshift($deckA->Deck, array_pop($warDeck));
}
}
if ($warwin==2) {
for ($z=1;$z=(sizeof($warDeck));$z++) {
array_unshift($deckB->Deck, array_pop($warDeck));
}
}
}
if (sizeof($deckA->Deck)==0 || sizeof($deckB->Deck)==0) $gameon=0;
}
print "
";
print "Run #$mainLoop results
\n";
if (sizeof($deckA->Deck)Deck)) {
print "Winner: Deck B
\n";
$globalDeckB=$globalDeckB+1;
} else {
print "Winner: Deck A
\n";
$globalDeckA=$globalDeckA+1;
}
if (!isSet($globalLow)) $globalLow=$numrounds;
if (!isSet($globalLowWar)) $globalLowWar=$numwars;
if ($numrounds>$globalHigh) $globalHigh=$numrounds;
if ($numwars>$globalHighWar) $globalHighWar=$numwars;
if ($numrounds<$globalLow) $globalLow=$numrounds;
if ($numwars<$globalLowWar) $globalLowWar=$numwars;
$globalTotalRounds=$globalTotalRounds+$numrounds;
$globalWars=$globalWars+$numwars;
print "Total Rounds: $numrounds
\n";
print "Total Wars: $numwars
\n";
}
print "
\n";
print "Stats for $runs runs
\n";
print "Round Stats
\n";
print "Total Rounds: $globalTotalRounds
\n";
print "Average Rounds per Run: ".($globalTotalRounds/$runs)."
\n";
print "Highest Round count: $globalHigh
\n";
print "Lowest Round count: $globalLow
\n";
print "
War Stats
\n";
print "Total Wars: $globalWars
\n";
print "Average Wars per Run: ".($globalWars/$runs)."
\n";
print "Highest War count: $globalHighWar
\n";
print "Lowest War count: $globalLowWar
\n";
print "
Deck Stats
\n";
print "DeckA wins: $globalDeckA
\n";
print "DeckB wins: $globalDeckB
\n";
$end=utime();
$run = $end - $start;
echo "
Page loaded in " . substr($run, 0, 5) . " secs.";