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
https://forums.phpfreaks.com/topic/232845-card-game-pairs-card-sorting/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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