0

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?

  • Please don't repost. [My question never got answered; what can I do?](http://meta.stackoverflow.com/q/251422) – jscs Mar 21 '16 at 20:48
  • @Josh Caswell The way I was doing things has changed a fair bit since I posted that last question, so I thought it would be worth reposting. Will delete the previous question. – Andrew Smith Mar 21 '16 at 21:12

2 Answers2

0

Just make the right card at the same time you make the left one.
You could easilly random choose the position for the right one if you don't want them to have the same position.

Cing
  • 797
  • 1
  • 9
  • 29
  • Thanks for the feedback! I've tried doing it this way before, but I'm not very sure how I'd randomise the position of the right grid. Do you have any suggestions? @Cing – Andrew Smith Mar 21 '16 at 21:20
  • Randomizing the position is "shuffling". A simple algorithm for that is the "Knuth Shuffle" for (i = 8; i >=0; i--) { int rand = arc4_random(i); tempCard1 = grid[i]; tempCard2 = grid[rand]; grid[rand] = tempCard1; grid[i] = tempCard2;} – Good Doug Mar 22 '16 at 14:42
0

You appear to be using all the "cards" that are available every time, so make two parallel arrays, one holding the data for the left side and one for the right side, and then shuffle them.

Then assign the two arrays to grid positions, in order.

If you're not using all the cards, you still build your arrays in parallel, by selection from somewhere.

Community
  • 1
  • 1
jscs
  • 63,095
  • 13
  • 148
  • 192