2013-03-11 39 views
3

我有一个匹配的纸牌游戏,目前为止非常简单,我只有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 
+0

目前尚不清楚:您不知道哪些卡匹配,或者您不知道如何显示标签? – 2013-03-11 20:46:56

+0

我知道如何创建一个标签(只是一个简单的像flipscount),并在匹配机制的作品。我试图弄清楚的是如何在匹配时显示另一个标签......当我写作时,我可以看到它不是最好的解释:/我试图想到一个更简单的@HotLicks – JohnBigs 2013-03-11 21:04:23

+0

所以,如果我想显示两张匹配的卡片,那么最好的方法是什么?像创建另一个对象,插入与数组匹配的卡片并显示这两张卡片的内容?你让我..? @HotLicks – JohnBigs 2013-03-11 21:07:20

回答

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所以我们的ViewController将知道结果了,然后我们就可以使用这个信息像这样结束。

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


}