ios - 重新启动 objective-c 中的对象 - Matchismo 作业 2 - 重新启动游戏

标签 ios objective-c initialization null

我正在尝试解决斯坦福 iOS7 开发中的作业 2(Matchismo 纸牌游戏)

游戏运行良好。现在我必须添加重启功能。如果用户按下重启按钮,游戏将重新开始(发新牌并重置分数)

我的游戏模型是@property(非原子,强)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 在我的情况下不起作用吗?

最佳答案

- (IBAction)startover:(UIButton *)sender {

    self.game= [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons count] usingDeck:[self createDeck]];

    [self updateUI];
}

关于ios - 重新启动 objective-c 中的对象 - Matchismo 作业 2 - 重新启动游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23830502/

相关文章:

ios - 如何从 UIImagePickerController 获取最长持续时间的视频

java - 将最大的产品计数器初始化为 "infinitely"小数

c - C 中已声明但未初始化的变量会发生什么情况?它有值(value)吗?

ios - 嵌入式导航栏在运行时不显示。

ios - 将 uint8_t 转换为 NSString

ios - 检测具有不同主要值和次要值的 iBeacon

objective-c - 运行奇怪的错误运行 objective-c 代码

ios - 如何使用 Swift 3 生成 RSA 公钥和私钥?

ios - 在本地安全存储应用程序购买数据

c# - 访问前可能未初始化 Out 参数