I'm currently working on a matching cards game in Sprite Kit, where I am creating a 3x3 grid with one set of randomly selected cards on the left, and another 3x3 grid for a set of randomly selected cards on the right.
The idea is that the each card on the left grid should have a matching card on the right grid. Note that the set of cards on the right, though matching are actually different sprite nodes; the cards on the left show images, the cards on the right show a corresponding letter.
I create a grid like so (please excuse the variable names, it's for a music related game):
Creating one grid of cards:
for (j = 0; j < 3; j++)
{
for (i = 0; i < 3; i ++)
{
int randomCard = arc4random_uniform(8);
switch (randomCard)
{
case CardC:
staveCard = [SKSpriteNode spriteNodeWithImageNamed:@"Card_C"];
staveCard.name = @"0";
break;
case CardD:
staveCard = [SKSpriteNode spriteNodeWithImageNamed:@"Card_D"];
staveCard.name = @"1";
break;
case CardE:
staveCard = [SKSpriteNode spriteNodeWithImageNamed:@"Card_E"];
staveCard.name= @"2";
break;
case CardF:
staveCard = [SKSpriteNode spriteNodeWithImageNamed:@"Card_F"];
staveCard.name = @"3";
break;
case CardG:
staveCard = [SKSpriteNode spriteNodeWithImageNamed:@"Card_G"];
staveCard.name = @"4";
break;
case CardA:
staveCard = [SKSpriteNode spriteNodeWithImageNamed:@"Card_A"];
staveCard.name = @"5";
break;
case CardB:
staveCard = [SKSpriteNode spriteNodeWithImageNamed:@"Card_B"];
staveCard.name = @"6";
break;
case CardC2:
staveCard = [SKSpriteNode spriteNodeWithImageNamed:@"Card_C2"];
staveCard.name = @"7";
break;
}
grid[j*3+i] = randomCard; // store the stave cards in the grid
staveCard.xScale = 0.5;
staveCard.yScale = 0.5;
staveCard.position = CGPointMake(((i*xGridSize)+120),((j*yGridSize)+120));
[self addChild:staveCard];
}
I do the same thing for the other grid with the other set of SKSpriteNodes.
Now obviously when I run the app, the selection of random cards is successful, but the cards in the right grid don't always correspond with the cards on the left; i.e. not every card always has a matching pair.
My question is: How can I make sure that the cards in the left grid can always have a corresponing, matching card in the right grid?