Zephni Posted April 6, 2011 Share Posted April 6, 2011 Hey, i have made a silly little tester game just to see if i could, its here: http://www.zephni.com/test/pairs its for some guy who is attending college and he was given the assignment to make a game of pairs using PHP to sort the cards and lay them down, and javascript to handle the actual game. Anyway at the moment, my game sorts out the cards as if they are values from 1 to 52, and picks randomly 18 cards from them. During the game, the cards have to be excatly the same, as in: if(card_picked1 == card_picked2){ win pair } that means that both cards would have to be the same number AND suit. But in pairs you have to pick the same number but obviously in a different suit. Any way I have been trying some diff code to pick 18 cards from the pack. It picks 9, and then doubles them up but +'s 13 to the card so it is the same number card. Here is the code <?php $i = 1; $total_cards = 18; #Sorting out cards $card_sort = range(1,52); while($i <= ($total_cards/2)){ $pick_card = $card_sort[rand(1,52)]; if($pick_card !== null){ $cards[] = $pick_card; } unset($card_sort[$pick_card]); $i = $i+1; } foreach($cards as $card){ echo($card."<br/>"); } ?> I would of thought that when you unset the number from $card_sort that was picked it would'nt choose it again if($pick_card !== null) but it still chooses the same card twice sometimes. I know I really havent explained this well, but i did my best for 9am... Quote Link to comment Share on other sites More sharing options...
spiderwell Posted April 6, 2011 Share Posted April 6, 2011 I vaguely recall doing something similar in asp years ago and I think I stored the 'picked cards' in an array and then each time i picked a new card, i looped through array to check it wasnt already picked. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.