3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php //Implements the classes above and creates and shuffles a deck and then //deals two or more 5-card hands from the deck. $argv[1] = 2 ;//rand(2,5); if(empty($argv[1])) { print("Exiting..Please run script with number of hands to create as argument."); exit(); } else { $num_of_hands = $argv[1]; print( "CREATING DECK " . "\n"); $myDeck = new Deck(); $myDeck->myPrint(); print("SHUFFLING DECK " . "\n"); $myDeck->shuffle(); $myDeck->myPrint(); print("DEALING " . $num_of_hands . " 5-card HANDS " . "\n"); $hands = array(); for($h=1; $h<= $num_of_hands; $h++) { $hands[$h] = new Hand(); } for($l=1 ; $l<=5; $l++) { for($h=1; $h<= $num_of_hands; $h++) { $card = $myDeck->dealOne(); $hands[$h]->addCard($card); } } print("DECK CARDS ARE NOW"); $myDeck->myPrint(); print("DO THEY HAVE FLUSHES?"); for($h=1; $h<= $num_of_hands; $h++) { print (" HAND " . $h . " HAS FLUSH? " . $hands[$h]->hasStraight($num_of_hands, TRUE) ) ; } print("DO THEY HAVE STRAIGHTS?"); for($h=1; $h<= $num_of_hands; $h++) { print (" HAND " . $h . " HAS FLUSH? " . $hands[$h]->hasStraight($num_of_hands, FALSE) ) ; } } class Deck { private $deck_cards = array(); private $dealt_cards = array(); private $num_of_dealt_cards = 0; private $num_of_cards = 52; private $suit_order; private $top_card; function __construct() { $this->suit_order = array('CLUB', 'DIAMOND', 'HEART', 'SPADE'); $deck_index = 0; for($i=0 ; $i< count($this->suit_order); $i++) { for($j=0; $j<13; $j++) { $this->deck_cards[$deck_index] = new Card(); $this->deck_cards[$deck_index]->suit = $this->suit_order[$i]; $this->deck_cards[$deck_index]->suit_num = $i+1; $this->deck_cards[$deck_index]->value = $j+2; $this->deck_cards[$deck_index]->value_num = $j+2; $this->deck_cards[$deck_index]->dealt = FALSE; switch($j+2) { case 11: $this->deck_cards[$deck_index]->value = "J"; break; case 12: $this->deck_cards[$deck_index]->value = "Q"; break; case 13: $this->deck_cards[$deck_index]->value = "K"; break; case 14: $this->deck_cards[$deck_index]->value = "A"; break; default: break; } $deck_index++; } } } public function dealOne() { if($this->num_of_dealt_cards < 52) { $top_card = $this->deck_cards[($this->num_of_cards - $this->num_of_dealt_cards)-1]; $top_card->myPrint(); $this->num_of_dealt_cards++; $this->dealt_cards[] = $top_card; return $top_card; } } public function myPrint() { echo "\n NOT DEALT: \n"; foreach($this->deck_cards as $card) { if(!$card->dealt) { $card->myPrint(); } } echo "\n ----- \n"; echo "\n DEALT: \n"; foreach($this->dealt_cards as $card) { $card->myPrint(); } echo "\n ----- \n"; } public function shuffle() { $grip = rand(1, 20); $time = rand(1, 60); $mix = rand(3, 5); echo ("<br> grip " . $grip . " time " . $time . " mix " . $mix); while($time >= 1) { for($c=0; $c < count($this->deck_cards); $c++) { $index = $c; $shufflecard = new Card(); $shufflecard->suit = $this->deck_cards[$index]->suit; $shufflecard->suit_num = $this->deck_cards[$index]->suit_num; $shufflecard->value = $this->deck_cards[$index]->value; $shufflecard->value_num = $this->deck_cards[$index]->value_num; $shufflecard->dealt = $this->deck_cards[$index]->dealt; if($index + $grip > 51) { $exchange_index = $index - $grip; } else { $exchange_index = $index + $grip; } $tempcard = new Card(); $tempcard->suit = $this->deck_cards[$exchange_index]->suit; $tempcard->suit_num = $this->deck_cards[$exchange_index]->suit_num; $tempcard->value = $this->deck_cards[$exchange_index]->value; $tempcard->value_num = $this->deck_cards[$exchange_index]->value_num; $tempcard->dealt = $this->deck_cards[$exchange_index]->dealt; $this->deck_cards[$exchange_index]->suit = $shufflecard->suit; $this->deck_cards[$exchange_index]->suit_num = $shufflecard->suit_num; $this->deck_cards[$exchange_index]->value = $shufflecard->value; $this->deck_cards[$exchange_index]->value_num = $shufflecard->value_num; $this->deck_cards[$exchange_index]->dealt = $shufflecard->dealt; $this->deck_cards[$index] = $tempcard; $this->deck_cards[$index]->suit = $tempcard->suit; $this->deck_cards[$index]->suit_num = $tempcard->suit_num; $this->deck_cards[$index]->value = $tempcard->value; $this->deck_cards[$index]->value_num = $tempcard->value_num; $this->deck_cards[$index]->dealt = $tempcard->dealt; unset($shufflecard); unset($tempcard); } $time-- ; } } } class Card { public $suit; public $value; public $suit_num; public $value_num; public $dealt = FALSE; function __construct() { } public function myPrint() { echo ("\n <BR>" . $this->value . " OF " . $this->suit . "\n"); } } class Hand { private $num_of_cards = 0; private $cards = array(); private $sorted = false; private $straight = false; private $boxes; private $bins; private $value_boxes; function __construct() { } public function myPrint() { foreach ($this->cards as $card) { $card->myPrint(); } } public function addCard(&$card) { if($this->num_of_cards >= 0 && $this->num_of_cards < 52) { $this->cards[$this->num_of_cards] = $card; $this->cards[$this->num_of_cards++]->dealt = TRUE; } } public function SortBySuit() { $this->boxes = array(); $this->bins = array(); foreach($this->cards as &$card) { $box_index = $card->suit_num; $this->boxes[$box_index][] = $card; } for($b = 1; $b < 5; $b++) { if(!isset($this->boxes[$b])) { continue; } else { foreach($this->boxes[$b] as &$boxcard) { $binfactor = ($boxcard->value_num - 1) / 3; switch($binfactor) { case $binfactor <= 1 : $this->bins[$b][1][] = $boxcard; break; case $binfactor > 1 && $binfactor <= 2 : $this->bins[$b][2][] = $boxcard; break; case $binfactor > 2 && $binfactor <= 3 : $this->bins[$b][3][] = $boxcard; break; case $binfactor > 3 && $binfactor <= 5 : $this->bins[$b][4][] = $boxcard; break; } } if(count($this->bins[$b] != 0) && FALSE) { foreach($this->bins[$b] as &$bin) { $bin_length = count($bin); for($i=0; $i < $bin_length-1; $i++) { for($j=$i+1; $j< $bin_length; $j++) { if($bin[$i]->value_num > $bin[$j]->value_num) { $temp1 = new Card(); $temp1->suit = $bin[$j]->suit; $temp1->suit_num = $bin[$j]->suit_num; $temp1->value = $bin[$j]->value; $temp1->value_num = $bin[$j]->value_num; $temp1->dealt = $bin[$j]->dealt; $temp2 = new Card(); $temp2->suit = $bin[$i]->suit; $temp2->suit_num = $bin[$i]->suit_num; $temp2->value = $bin[$i]->value; $temp2->value_num = $bin[$i]->value_num; $temp2->dealt = $bin[$i]->dealt; $bin[$i]->suit = $temp1->suit; $bin[$i]->suit_num = $temp1->suit_num; $bin[$i]->value = $temp1->value; $bin[$i]->value_num = $temp1->value_num; $bin[$i]->dealt = $temp1->dealt; $bin[$j]->suit = $temp2->suit; $bin[$j]->suit_num = $temp2->suit_num; $bin[$j]->value = $temp2->value; $bin[$j]->value_num = $temp2->value_num; $bin[$j]->dealt = $temp2->dealt; unset($temp1); unset($temp2); } } } } } } } $card_index = 0; for($b = 1; $b < 5; $b++) { echo var_export($this->bins[$b], TRUE); /* if(!isset($this->boxes[$b])) { continue; } else { foreach($this->bins[$b] as &$bin) { foreach($bin as &$bincard) { $this->cards[$card_index]->suit = $bincard->suit; $this->cards[$card_index]->suit_num = $bincard->suit_num; $this->cards[$card_index]->value = $bincard->value; $this->cards[$card_index]->value_num = $bincard->value_num; $this->cards[$card_index]->dealt = $bincard->dealt; $card_index++; unset($bincard); } } }*/ } echo var_export($this->cards, TRUE); } public function SortByValue() { $this->value_boxes = array(); foreach($this->cards as $card) { $this->value_boxes[$card->value_num][] = $card; } for($v = 2; $v < 15 ; $v++) { if(count($this->value_boxes[$v]) != 0) { foreach($this->value_boxes[$v] as $boxcard) { $box_length = count($this->value_boxes[$v]); for($i=0; $i < $box_length-1; $i++) { for($j=i+1; $j< $box_length; $j++) { if($boxcard[$i]->suit_num > $boxcard[$j]->value_num) { $temp1 = new Card(); $temp1->suit = $boxcard[$j]->suit; $temp1->suit_num = $boxcard[$j]->suit_num; $temp1->value = $boxcard[$j]->value; $temp1->value_num = $boxcard[$j]->value_num; $temp1->dealt = $boxcard[$j]->dealt; $temp2 = new Card(); $temp2->suit = $boxcard[$i]->suit; $temp2->suit_num = $boxcard[$i]->suit_num; $temp2->value = $boxcard[$i]->value; $temp2->value_num = $boxcard[$i]->value_num; $temp2->dealt = $boxcard[$i]->dealt; $boxcard[$i]->suit = $temp1->suit; $boxcard[$i]->suit_num = $temp1->suit_num; $boxcard[$i]->value = $temp1->value; $boxcard[$i]->value_num = $temp1->value_num; $boxcard[$i]->dealt = $temp1->dealt; $boxcard[$j]->suit = $temp2->suit; $boxcard[$j]->suit_num = $temp2->suit_num; $boxcard[$j]->value = $temp2->value; $boxcard[$j]->value_num = $temp2->value_num; $boxcard[$j]->dealt = $temp2->dealt; unset($temp1); unset($temp2); } } } } } } foreach($this->cards as &$card) { unset($card); } $this->cards = array(); for($v = 2; $v < 15; $v++) { if(count($this->value_boxes[$v] != 0)) { foreach($this->value_boxes[$v] as &$boxcard) { $this->cards[] = $boxcard; unset($boxcard); } } } } public function hasStraight($len, $sameSuit) { $straight = false; if($sameSuit) { $this->SortBySuit(); for($i = 0; i < $this->num_of_cards-2; $i++) { for($j = i+1; j < $this->num_of_cards-1; $j++) { if($this->card[$i]->suit_num != $this->card[$i]->suit_num) { if($length_count == $len) { $straight = true; break; } else { $i = $j; $straight = false; continue; } } else { if($this->card[$i]->value_num == $this->card[$i]->value_num + 1) { $length_count++; if($length_count == $len) { $straight = true; break; } } else { $i = $j; $straight = false; continue; } } } if($straight) { break; } } } else { SortByValue(); for($i = 0; i < $this->num_of_cards-2; $i++) { for($j = i+1; j < $this->num_of_cards-1; $j++) { if($this->card[$i]->value_num != $this->card[$i]->value_num+1) { if($length_count == $len) { $straight = true; } else { $i = $j; $straight = false; continue; } } else { $length_count++; if($length_count == $len) { $straight = true; break; } } } if($straight) { break; } } } return $straight; } }