Jump to content

get numbers from array until


mikem562

Recommended Posts

I have a cards array, and I have this function which will shuffle the cards for me. How can I edit this to 'deal' 12cards (6 & 6) until all the cards are a specific 6 & 6 I pick out?

 

   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));
     }
    }
   }

Link to comment
Share on other sites

I'm not really following what you mean by "How can I edit this to 'deal' 12cards (6 & 6) until all the cards are a specific 6 & 6 I pick out?". Are you wanting to run the function over and over until, say, you get one hand with two pair and the other with four of a kind? Sounds, like you are wanting to cheat the game.

 

Anyway, You are over complicating this whle script. For example, to "split" the array into two arrays you use this inefficient looping logic:

     for($z=0;$z<$arraySize;$z++)
     {
      if($z<$cutLocation)
       array_push($firstArray, $cardsArray[$z]);
      else
       array_push($secondArray, $cardsArray[$z]);
     }

 

When instead you could simply use array_chunk() or array_slice()

$firstArray = array_slice($cardsArray, 0, $cutLocation);
$secondArray = array_slice($cardsArray, $cutLocation);

 

But, why would you split the deck into two stacls and alternate cards between the two? When you cut a deck you should put the bottom half on top of the deck and deal from the top.

 

 

Anyway, I'm really not going to try and read your code and give advice on the rest since you don't bother to put in comments for the mojority of the code. It's too laborious a process to try and determine the intent of the code without comments. So, I wouldn't even be able to suggest a solution for what you are asking since I have no clue what is happening where.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.