PDA

View Full Version : problem with memory card game code - help?


shadedblue
06-13-2008, 02:48 PM
Hi all, I hope someone can help me with this.

I am creating a simple memory card game with 10 cards. For anyone who's unfamiliar with the concept, the user gets a quick look at the faces on the cards before the cards are flipped face down, and the user then has to try to find pairs of faces (10 cards = 5 pairs). In theory, each time the user plays the game, the face images are populated randomly so it's a new game each time. The problem I am having is that with the code I've written, some of the cards are coming up blank, which means not all 5 faces are represented on the cards. Below is the function I wrote to populate the card faces. I'm on a Mac so please bear with me if the spacing is a bit confusing:

_global.loadCards = function () {
trace ("you are accessing the loadCards function")
var randomUsed = false;
var listOfRandoms = new Array ();
for (i=1; i<=10; i++){
var newRandom = int(Math.random()*11);
if (newRandom == 0){
newRandom = 1;
/*for (j=1; j <= listOfRandoms.length; j++){
if (newRandom == listOfRandoms[j]){
randomUsed = true;
break;
} else {
randomUsed = false;
}
}*/
}
trace ("newRandom is " + newRandom);
listOfRandoms[i] = newRandom;
trace("List of randoms is "+listOfRandoms.length+" long");
trace(listOfRandoms);
/*for (j=1; j <= listOfRandoms.length; j++){
if (newRandom == listOfRandoms[j]){
randomUsed = true;
break;
} else {
randomUsed = false;
}
}*/
if (rabbitCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("rabbitNodding", "animal", 1);
_root["card"+newRandom].clickValue = "rabbit";
rabbitCounter++;
trace ("rabbitCounter is " + rabbitCounter);
} else if (sheepCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("sheepWalking", "animal", 1);
_root["card"+newRandom].clickValue = "sheep";
sheepCounter++;
trace ("sheepCounter is " + sheepCounter);
} else if (raccoonCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("raccoonHopping", "animal", 1);
_root["card"+newRandom].clickValue = "raccoon";
raccoonCounter++;
trace ("raccoonCounter is " + raccoonCounter);
} else if (foxCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("foxJumping", "animal", 1);
_root["card"+newRandom].clickValue = "fox";
foxCounter++;
trace ("foxCounter is " + foxCounter);
} else if (beaverCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("beaverHovering", "animal", 1);
_root["card"+newRandom].clickValue = "beaver";
beaverCounter++;
trace ("beaverCounter is " + beaverCounter);
}
if (randomUsed == true){
i--;
}
}
}


You'll note that there is a second for loop nested within the first. Actually, it is in there twice, and I have it commented out in both places at the moment because I don't know where it is supposed to go, AND it tends to cause flash to crash entirely when I try to test the movie if I uncomment it in either spot. But a friend who's more familiar with AS than I am helped me with this code and he put it in there. I believe it (or something similar) needs to be there somewhere, as it is meant to check that the random number that is generated during an iteration of the first for loop hasn't been used before (which is what determines which card will be populated with which face), but I don't know how to fix it at all.

Any help with this would be much appreciated, as I have a deadline approaching :eek:

Thanks so much in advance...

DiamondDog
06-13-2008, 09:15 PM
In the Gary Rosenzweig book, AS3 Game Programming University there's one of these 'matching card/memory' games. It uses what I'm sure is a simpler way of ensuring that you 'deal' these cards in the way you want.

In outline, it's as follows (I've never worked in AS2, so please excuse the 'pseudocode'.)
var cardsArray = [0,0,1,1,2,2,3,3,4,4]; // two of each (you'll see why in a minute)

for(var i: int = 0; i<10; i++)
{
// pick a random position in cardsArray
var position:int = Math.floor(Math.random()*cardsArray.length);

// note the value in that position
var pickedValue:int = cardsArray[position];

// remove that value from cardsArray
cardsArray.splice[pick,1];

switch(pickedValue) // the next movie we load depends on the value we picked
{
case 0: // rabbit
// make the next movie a rabbit movie
break;

case 1: // sheep
// make the next movie a sheep movie
break;

case 2: // racoon
// make the next movie a racoon movie
break;

case 3: // fox
// make the next movie a fox movie
break;

case 4: // beaver
// make the next movie a beaver movie
break;
} // end switch

} // end for loop

Because of the way we defined cardsArray right at the start, with two each of 0,1,2,3,4, you're guaranteed to get two of each movie, but in a random order. Taking the picked value out of cardsArray reduces the length of cards array, so the random numbers are being picked from a smaller range each time through the loop.

Also, you don't have any random numbers 'wasted' or have to go to all that bother of checking whether a random number's been used already.

You may find this is a simpler way to do things.

Let me know if this doesn't work, or you need further help.

Good Luck.

shadedblue
06-14-2008, 05:07 PM
This is probably a stupid question, but I'm working in Flash 8...can I use AS3 in Flash 8?

DiamondDog
06-14-2008, 05:43 PM
Not a stupid question, but the answer is no. You can't use AS3 in Flash 8.

What I was trying to do was outline a structure you could use. Never having used AS2 I couldn't give you the exact code but I don't think there's much in that code that couldn't be fairly easily translated into AS2.

I'm assuming there are Switch statements in AS2, to make a choice depending on which of several values a variable has? And you can use 'splice' to remove elements from an array? If not, there must be other, similar functions.

If you wanted to use that approach and got stuck on how to do a particular part in AS2, post another question and I'm sure someone can sort it out for us.