我有一个卡片匹配游戏,非常简单。现在,在Card类中有2个BOOL属性“isFaceUp”和“isUnplayable”,游戏正在加载,我可以翻转卡但是它们不会翻转......并且不会匹配。问题可能有两部分,在Logic方法(flipCardAtIndex),匹配方法(匹配:)和视图控制器中。
这是我的代码:
这就是他们宣称:
@interface Card : NSObject
@property (strong, nonatomic) NSString *contents;
@property (nonatomic) BOOL isFaceUp;
@property (nonatomic) BOOL isUnplayable;
- (NSInteger)match:(NSArray *)cardToMatch;
@end
匹配方法:
#import "PlayingCards.h"
@implementation PlayingCards
@synthesize suit = _suit;
//overriding the :match method of cads to give different acore if its only a suit match or a number match
- (NSInteger)match:(NSArray *)cardToMatch {
int score = 0;
BOOL noMisMatchedRank = YES;
BOOL noMisMatchedSuit = YES;
for (PlayingCards *otherCard in cardToMatch) {
noMisMatchedSuit &= [otherCard.suit isEqualToString:self.suit];
noMisMatchedRank &= (otherCard.rank == self.rank);
}
if (noMisMatchedSuit) {
score += 1;
} else if (noMisMatchedRank) {
score += 4;
}
return score;
}
逻辑方法:
#define FLIP_COST 1
#define MISMATCH_PENALTY 2
#define BONUS 4
-(void) flipCardAtIndex:(NSUInteger)index {
NSString *otherCardsString;
Card *card = [self cardAtIndex:index];
if (!card.isFaceUp) {
if (!card.isUnplayable) {
NSMutableArray *otherCards = [[NSMutableArray alloc] init];
for (Card *otherCard in otherCards) {
if (otherCards.count > self.numberOfCardsToPlayWith && otherCard.isFaceUp && !otherCard.isUnplayable) {
[otherCards addObject:otherCard];
}
}
if (otherCards.count == self.numberOfCardsToPlayWith - 1) {
NSInteger matchScore = [card match:otherCards];
if (matchScore) {
for (Card *otherCard in otherCards) {
otherCardsString = otherCard.contents;
card.isUnplayable = YES;
}
card.isUnplayable = YES;
NSInteger points = matchScore * BONUS * self.numberOfCardsToPlayWith;
self.score += points;
self.notification = [NSString stringWithFormat:@"%@ & %@ match!", card.contents, otherCardsString];
} else {
for (Card *otherCard in otherCards) {
otherCard.isFaceUp = NO;
otherCardsString = otherCard.contents;
}
NSInteger panelty = MISMATCH_PENALTY * self.numberOfCardsToPlayWith;
self.score -= panelty;
self.notification = [NSString stringWithFormat:@"%@ did not matched to %@", card.contents, otherCardsString];
}
}
self.score -= FLIP_COST;
}
card.isFaceUp = !card.isFaceUp;
}
}
视图控制器:
#import "CardGameViewController.h"
#import "PlayingCardsDeck.h"
#import "CardMatchingGame.h"
enum CardGame {
twoCardGame,
threeCardGame
};
@interface CardGameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *notificationLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoreCounter;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@property (strong, nonatomic) CardMatchingGame *game;
@property (weak, nonatomic) IBOutlet UISegmentedControl *numberOfCardsToPlayWith;
@end
@implementation CardGameViewController
//creating the getter method that creates a new card game.
- (CardMatchingGame *)game {
if (!_game) {
_game = [[CardMatchingGame alloc] initWithCardCount:self.cardButtons.count
usingDeck:[[PlayingCardsDeck alloc] init]];
_game.numberOfCardsToPlayWith = [self selectNumberOfCardsToPlayWith];
}
return _game;
}
//creating a setter for the IBOutletCollection cardButtons
-(void)setCardButtons:(NSArray *)cardButtons {
_cardButtons = cardButtons;
[self updateUI];
}
-(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.isUnplayable;
if (card.isUnplayable) {
cardButton.alpha = 0.1;
}
//updating the score
self.scoreCounter.text = [NSString stringWithFormat:@"Score: %d", self.game.score];
//if notification in CardMatchingGame.m is no nil, it will be presented
if (self.game.notification) {
self.notificationLabel.text = self.game.notification;
}
}
}
//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 updateUI];
}
//sending an alert if the user clicked on new game button
- (IBAction)newGame:(UIButton *)sender {
UIAlertView* mes=[[UIAlertView alloc] initWithTitle:@"Think about it for a sec..?" message:@"This will start a new game" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[mes show];
}
-(NSUInteger) selectNumberOfCardsToPlayWith {
switch (self.numberOfCardsToPlayWith.selectedSegmentIndex) {
case twoCardGame:
return 2;
break;
case threeCardGame:
return 3;
default:
return 2;
break;
}
}
//preforming an action according to the user choice for the alert yes/no to start a new game
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex]) {
self.game = nil;
for (UIButton *button in self.cardButtons) {
Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:button]];
card.isUnplayable = NO;
card.isFaceUp = NO;
button.alpha = 1;
}
self.notificationLabel.text = nil;
[self updateUI];
}
}
@end