Пример #1
0
//var_dump($deck);
function countCards($deck)
{
    //while count recursive will count the total amount of ALL elements in deck array, including suits, I subtract the amount of suits from the total
    $count = count($deck, COUNT_RECURSIVE) - count($deck);
    return $count;
}
/*
* Bring in your createDeck and dealCards function
  For the specified number of players below, assign each one an even set of cards.
  We will do this by counting out how many players there are, counting how many cards
  are in the deck. then dividing them so we know how many cards each player should get
*/
$num_players = 4;
//already in code from the start
$num_cards_in_deck = countCards($deck);
//echo $num_cards_in_deck; //proves my countCards function works
$num_cards_to_give_each_player = $num_cards_in_deck / $num_players;
//echo $num_cards_to_give_each_player; //proves my equation to determine the amount of cards to deal works
/*
  use a for loop to add the "dealt hands" to the $players array
*/
$players = array();
for ($i = 1; $i <= $num_players; $i++) {
    $players[$i] = dealCards($deck, $num_cards_to_give_each_player);
}
//var_dump($players);
/*
               lets create a simple game
               each player will play a card and whoever has the highest value wins. if there are 2 cards played
               that have the same value everybody loses and that round is now a draw.
Пример #2
0
function makeGame()
{
    session_destroy();
    session_start();
    //set game stat
    $_SESSION['blackjack_state'] = "in_game";
    //create deck
    $_SESSION['blackjack_deck'] = array("2-c", "3-c", "4-c", "5-c", "6-c", "7-c", "8-c", "9-c", "10-c", "j-c", "q-c", "k-c", "a-c", "2-d", "3-d", "4-d", "5-d", "6-d", "7-d", "8-d", "9-d", "10-d", "j-d", "q-d", "k-d", "a-d", "2-h", "3-h", "4-h", "5-h", "6-h", "7-h", "8-h", "9-h", "10-h", "j-h", "q-h", "k-h", "a-h", "2-s", "3-s", "4-s", "5-s", "6-s", "7-s", "8-s", "9-s", "10-s", "j-s", "q-s", "k-s", "a-s");
    //shuffle deck
    shuffle($_SESSION['blackjack_deck']);
    //temp counter
    $count = 0;
    //deal player cards
    for ($x = 0; $x < 2; $x++) {
        $_SESSION['blackjack_player'][] = $_SESSION['blackjack_deck'][$count];
        $_SESSION['blackjack_dealer'][] = $_SESSION['blackjack_deck'][$count + 1];
        $count = $count + 2;
    }
    //display players cards
    for ($x = 0; $x < 2; $x++) {
        $temp = explode('-', $_SESSION['blackjack_player'][$x]);
        $_SESSION['blackjack_player_cards'][] = $temp[0];
        $_SESSION['blackjack_player_suits'][] = $temp[1];
    }
    //deal dealers card
    for ($x = 0; $x < 2; $x++) {
        $temp = explode('-', $_SESSION['blackjack_dealer'][$x]);
        $_SESSION['blackjack_dealer_cards'][] = $temp[0];
        $_SESSION['blackjack_dealer_suits'][] = $temp[1];
    }
    //check if dealer was dealt blackjack
    if (countCards($_SESSION['blackjack_dealer_cards']) == 21) {
        echo "Dealer Won. He was dealt blackjack...";
    } else {
        $_SESSION['blackjack_card_count'] = 4;
    }
}