【发布时间】:2014-05-23 13:27:21
【问题描述】:
我正在尝试解决斯坦福 iOS7 开发(Matchismo 纸牌游戏)的作业 2
游戏运行良好。现在我必须添加重启功能。如果用户按下重启按钮,游戏会重启(它会发新牌并重置分数)
我的游戏模型是@property (nonatomic, strong) CardMatchingGame *game;
这是 CardMatchingGame.m 的代码:
#import "CardMatchingGame.h"
#import "PlayingCardDeck.h"
@interface CardMatchingGame()
@property (nonatomic, readwrite) NSInteger score;
@property (nonatomic, strong) NSMutableArray *cards;
@end
@implementation CardMatchingGame
static const int MATCH_BONUS = 4;
static const int MATCH_PENALTY = 2;
static const int COST_TO_CHOOSE = 1;
-(NSMutableArray *)cards{
if(!_cards) _cards = [[NSMutableArray alloc]init];
return _cards;
}
-(instancetype)initWithCardCount:(NSUInteger)count usingDeck:(Deck *)deck{
self = [super init];
if(self){
for(int i=0; i < count; i++){
Card *card = [deck drawRandomCard];
if(card){
[self.cards addObject:card];
} else{
self = nil;
break;
}
}
}
return self;
}
-(void)chooseCardAtIndex:(NSUInteger)index{
Card *card = [self cardAtIndex:index];
if(!card.isMatched){
if(card.isChosen){
card.chosen = NO;
} else{
for(Card *otherCard in self.cards){
if(otherCard.isChosen && !otherCard.isMatched){
int matchScore = [card match:@[otherCard]];
if(matchScore){
self.score += matchScore * MATCH_BONUS;
card.matched = YES;
otherCard.matched = YES;
} else{
self.score -= MATCH_PENALTY;
otherCard.chosen = NO;
}
break;
}
}
self.score -= COST_TO_CHOOSE;
card.chosen = YES;
}
}
}
-(Card *)cardAtIndex:(NSUInteger)index{
return (index < [self.cards count]) ? self.cards[index] : nil;
}
@end
这是我的 CardGameViewController.m:
#import "CardGameViewController.h"
#import "PlayingCardDeck.h"
#import "CardMatchingGame.h"
@interface CardGameViewController ()
@property (nonatomic, strong) Deck *deck;
@property (nonatomic, strong) CardMatchingGame *game;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardsCollection;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@end
@implementation CardGameViewController
@synthesize game = _game;
-(CardMatchingGame *)game{
if(!_game) _game = [[CardMatchingGame alloc] initWithCardCount:[self.cardsCollection count]
usingDeck:self.deck];
return _game;
}
-(Deck *)deck{
if(!_deck) _deck = [[PlayingCardDeck alloc] init];
return _deck;
}
- (IBAction)touchRestartButton:(id)sender {
self.game = nil;
[self updateUI];
}
- (IBAction)touchCardButton:(UIButton *)sender {
int chosenButtonIndex = [self.cardsCollection indexOfObject:sender];
[self.game chooseCardAtIndex:chosenButtonIndex];
[self updateUI];
}
-(void)updateUI{
for(UIButton *cardButton in self.cardsCollection){
int buttonIndex = [self.cardsCollection indexOfObject:cardButton];
Card *card = [self.game cardAtIndex:buttonIndex];
[cardButton setTitle:[self titleForCard:card] forState:UIControlStateNormal];
[cardButton setBackgroundImage:[self backgroundImageForCard:card] forState:UIControlStateNormal];
cardButton.enabled = !card.isMatched;
}
self.scoreLabel.text = [NSString stringWithFormat:@"Score: %d", self.game.score];
}
-(NSString *)titleForCard:(Card *)card{
return card.isChosen ? card.contents : @"";
}
-(UIImage *)backgroundImageForCard:(Card *)card{
return [UIImage imageNamed: card.isChosen ? @"cardfront" : @"cardback"];
}
@end
为了重新开始游戏,我想我应该简单地重新初始化属性 CardMatchingGame *game。
这是我尝试做的,通过设置self.game = nil; 然后它应该在游戏的getter中自动重新初始化。
这确实是我在互联网上找到的解决方案。但是,在我的程序中它不起作用。 *游戏设置为 nil 并且从不恢复,所以当你点击重启时游戏结束。
您能帮我弄清楚为什么self.game = nil 在我的情况下不起作用吗?
【问题讨论】:
-
为什么不自己重新初始化呢?
-
我该怎么做?抱歉,这是我第一次使用 Objective C
-
self.game = [[CardMatchingGame alloc] initWithCardCount:[self.cardsCollection count] usingDeck:self.deck];
-
不幸的是,这也不起作用
标签: ios objective-c initialization null