Re: Shuffle Cards Function
| From: | Richard Lynch | Date: | Sun, 19 Aug 2001 20:45:55 +0000 |
| Subject: | Re: Shuffle Cards Function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-63423@lists.php.net to get a copy of this message | ||
http://php.net/shuffle will be much faster and just as
random...
--
WARNING richard@zend.com address is an endangered species -- Use
ceo@l-i-e.com
Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm
Volunteer a little time: http://chatmusic.com/volunteer.htm
----- Original Message -----
From: Robert Schultz <bert@ncinter.net>
Newsgroups: php.general
To: <php-general@lists.php.net>
Sent: Saturday, August 18, 2001 11:37 PM
Subject: Shuffle Cards Function
> I've written a ShuffleCards function that will actually shuffle a deck of
> cards just like would happen in real life (divide the deck in half and
stick
> them into each other with 1-3 cards between each other card)
>
> function ShuffleCards(&$cardsArray, $times)
> {
> // Randomizes where to split within center (-3 to +3 from dead center)
> (MINIMUM 10 CARDS IN DECK)
> // Splits into two array. Randomizes how many cards from left and how
> many cards from right (between 1 and 3)
> // Alternates sides. Continues until both arrays are empty.
> $arraySize = count($cardsArray);
>
> if($arraySize<10)
> return;
>
> $deadCenter = $arraySize/2;
>
> for($i=0;$i<$times;$i++)
> {
> reset($cardsArray);
>
> $cutVariant = rand(-3, 3);
> $cutLocation = $deadCenter+$cutVariant;
>
> $firstArray = array();
> $secondArray = array();
>
> for($z=0;$z<$arraySize;$z++)
> {
> if($z<$cutLocation)
> array_push($firstArray, $cardsArray[$z]);
> else
> array_push($secondArray, $cardsArray[$z]);
> }
>
> $bottomFirst = rand(0, 1);
> $cardsArray = array();
>
> while(count($firstArray) && count($secondArray))
> {
> $leftNewCards = array();
> $rightNewCards = array();
>
> $leftVariant = rand(1, 3);
> if($leftVariant>count($firstArray))
> $leftVariant = count($firstArray);
>
> $rightVariant = rand(1, 3);
> if($rightVariant>count($secondArray))
> $rightVariant = count($secondArray);
>
>
> for($x=0;$x<$leftVariant;$x++)
> {
> array_push($leftNewCards, array_shift($firstArray));
> }
>
> for($y=0;$y<$rightVariant;$y++)
> {
> array_push($rightNewCards, array_shift($secondArray));
> }
>
> if($bottomFirst==0)
> {
> $newCardsArray = array_merge($leftNewCards, $rightNewCards);
> $bottomFirst = 1;
> }
> else
> {
> $newCardsArray = array_merge($rightNewCards, $leftNewCards);
> $bottomFirst = 0;
> }
>
> reset($newCardsArray);
>
> while(count($newCardsArray))
> {
> array_push($cardsArray, array_shift($newCardsArray));
> }
> }
>
> if(count($firstArray))
> {
> while(count($firstArray))
> array_push($cardsArray, array_shift($firstArray));
> }
> if(count($secondArray))
> {
> while(count($secondArray))
> array_push($cardsArray, array_shift($secondArray));
> }
> }
> }
>
> Robert Schultz - bert@ncinter.net
>
>
>