Jump to content

PHP & Playing Cards :|


Wo0tHigh

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.