/** * Return true once all players have * been dealt cards * Return $this for possible chaining * * @return Dealer */ public function dealCards() { foreach ($this->_players as $player) { if ($player->getHasBeenDealt() == false) { while ($player->getTotalCards() < 5) { $player->receiveCard($this->_deck->dealCard()); } $player->setHasBeenDealt(true); } } return $this; }
public function testDeck() { $Deck = new Deck(); $cards = $Deck->getCards(); $this->assertEquals(52, count($cards), 'There must be 52 cards in the Deck'); $spades = []; $hearts = []; $clubs = []; $diamonds = []; foreach ($cards as $Card) { switch ($Card->getSuit()) { case '♣': $clubs[] = $Card; break; case '♦': $diamonds[] = $Card; break; case '♥': $hearts[] = $Card; break; case '♠': $spades[] = $Card; break; default: throw new Exception('Invalid Suit Found.'); } } $this->assertEquals(13, count($spades), 'There must be 13 spade cards'); $this->assertEquals(13, count($hearts), 'There must be 13 heart cards'); $this->assertEquals(13, count($clubs), 'There must be 13 club cards'); $this->assertEquals(13, count($diamonds), 'There must be 13 diamond cards'); $Card = $Deck->dealCard(); $this->assertTrue($Card instanceof Card); $cards = $Deck->dealCards(5); $this->assertEquals(5, count($cards)); $cards = $Deck->dealCards(2); $this->assertEquals(2, count($cards)); $stack = $Deck->getStack(); //We dealt 8 cards, so should only have 44 in the stack $this->assertEquals(44, count($stack)); }
<?php require_once "Player.php"; require_once "Deck.php"; $players = array(new Player("Player One"), new Player("Player Two"), new Player("Player Three"), new Player("Player Four")); $deckOfCards = new Deck(); $deckOfCards->shuffleCards(); // Deal the cards to the player for ($i = 0; $i < 7; $i++) { foreach ($players as $player) { $player->addCard($deckOfCards->dealCard()); } } // Display cards and information foreach ($players as $player) { echo "<h1>{$player->getName()}</h1>"; echo $player->showCards(); }
$deckLength = $this->cards->length(); $index = rand(0, $deckLength - 1); $this->cards->swapToHead($index); for ($i = 0; $i < $deckLength - 1; $i++) { $index = rand($i, $deckLength - 2); $this->cards->swapToHead($index); } } public function dealCard() { $temp = $this->cards->head; $this->cards->head = $this->cards->head->next; $temp->next = null; return $temp; } } $deck1 = new Deck(); var_dump($deck1->cards); $deck1->cards->printValues(); $deck1->shuffleCards(); var_dump($deck1->cards); var_dump($deck1->dealCard()); var_dump($deck1->dealCard()); var_dump($deck1->dealCard()); var_dump($deck1->dealCard()); $deck1->shuffleCards(); var_dump($deck1->cards); echo "" . $deck1->cards->length() . "cards in the deck"; $deck1->resetDeck(); var_dump($deck1->cards); echo "" . $deck1->cards->length() . "cards in the deck";