代码之家  ›  专栏  ›  技术社区  ›  JohnBigs

在我的纸牌游戏中,无法想出如何在火柴上贴标签

  •  3
  • JohnBigs  · 技术社区  · 13 年前

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

    现在我想创建另一个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
    
    1 回复  |  直到 13 年前
        1
  •  0
  •   Cömert Tülümen    13 年前

    首先创建标签:

     @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.";
    }
    
    
    }