6 Replies - 634 Views - Last Post: 25 September 2012 - 01:40 AM Rate Topic: -----

#1 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

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?

  $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>";


Is This A Good Question/Topic? 0
  • +

Replies To: Adding cards from a shuffled deck to 4 hands (arrays)

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,323
  • Joined: 08-June 10

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()).
Was This Post Helpful? 2
  • +
  • -

#3 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Adding cards from a shuffled deck to 4 hands (arrays)

Posted 24 September 2012 - 10:04 AM

View PostDormilich, 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

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,323
  • Joined: 08-June 10

Re: Adding cards from a shuffled deck to 4 hands (arrays)

Posted 24 September 2012 - 11:31 AM

View PostTenderfoot, on 24 September 2012 - 07:04 PM, said:

I was trying to avoid using 4 arrays

well, thatís what I read on line #3.

depending on the situation, it might suffice to know which elements (indices) of the shuffled array belong into each hand.
Was This Post Helpful? 1
  • +
  • -

#5 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Adding cards from a shuffled deck to 4 hands (arrays)

Posted 24 September 2012 - 02:52 PM

View PostDormilich, on 24 September 2012 - 11:31 AM, said:

View PostTenderfoot, on 24 September 2012 - 07:04 PM, said:

I was trying to avoid using 4 arrays

well, thatís what I read on line #3.

depending on the situation, it might suffice to know which elements (indices) of the shuffled array belong into each hand.


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.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,323
  • Joined: 08-June 10

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

Was This Post Helpful? 2
  • +
  • -

#7 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Adding cards from a shuffled deck to 4 hands (arrays)

Posted 25 September 2012 - 01:40 AM

View PostDormilich, 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. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1