Example #1
0
    $pHands = array();
    foreach ($players as $player) {
        $pCards = array();
        foreach ($shuffledDeck as $k => $card) {
            $pCards[] = $card;
            unset($shuffledDeck[$k]);
            if (count($pCards) == $numCards) {
                break;
            }
        }
        $pHands[$player] = $pCards;
    }
    return $pHands;
}
echo '<pre>';
// ----------- USAGE -----------------
// Crack open a brand new deck of cards
$deck = getDeck();
// Shuffle the deck
shuffleDeck($deck);
echo "<br>";
echo '<h2>' . 'New deck before dealing: <br/>' . '</h2>';
print_r($deck);
$players = array('Joe', 'Mary', 'Zim');
$numCards = 3;
$playerHands = deal($players, $numCards, $deck);
echo '<h2>' . 'Hands each player has: <br/>' . '</h2>';
print_r($playerHands);
echo '<h2>' . 'Deck after dealing: <br/>' . '</h2>';
print_r($deck);
echo '<pre>';
/**
*Controls card game
* @param array $players list of 
*
* @return void
*/
function playCardGame($players, $num_cards)
{
    //Create assoc. array for players and their scores w/ empty array for their hands
    $playersList = [];
    foreach ($players as $player) {
        $playersList[$player] = array('score' => 0, 'hand' => array());
    }
    //create a deck
    $new_deck = getDeck();
    //Initiate round counter
    $round = 1;
    //Play rounds while there are enough cards in deck to do so
    while (count($players) * $num_cards <= count($new_deck)) {
        gameRound($playersList, $num_cards, $new_deck, $round);
        $round++;
    }
    //Figure out who won and print to screen
    $winner = findWinner($playersList);
    echo "<h3>The Winner is: " . $winner . "</h3>";
}