Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate Dealing card in PHP

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
            )

    )
like image 328
Cypher or KJ Avatar asked Sep 28 '26 02:09

Cypher or KJ


1 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];   
}
like image 115
Taha Paksu Avatar answered Sep 29 '26 15:09

Taha Paksu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!