Jump to content

Card game "pairs" card sorting


Zephni

Recommended Posts

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...

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.