Jump to content


Photo

PHP & Playing Cards :|


  • Please log in to reply
2 replies to this topic

#1 Wo0tHigh

Wo0tHigh
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 22 October 2006 - 06:54 PM

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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 22 October 2006 - 06:58 PM

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 22 October 2006 - 07:39 PM

Here's 1 way

<?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 />';
}
?>

Typical output
[pre]Kd 5s 3h 7d 5c
Td 8c As Qd Js
Jc Ah 9c 4h Ts
Tc Th 6c 8d 4s [/pre]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users