Simulate Dealing card in PHP

Viewed 989

I am making function for poker hand(dealing cards to 'n' players). I am trying to simulate card dealing function in my codeigniter. but I am stuck, because all players getting same card pairs.

Here is my Model function:

$deck = array(
    'AD', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD',
    'AC', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC',
    'AH', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH',
    'AS', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS'
);

shuffle($deck);

$player_card = array();

$cycle = 2;
for ($k=0; $k < $players; $k++) {
    for ($i=0; $i < $cycle; $i++) { 
        $player_card[$k][$i] = $deck[$i];   
    }
}

I am Getting output like this:

this is shuffled deck:

Array
(
    [0] => JC
    [1] => AC
    [2] => 4H
    [3] => 5D
    [4] => 3D
    [5] => QD
    [6] => 7H
    [7] => 8S
    [8] => AD
    [9] => KD
    [10] => 6C
    [11] => KH
    [12] => TS
    [13] => 8D
    [14] => 7S
    [15] => 9C
    [16] => 6D
    [17] => 6S
    [18] => 4S
    [19] => KC
    [20] => 2H
    [21] => 9H
    [22] => 8H
    [23] => 2C
    [24] => AS
    [25] => 7C
    [26] => 3C
    [27] => 2D
    [28] => QS
    [29] => QC
    [30] => JS
    [31] => JH
    [32] => 9D
    [33] => TC
    [34] => 3S
    [35] => 4C
    [36] => 2S
    [37] => 3H
    [38] => JD
    [39] => 5H
    [40] => 6H
    [41] => AH
    [42] => TH
    [43] => TD
    [44] => KS
    [45] => 9S
    [46] => 8C
    [47] => 5S
    [48] => 7D
    [49] => 4D
    [50] => 5C
    [51] => QH
)

this is 'n' Player's card(n = 3)

Array
(
    [0] => Array
        (
            [0] => JC
            [1] => AC
        )

    [1] => Array
        (
            [0] => JC
            [1] => AC
        )

    [2] => Array
        (
            [0] => JC
            [1] => AC
        )

)

What I want in out Put is: want to make turn based assignment of cards to each players.

Array
    (
        [0] => Array
            (
                [0] => JC
                [1] => 5D
            )

        [1] => Array
            (
                [0] => AC
                [1] => 3D
            )

        [2] => Array
            (
                [0] => 4H
                [1] => QD
            )

    )
4 Answers

You've already shuffled the cards, why not simply array_chunk?

shuffle($cards);
$player_card = array_chunk($cards, 13);

http://php.net/manual/tr/function.array-chunk.php

This will output something like this:

Array
(
    [0] => Array
    (
        [0] => 3D
        [1] => 7S
        [2] => AD
        [3] => 9C
        [4] => 5D
        [5] => KS
        [6] => 4H
        [7] => 6S
        [8] => AS
        [9] => TH
        [10] => 4S
        [11] => TS
        [12] => 3H
    )

    [1] => Array
    (
        [0] => AH
        [1] => 5S
        [2] => AC
        [3] => QS
        [4] => 5H
        [5] => 7D
        [6] => JH
        [7] => QD
        [8] => JS
        [9] => 9S
        [10] => 8S
        [11] => 6C
        [12] => 2H
    )

    [2] => Array
    (
        [0] => TD
        [1] => 8H
        [2] => 5C
        [3] => KD
        [4] => 8D
        [5] => QH
        [6] => JC
        [7] => TC
        [8] => 3C
        [9] => 6D
        [10] => 9H
        [11] => 2C
        [12] => 2D
    )

    [3] => Array
    (
        [0] => 7C
        [1] => KH
        [2] => 4D
        [3] => 9D
        [4] => 6H
        [5] => 7H
        [6] => KC
        [7] => QC
        [8] => JD
        [9] => 2S
        [10] => 8C
        [11] => 4C
        [12] => 3S
    )

)

EDIT: If you want to deal the cards one by one to the players;

