note 92366 added to function.shuffle

From: Date: Tue, 21 Jul 2009 16:08:08 +0000
Subject: note 92366 added to function.shuffle
Groups: php.notes 
Request: Send a blank email to php-notes+get-158552@lists.php.net to get a copy of this message
I wrote these functions just for the heck of it, but I managed to get them to work even though I'm a PHP amateur. They're just two functions for a deck of 52 cards. newDeck() creates a deck and deal() creates hands for the deck. <?php // Two basic playing card functions // newDeck([$numJokers]) - returns array of 52 cards plus (if supplied) $numJokers jokers // deal($deck[, $numHands[, $numCards]]) - returns two compact()ed variables--the new $deck and $hand arrays function newDeck($numJokers = 0) { // Returns $deck: array of 52 standard deck of 4-suit cards plus $numJokers jokers, shuffled (key starts at zero) // in format of e.g. $deck[4] = "A &spades;" = ace of spades (fifth card) $suitCards = array("J", "Q", "K", "A"); $suitCards = array_merge($suitCards, range(2, 10)); $hearts = array(); $spades = array(); $diamonds = array(); $clubs = array(); foreach($suitCards as $card) { $hearts[] = "<span style=\"color: #FF0000;\">" . $card . " &hearts;" . "</span>"; $spades[] = "<span style=\"color: #000000;\">" . $card . " &spades;" . "</span>"; $diamonds[] = "<span style=\"color: #FF0000;\">" . $card . " &diams;" . "</span>"; $clubs[] = "<span style=\"color: #000000;\">" . $card . " &clubs;" . "</span>"; } $jokers = array(); for($i = 0; $i < $numJokers; $i++) $jokers[] = "<span style=\"color: #8467D7;\">JOKER</span>"; $deck = array_merge($hearts, $spades, $clubs, $diamonds, $jokers); shuffle($deck); return $deck; } function deal($deck, $numHands = 1, $numCards = 7) { // Returns $hand: two-dimensional array, first key being player ID and second being card number (both starting from zero) // e.g. $hand[2][4] = "6 &hearts;" = six of hearts (third hand, fifth card) // To return, use extract, e.g. extract(deal($deck, 2, 13)); (also returns $deck) $hand = array(); for($i = 0; $i < $numHands; $i++) { for($j = 0; $j < $numCards; $j++) { $hand[$i][$j] = next($deck); if($i == 0 && $j == 0) $hand[0][0] = prev($deck); } } $deck = array_splice($deck, ($numHands * $numCards)); return compact("hand", "deck"); } $deck = newDeck(); extract(deal($deck, 3, 7)); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- Just an example of test output to demonstrate capability. --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Basic Playing Card Functions</title> </head> <body> <h1>Deck</h1> <pre> <?php print_r($deck); ?> </pre> <hr /> <h1>Hands</h1> <pre> <?php print_r($hand); ?> </pre> </body> </html> ---- Server IP: 69.147.83.197 Probable Submitter: 72.208.240.199 ---- Manual Page -- http://www.php.net/manual/en/function.shuffle.php Edit -- https://master.php.net/note/edit/92366 Del: integrated -- https://master.php.net/note/delete/92366/integrated Del: useless -- https://master.php.net/note/delete/92366/useless Del: bad code -- https://master.php.net/note/delete/92366/bad+code Del: spam -- https://master.php.net/note/delete/92366/spam Del: non-english -- https://master.php.net/note/delete/92366/non-english Del: in docs -- https://master.php.net/note/delete/92366/in+docs Del: other reasons-- https://master.php.net/note/delete/92366 Reject -- https://master.php.net/note/reject/92366 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158552) next »