note 103161 modified in function.shuffle by danbrown
| From: | danbrown@php.net | Date: | Tue, 29 Mar 2011 18:13:41 +0000 |
| Subject: | note 103161 modified in function.shuffle by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178310@lists.php.net to get a copy of this message | ||
Copy and paste this script and refresh the page to see the shuffling effect.
<?php
/**
* Shuffles and displays cards in a deck
* @author: Eric Anderson
* @filename: deckofcards.php
*/
// Create an array of face values
// and an array of card values
// then merge them together
$cards = array_merge(array("J", "Q", "K", "A"),
range(2,10)); // 13 cards
// Shuffle the cards
shuffle($cards);
// Create an multidimentional array to hold the 4 suits
$suits = array(
'Heart' => array(),
'Spade' => array(),
'Diamond' => array(),
'Club' => array()
);
// Add cards to their respective suits
for($i = 0; $i < count($suits); $i++)
{
for($j = 0; $j < count($cards); $j++)
{
$suits['Heart'][$j] = $cards[$j]."<span
style=color:#FF0000;>♥</span>";
$suits['Spade'][$j] = $cards[$j]."♠";
$suits['Diamond'][$j] = $cards[$j]."<span
style=color:#FF0000;>♦</span>";
$suits['Club'][$j] = $cards[$j]."♣";
}
}
// Create a deck
$deck = array();
// Merge the suits into the empty deck array
$deck = array_merge($deck, $suits);
// Display the deck to the screen
echo "<p><b>Deck of cards:</b></p>";
foreach($deck as $k1 => $v1)
{
// Display suit name
echo "<p> $k1's<br /> {<br
/>  ";
$acc = 0;
// Display card value
foreach($v1 as $k2 => $v2)
{
echo "$v2 ";
$acc++;
if ($acc == 4)
{
echo "<br />  ";
$acc = 0;
}
}
echo "<br /> }</p>";
}
?>
--was--
Copy and paste this script and refresh the page to see the shuffling effect.
<?php
/**
* Shuffles and displays cards in a deck
* @author: Eric Anderson
* @filename: deckofcards.php
*/
// Create an array of face values
// and an array of card values
// then merge them together
$cards = array_merge(array("J", "Q", "K", "A"),
range(2,10)); // 13 cards
// Shuffle the cards
shuffle($cards);
// Create an multidimentional array to hold the 4 suits
$suits = array(
'Heart' => array(),
'Spade' => array(),
'Diamond' => array(),
'Club' => array()
);
// Add cards to their respective suits
for($i = 0; $i < count($suits); $i++)
{
for($j = 0; $j < count($cards); $j++)
{
$suits['Heart'][$j] = $cards[$j]."<span
style=color:#FF0000;>♥</span>";
$suits['Spade'][$j] = $cards[$j]."♠";
$suits['Diamond'][$j] = $cards[$j]."<span
style=color:#FF0000;>♦</span>";
$suits['Club'][$j] = $cards[$j]."♣";
}
}
// Create a deck
$deck = array();
// Merge the suits into the empty deck array
$deck = array_merge($deck, $suits);
// Display the deck to the screen
echo "<p><b>Deck of cards:</b></p>";
foreach($deck as $k1 => $v1)
{
// Display suit name
echo "<p> $k1's<br /> {<br
/>  ";
$acc = 0;
// Display card value
foreach($v1 as $k2 => $v2)
{
echo "$v2 ";
$acc++;
if ($acc == 4)
{
echo "<br />  ";
$acc = 0;
}
}
echo "<br /> }</p>";
}
http://php.net/manual/en/function.shuffle.php