$player_card = [[],[],[],[]]; // four players
for($i = 0; i < count($deck); $i++){
    $player_card[$i % 4][] = $deck[$i];   
}

This is happening because the logic determining which card to deal is in the inner loop, with the players loop being the outside one. Ergo, for every iteration of the outer loop, you're dealing the same card to each player.

Better would be to scrap your initial shuffle, and instead deal a random card every time, then remove that card from the master array.

$num_cards_to_deal = 2;
for ($k=0; $k < $players; $k++)
    for ($i=0; $i < $num_cards_to_deal; $i++) {
        $card_index = rand(0, count($deck) - 1);
        $player_card[$k][$i] = array_values($deck)[$card_index];
        unset($deck[$card_index]);
    }

This, however, modifies the original $deck, so you might want to first make a copy of it for use in the current round of dealing only.

$this_round_deck = $deck;
//...then replace $deck with $this_round_deck in the above code

The problem is that you are giving out cards to players but never removing the card from the deck.

If you want to make it work like a normal deck of cards, you should always take out the top-most card. The simplest way to do it, is to use array_shift($deck). It will return the first card from the array and remove it. See details.

To make it clean, I would create a function draw

function draw(){
    global $deck;
    return array_shift($deck);
}

You could create a deck class too, it would be even better as you wouldn't need to juggle with global variables, but that's for another post...

Also, if you are afraid of performances (there's no reason to be unless you are playing with a deck of a couple thousands cards), you could use array_pop() instead. It does the same thing but instead of taking the first card, takes the last card. Wouldn't change much but memory wise, it'd be easier on PHP.

Special thanks to @taha Paksu:

array_chunk() wored for me. I managed it according to my need.

Here is the complete model for 'N' players card dealing logic.

class Card_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }


    public function shuffled_deck($type, $players)
    {
        $noraml_deck = $deck = array(
            'AD', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD',
            'AC', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC',
            'AH', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH',
            'AS', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS'
        );



        shuffle($deck);
        $player_hand = array();
        if ($type === 'holdem')
        {

            $card_per_player = 2;
            $chunk = array_chunk($deck, $players);

            for ($j=0; $j < $players; $j++)
            { 
                for ($i=0; $i < $card_per_player; $i++)
                { 
                    array_push($player_hand, $chunk[$i][$j]);
                }
            }
            $player_hand = array_chunk($player_hand, $card_per_player);

        }

        return [$player_hand];

    }
}

OUTPUT:SHUFFLED DECK

[0] => Array
    (
        [0] => 2H
        [1] => 6D
        [2] => 3D
        [3] => 2S
        [4] => 8S
        [5] => KC
        [6] => JC
        [7] => 8H
        [8] => TD
        [9] => 4S
        [10] => 7C
        [11] => KH
        [12] => 6C
        [13] => AC
        [14] => 2D
        [15] => JH
        [16] => 7D
        [17] => 9H
        [18] => 8C
        [19] => AD
        [20] => TC
        [21] => 9S
        [22] => TH
        [23] => 4C
        [24] => 6S
        [25] => 5S
        [26] => 5C
        [27] => 6H
        [28] => 4D
        [29] => KS
        [30] => JD
        [31] => KD
        [32] => TS
        [33] => QH
        [34] => 5H
        [35] => 8D
        [36] => 9C
        [37] => 5D
        [38] => 3H
        [39] => QC
        [40] => 9D
        [41] => 3S
        [42] => 7H
        [43] => QD
        [44] => 3C
        [45] => QS
        [46] => AS
        [47] => 7S
        [48] => AH
        [49] => 4H
        [50] => 2C
        [51] => JS
    )

PLAYERS CARDS: PLAYER = N = 3 here

Array
    (
        [0] => Array
            (
                [0] => 2H
                [1] => 2S
            )

        [1] => Array
            (
                [0] => 6D
                [1] => 8S
            )

        [2] => Array
            (
                [0] => 3D
                [1] => KC
            )

)
Related