1

i want to make a simple memory matching game. this is my following situation: i have a levelselectionviewcontroller and a gameviewcontroller. in my levelselectionviewcontroller i have 10 buttons (10 levels). i want, that when i click on level 1, my gameviewcontroller make 1 row with 2 pairs. when i click on level 10 i want that may gameviewcontroller make 2 rows with 3 pairs for example. my maximum pairs are 11, my maximum rows are 4.

my code in my gameviewcontroller is

.h

#define CARDS_PER_ROW 4
#define NUMBER_OF_PAIRS 2

.m

-(void) generateCardViews {
int positionsLeftInRow = CARDS_PER_ROW;
int j = 0; // j = ROWNUMBER (j = 0) = ROW1, (j = 1) = ROW2...

for (int i = 0; i < [self.gameModel.cards count]; i++) {
    NSInteger value = ((CardModel *)self.gameModel.cards[i]).value;

    CGFloat x = (i % CARDS_PER_ROW) * 121 + (i % CARDS_PER_ROW) * 40 + 285;
    if (j == 1) {
        x += 80; // set additional indent (horizontal displacement)
    }
    if (j == 2) {
        x -= 160;
    }

    CGFloat y = j * 122 + j * 40 + 158;
    CGRect frame = CGRectMake(x, y, 125, 125);




    CardView *cv = [[CardView alloc] initWithFrame:frame andPosition:i andValue:value];

    if (!((CardModel *)self.gameModel.cards[i]).outOfPlay) {
        [self.boardView addSubview:cv];

       if ([self.gameModel.turnedCards containsObject: self.gameModel.cards[i]]) {
            [self.turnedCardViews addObject: cv];
            [cv flip];
        }
    }

    if (--positionsLeftInRow == 0) {
        j++;
        positionsLeftInRow = CARDS_PER_ROW;
        if (j == 1) {
            positionsLeftInRow = CARDS_PER_ROW-1;

        if (j == 2) {
            positionsLeftInRow = CARDS_PER_ROW-2;
        }}
    }
}
}

my code now is working, but i can only change the rows and pairs in my #define macro in my gameviewcontroller. but how can i #define it in my levelselectionviewcontroller? i want a reusable gameviewcontroller for n levels.

i hope my question is understandable.

Sausagesalad
  • 91
  • 10

1 Answers1

0

if i understand what you're writing, you can create those parameters (cards & pairs) as params (instead of constants) in gameviewcontroller.h:

@property (nonatomic, assign) int CARDS_PER_ROW;
@property (nonatomic, assign) int NUMBER_OF_PAIRS;

add to gameviewcontroller.m:

-(id)initWithCards:(int)numCards andPairs:(int)numPairs
{
    self = [Gameviewcontroller init];
    if (self)
    {
        CARDS_PER_ROW = numCards;
        NUMBER_OF_PAIRS = numPairs;
    }
    return self;
}

and in levelselectionviewcontroller.m:

switch (chosenLevel):
    case 1:
    {
        Gameviewcontroller *level = [[Gameviewcontroller alloc] initWithCards: CARDS_PER_ROW andPairs: NUMBER_OF_PAIRS];
        break;
    }
    .
    .
    .
    case 10:
    {
        Gameviewcontroller *level = [[Gameviewcontroller alloc] initWithCards: CARDS_PER_ROW andPairs: NUMBER_OF_PAIRS];
        break;
    }
}
rdurand
  • 7,342
  • 3
  • 39
  • 72
yonivav
  • 994
  • 11
  • 18
  • thank you i changed it now but my gamemodel.m says use of undeclared identifier _NUMBER_OF_ROWS. don't know why? I've imported the gameviewcontroller.h in my gamemodel.m – Sausagesalad May 14 '15 at 06:38
  • its means you are calling an object / parameter / constant called "NUMBER_OF_ROWS", which wasn't defined at all or was defined not in the relevant class. search your project (command shift f ) for "NUMBER_OF_ROWS" and you'll see why this value isn't known there. maybe you delete NUMBER_OF_ROWS from gameviewcontroller.h ?? anyway, if my answer was right - i'll appreciate if you will tag it as the right one. – yonivav May 14 '15 at 06:53
  • in my gamemodel.m i have #import "GameViewController.h" and the error occurs in -(NSMutableArray *) createNewDeck { NSMutableArray *cardDeck = [NSMutableArray array]; for (int i = 0; i < _NUMBER_OF_PAIRS; i++) { [cardDeck addObject:[[CardModel alloc] initWithValue:i]]; [cardDeck addObject:[[CardModel alloc] initWithValue:i]]; } return cardDeck; } – Sausagesalad May 14 '15 at 09:21
  • and in my gameviewcontroller.h i have your properties (@property (nonatomic, assign) int CARDS_PER_ROW; @property (nonatomic, assign) int NUMBER_OF_PAIRS;) – Sausagesalad May 14 '15 at 09:22