3

我有一个匹配的纸牌游戏,到目前为止它非常简单,我只有 1 个视图控制器和 2 个 UILabel。1 个 UILabel 用于得分,1 个 UILabel 用于翻转。

现在我想创建另一个 UILabel 让用户知道何时有匹配并呈现匹配的卡片内容。我正在试图弄清楚如何获得匹配的两张卡..

这是我的控制器:(如果您需要查看另一个文件,请告诉我,或者您可以根据这个文件提供帮助,理论上的答案也可以)

CardGameViewController.m

#import "CardGameViewController.h"
#import "PlayingCardsDeck.h"
#import "CardMatchingGame.h"


@interface CardGameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *cardLabel; //creating a label propery to update counts
@property (nonatomic) int flipsCount; //creating a NSUInteger property to count the flips
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@property (strong, nonatomic) CardMatchingGame *game;
@property (weak, nonatomic) IBOutlet UILabel *scoreCounter;
@property (nonatomic) int userScore;

@end


@implementation CardGameViewController


-(CardMatchingGame *) game {

    if (!_game) _game = [[CardMatchingGame alloc] initWithCardCount:self.cardButtons.count usingDeck:[[PlayingCardsDeck alloc] init]];
    return _game;
}


-(void) setCardButtons:(NSArray *)cardButtons {

    _cardButtons = cardButtons;
   [self updateUI];
}


//Here I implemented the setter for the flipCount propert. Whick is setting the cardLabel to the right text and adding the number of counts.
-(void) setFlipsCount:(int)flipsCount {

    _flipsCount = flipsCount;
    self.cardLabel.text = [NSString stringWithFormat:@"Flips: %d", self.flipsCount];

}


-(void) updateUI {

    for (UIButton *cardButton in self.cardButtons) {
        Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]];
        [cardButton setTitle:card.contents forState:UIControlStateSelected];
        [cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled];
        cardButton.selected = card.isFaceUp;
        cardButton.enabled = !card.unplayble;
        if (card.unplayble) {
            cardButton.alpha = 0.1;
        }
        self.scoreCounter.text = [NSString stringWithFormat:@"Score: %d", self.game.score];

    }
}


//Here I created a method to flipCards when the card is selected, and give the user a random card from the deck each time he flips the card. After each flip i'm incrementing the flipCount setter by one.
- (IBAction)flipCard:(UIButton *)sender {

    [self.game flipCardAtIndex:[self.cardButtons indexOfObject:sender]];
    self.flipsCount++;
    [self updateUI];
}


@end

这是 CardMatchingGame 的模型:

#import "CardMatchingGame.h"
#import "PlayingCardsDeck.h"

@interface CardMatchingGame()

@property (readwrite, nonatomic) int score;
@property (strong, nonatomic) NSMutableArray *cards;

@end        


@implementation CardMatchingGame


-(NSMutableArray *) cards {

    if (!_cards) _cards = [[NSMutableArray alloc] init];
    return _cards;
}


-(id)initWithCardCount:(NSUInteger)count usingDeck:(Deck *)deck {

    self = [super init];

    if (self) {

        for (int i = 0; i < count; i++) {
            Card *card = [deck drawRandonCard];

            if (!card) {
                self = nil;
            } else {
                self.cards[i] = card;
            }
        }
    }
    return self;
}


-(Card *) cardAtIndex:(NSUInteger)index {

    return (index < self.cards.count) ? self.cards[index] : nil;
}


#define FLIP_COST 1
#define MISMATCH_PENALTY 2
#define BONUS 4

-(void) flipCardAtIndex:(NSUInteger)index {

    Card *card = [self cardAtIndex:index];

    if (!card.isUnplayable) {

        if (!card.isFaceUp) {

            for (Card *otherCard in self.cards) {

                if (otherCard.isFaceUp && !otherCard.isUnplayable) {

                   int matchScore = [card match:@[otherCard]];

                    if (matchScore) {

                        otherCard.unplayble = YES;
                        card.unplayble = YES;

                        self.score += matchScore * BONUS;
                    } else {
                        otherCard.faceUp = NO;
                        self.score -= MISMATCH_PENALTY;
                    }
                    break;
                }

            }
            self.score -= FLIP_COST;
        }
        card.faceUp = !card.isFaceUp;

    }
}


@end
4

1 回答 1

0

首先创建您的标签:

 @property (weak, nonatomic) IBOutlet UILabel *matchInfo;

然后更改以下函数,使其返回 BOOL:

-(BOOL)flipCardAtIndex:(NSUInteger)index { //Return type changed from void to BOOL

Card *card = [self cardAtIndex:index];

if (!card.isUnplayable) {

    if (!card.isFaceUp) {

        for (Card *otherCard in self.cards) {

            if (otherCard.isFaceUp && !otherCard.isUnplayable) {

               int matchScore = [card match:@[otherCard]];

                if (matchScore) {

                    otherCard.unplayble = YES;
                    card.unplayble = YES;

                    self.score += matchScore * BONUS;

                } else {
                    otherCard.faceUp = NO;
                    self.score -= MISMATCH_PENALTY;

                }
                break;
            }

        }
        self.score -= FLIP_COST;
    }
    card.faceUp = !card.isFaceUp;

}
return matchScore;

}

最后我们返回 matchScore,这样我们的视图控制器就会知道结果,然后我们可以像这样使用这个信息。

- (IBAction)flipCard:(UIButton *)sender {

if([self.game flipCardAtIndex:[self.cardButtons indexOfObject:sender]]){
    self.flipsCount++;
    [self updateUI];
    self.matchInfo.text = @"You have a match";
}
else{
    self.matchInfo.text = @"Sorry bro.";
}


}
于 2013-03-19T13:57:49.603 回答