Exemplo n.º 1
//Temporary values for loops
if (isset($_REQUEST)) {
    //The values set by the User
/* ** Main Logic ** */
$cards_type_len = count($card_type);
$cards_numbers_len = count($card_numbers);
$total_cards = $cards_type_len * $cards_numbers_len;
//Total Number of Cards Present for distributing
$total_cards_distribute = $person_num * $cards_person;
//Create cards
$cards_arr = createCards();
$cards_len = count($cards_arr);
//$cards_arr['Club'][13] = Array('K'=> Array(0=>Array(4, 6, 8),1,2)); //Checking if multi-dimensional
$cards_arr = shuffleDeck($cards_arr);
//Double shuffling so that no Arithmetic predictions
$reserves = NULL;
//Dynamically creating the persons
for (; $i < $person_num;) {
    //Pre-increment is faster than Post-increment in PHP
    $p[$i] = new ReadingCards($cards_person);
    //Creating the no. of required persons dynamically on fly as per mentioned
    //For last distribution if it only meant for last person then no jumbling simple assign
    distributeCards($p[$i], (bool) (++$i == $person_num && $total_cards == $total_cards_distribute));
//For extra cards we would be pushing it into reserves
if ($total_cards != $total_cards_distribute) {
    $remaining_cards = $total_cards - $total_cards_distribute;
    $reserves = new ReadingCards($remaining_cards);
    //Left out cards
Exemplo n.º 2
    $pHands = array();
    foreach ($players as $player) {
        $pCards = array();
        foreach ($shuffledDeck as $k => $card) {
            $pCards[] = $card;
            if (count($pCards) == $numCards) {
        $pHands[$player] = $pCards;
    return $pHands;
echo '<pre>';
// ----------- USAGE -----------------
// Crack open a brand new deck of cards
$deck = getDeck();
// Shuffle the deck
echo "<br>";
echo '<h2>' . 'New deck before dealing: <br/>' . '</h2>';
$players = array('Joe', 'Mary', 'Zim');
$numCards = 3;
$playerHands = deal($players, $numCards, $deck);
echo '<h2>' . 'Hands each player has: <br/>' . '</h2>';
echo '<h2>' . 'Deck after dealing: <br/>' . '</h2>';
echo '<pre>';
Exemplo n.º 3
*Plays a single round/hand by shuffling deck, dealing cards, calculating score, and printing to screen
* @param array $players an array containing the players and their scores (passed by ref)
*@param int $numCards the number of cards to deal this round
*@param array $deck the array of cards passed by ref
*@param int $roundNum the number of the round in the game
* @return void 
function gameRound(&$players, $numCards, &$deck, $roundNum)
    //shuffle the deck
    //deal cards out to players
    deal($players, $numCards, $deck);
    //echo new round title to screen
    echo "<div class = 'Round'>";
    echo "<h3>Round Number {$roundNum} </h3>";
    //loop through players
    foreach ($players as $player => $hand) {
        //return score of player's hand
        $roundScore = scoreHand($players[$player]['hand']);
        //add hand score to total score
        $players[$player]['score'] += $roundScore;
        //Print hand and score to screen
        displayHand($players, $player, $roundScore, $players[$player]['score']);
        //Clear hand for next round
        $players[$player]['hand'] = [];
    //Print count of remaining cards in deck to screen
    echo "<br><p>Remaining cards: " . count($deck) . "</p><br></div>";