Game Development Community

Solitaire Game

by Johnathon · in Game Design and Creative Issues · 11/28/2007 (4:39 pm) · 2 replies

So I have started work on a Solitaire game using TGB and was wondering, which would be the better route to go. Load all of the cards at run time, and move them into the needed position, or place them all on the table within TGB. It would be easier to just place them all in TGB but I wasn't sure if it's the best solution.

Another thing I am thinking about is how to go about randomizing the cards. Each card is given a name (Diamonds5, ClubsQ ect) and I access the card in that manor. I need to put some thought on how I will randomize them, but I figured I would generate a random number from 1-52 and place each randomly generated number into an array, then just work through the array to find which order the cards will be placed in the deck after 'shuffling' them.

Example after randomizing:
array[0] = 6 would be Diamond 6
array[1] = 12 would be diamond Queen
array[2] = 15 would be club 2
array[3] = 14 would be club Ace

What do you guys think? Good approach?

#1
11/29/2007 (12:41 pm)
It's much easier to add the cards at run-time rather than adding them to the scene graph by hand. But, there's nothing wrong with either approach.

In terms of shuffling the cards. The fastest way to shuffle cards is to add them to a 52-element array, the iterate through each card, exchanging it's place with a random card (not itself and not it's original position) so that you're guaranteed every card has been moved within the array.

Once the array is shuffled, you can add each card to a SimSet in shuffled order. The SimSet is where you deal cards from (that way you don't have to track which cards are dealt because if they're not in the draw set, they've been dealt).

You can use the static sprites themselves to store their rank and suit in dynamic fields.

Does that answer your questions?
#2
11/29/2007 (3:54 pm)
It helps point me in a good direction, thanks! I used the following code to create my array.

$Deck = new simset();
$DeckBack = "ImageMap"; //backside of the card

function CreateDeck()
{
	for (%face = 0; %face != 4; %face++)
	{
		for (%value = 1; %value != 14; %value++)
		{
			%card = new t2dSceneObject(getCardName(%face, %value))
			{
				defaultconfig = CardDataBlock;
				scenegraph = t2dScene.SceneGraph;
				cardFace = %face;
				cardValue = %value;
				imageMap = "CardBack";
				position = getSlot(0);
			};
			%card.setLayer = 1;
			$Deck.Add(%card);
		}
	}
	
	//randomize the deck
	$DeckSorter = $Deck;
	$Deck = new simset();
	while ($DeckSorter.getCount() > 0)
	{
		%card = $DeckSorter.getObject(getRandom(0, $DeckSorter.getCount() - 1));
		$Deck.add(%card);
		$DeckSorter.Remove(%card);
	}
	
	for (%value = 0; %value != $Deck.getCount(); %value++)
	{
		%card = $Deck.getObject(%value);
		echo("Card: " @ getCardName(%card.cardFace, %card.cardValue));
		%card.setVisible(true);
	}
}

function getImageName(%face, %value)
{
	%img = "Cards";
	
	switch$ (%face)
	{
		case "0": //Hearts
			%img = %img @ "Hearts" @ %value @ $DeckBack;
		case "1": //clubs
			%img = %img @ "Clubs" @ %value @ $DeckBack;
		case "2": //Diamonds
			%img = %img @ "Diamonds" @ %value @ $DeckBack;
		case "3": //Spades
			%img = %img @ "Spades" @ %value @ $DeckBack;
	}
}

function getCardName(%face, %value)
{
	switch$ (%face)
	{
		case "0":
			%img = "Hearts" @ %value;
		case "1":
			%img = "Clubs" @ %value;
		case "2":
			%img = "Diamonds" @ %value;
		case "3":
			%img = "Spades" @ %value;
	}
}