Wo0tHigh Posted October 22, 2006 Share Posted October 22, 2006 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 gratefulThanks, Dan Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 22, 2006 Share Posted October 22, 2006 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 22, 2006 Share Posted October 22, 2006 Here's 1 way[code]<?phpfunction 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 handsforeach ($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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.