コード例 #1
0
ファイル: index.php プロジェクト: mrajkumar83/deck-problem
//Temporary values for loops
if (isset($_REQUEST)) {
    //The values set by the User
    extract($_REQUEST);
}
/* ** 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
コード例 #2
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>';
コード例 #3
0
/**
*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
    shuffleDeck($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>";
}