gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:56:30 25 4
gpt4 key购买 nike

我正在尝试解决斯坦福 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com