$handTeljari = 0;
$hendur = array("Hand 1", "Hand 2", "Hand 3", "Hand 4");
$hands = array("Hand 1" => array(), "Hand 2" => array(), "Hand 3" => array(), "Hand 4" => array());
$HendiTilAdBaetaVid = $hendur[$handTeljari];
for ($i = 0; $i < count($deck); $i++) //Deck has 52 shuffled cards
{
if ((($i + 1) % 13) == 0) //Each hand in bridge has 13 cards
{
$handTeljari++; //Will use this to point to the key of the next hand in the hand array
$hendiTilAdBaetaVid = $hendur[$handTeljari]; //Here I use the aforementioned variable to point to Hand 2, 3, and eventually 4.
}
array_unshift($hands[$hendiTilAdBaetaVid], $deck[$i]); //Attempt to add a card from deck to $hands["Hands 1"] (and the rest).
//$hands is an associative array with a key and an array
}
echo "<pre>";
print_r($hands);
echo "</pre>";
6 Replies - 387 Views - Last Post: 25 September 2012 - 01:40 AM
#1
Adding cards from a shuffled deck to 4 hands (arrays)
Posted 24 September 2012 - 08:22 AM
Below is my code with some commentations. I'm attempting to add cards from a shuffled deck to an array of hands. The array contains 4 hands as keys, and each has a value of an array, in which I want to stuff the cards from the shuffled deck. I'm not quite sure what I should do here, how can I reference the value-array?
Replies To: Adding cards from a shuffled deck to 4 hands (arrays)
#2
Re: Adding cards from a shuffled deck to 4 hands (arrays)
Posted 24 September 2012 - 09:16 AM
If you’re to distribute the cards equally to the hands (13 each), I’d shuffle the array and then splice it into 4 parts (using array_splice()).
#3
Re: Adding cards from a shuffled deck to 4 hands (arrays)
Posted 24 September 2012 - 10:04 AM
Dormilich, on 24 September 2012 - 09:16 AM, said:
If you’re to distribute the cards equally to the hands (13 each), I’d shuffle the array and then splice it into 4 parts (using array_splice()).
I was trying to avoid using 4 arrays - thought it'd be better or well, mostly I thought it would look better. But what I'm doing may just be complicating things. I'll look into array_splice, thanks
This post has been edited by Tenderfoot: 24 September 2012 - 10:05 AM
#4
Re: Adding cards from a shuffled deck to 4 hands (arrays)
Posted 24 September 2012 - 11:31 AM
#5
Re: Adding cards from a shuffled deck to 4 hands (arrays)
Posted 24 September 2012 - 02:52 PM
Dormilich, on 24 September 2012 - 11:31 AM, said:
Yeah, sounds like it'd work for some. Would be a bit more comfortable me for the cards to be located in separate arrays. I intend to sort the hand later. Oh and I have gotten it to work with 4 array_splice statements, but didn't think it was pretty enough (I'm terrible) - but why doesn't the following example work?
$hand1 = $hand2 = $hand3 = $hand4 = array();
$hands = array($hand1, $hand2, $hand3, $hand4);
foreach ($hands as $hand => $array)
{
$array = array_splice($deck, 0, 13);
}
When I print_r for $hands all I get is 4 empty arrays. When I print_r for $deck I see that it is empty, which means array_splice at least threw the cards out. It seems like it should work to me, but that may be my inexperience talking.
#6
Re: Adding cards from a shuffled deck to 4 hands (arrays)
Posted 24 September 2012 - 10:34 PM
AFAIK, foreach() operates on a copy of your array. hence your empty array stays empty. I wouldn’t predefine all those arrays, I’d do it on-the-fly:
$hands = array();
for ($i = 4; $i--;)/>
{
$hands[] = array_splice($deck, 0, 13);
}
var_dump($hands);
#7
Re: Adding cards from a shuffled deck to 4 hands (arrays)
Posted 25 September 2012 - 01:40 AM
Dormilich, on 24 September 2012 - 10:34 PM, said:
AFAIK, foreach() operates on a copy of your array. hence your empty array stays empty.
Ahh. I just tested it now and it worked as I passed $array as a reference. Your way does look better though so I'm going to attempt to implement that. Thanks again for helping me out.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|