我有一个匹配的纸牌游戏,到目前为止非常简单,我只有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
答案 0 :(得分: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.";
}
}