내가 스탠포드 iOS7에 개발 (Matchismo 카드 게임)다시 시작 객체 - Matchismo 할당 2 - 다시 게임
게임은 잘 작동에서 할당이를 해결하기 위해 노력했다. 이제 Restart 기능을 추가해야합니다. 다시 시작 버튼을 사용자 프레스, 게임 다시 시작 (그것은 새로운 카드를 거래하며 점수 재설정)
내 게임 모델이이 CardMatchingGame.m에 대한 코드입니다 @property (nonatomic, strong) CardMatchingGame *game;
경우 :
을 여기#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
, 나는 단순히 속성 CardMatchingG을 다시 초기화해야한다고 생각 ame * 게임.
이것은 내가 시도한 것입니다. self.game = nil;
다음은 게임의 게터에서 자동으로 다시 초기화되어야합니다.
실제로 인터넷에서 발견 한 해결책입니다. 그러나, 내 프로그램에서 작동하지 않습니다. * 게임은 nil로 설정되어 복원되지 않으므로 다시 시작을 클릭하면 게임이 종료됩니다.
필자의 경우 self.game = nil
이 작동하지 않는 이유를 알아 내도록 도와 주시겠습니까?
왜 직접 재 초기화하지 않습니까? –
어떻게해야합니까? 미안하지만 객관적인 C로 처음이다. – user3669039
self.game = [[CardMatchingGame alloc] initWithCardCount : [self.cardsCollection count] usingDeck : self.deck]; –