Jump to content

PHP & Playing Cards :|


Wo0tHigh

Recommended Posts

dan, why don't you give us a rundown of how you are handling your deck? your best bet would be to populate a deck as an array, and then simply use array_shuffle() to mix up the order of the cards. then, you can simply deal them out to the individuals you need.
Link to comment
https://forums.phpfreaks.com/topic/24762-php-playing-cards/#findComment-112747
Share on other sites

Here's 1 way

[code]
<?php
function deal (&$pack, $numhands, $cards_per_hand) {
    $hands = array();
    if ($numhands*$cards_per_hand > 52) return $hands;
    $temp = array_slice ($pack, 0, $numhands*$cards_per_hand);
    $hands = array_chunk($temp, $cards_per_hand);
    return $hands;
}

$vals  = array('2','3','4','5','6','7','8','9','T','J','Q','K','A');
$suits = array('c','d','h','s');

$pack = array();

foreach ($vals as $v) {
    foreach ($suits as $s) {
        $pack[] = "$v$s";
    }
}

shuffle ($pack);

$hands = deal ($pack, 4, 5);  // deal four hands of 5 cards

// show hands

foreach ($hands as $cards) {
    foreach ($cards as $c) {
        echo "$c ";
    }
    echo '<br />';
}
?>[/code]

Typical output
[pre]Kd 5s 3h 7d 5c
Td 8c As Qd Js
Jc Ah 9c 4h Ts
Tc Th 6c 8d 4s [/pre]
Link to comment
https://forums.phpfreaks.com/topic/24762-php-playing-cards/#findComment-112760
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.