Jump to content

Archived

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

Wo0tHigh

PHP & Playing Cards :|

Recommended Posts

Hello there!

I'm trying to make a script that will deal out random cards to X amount of different hands, but to make it so each card only is dealt once and then stored in the database.

I'm having problems with making it random, yet unique.

If anyone could help me id be grateful

Thanks, Dan

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.