CS193P-2013 Lecture 3

CardMatchingGame

  • .h

    @import Foundation
    #import "Deck.h"
    @interface CardMatchingGame : NSObject
    - (instancetype)initWithCardCount: (NSUInteger)count usingDeck: (Deck *)deck;
    @property (nonatomic, readonly) NSUInteger score;
    - (void)chooseCardAtIndex: (NSUInteger)index;
    - (Card *)cardAtIndex: (NSUInteger)index;
    @end
    
  • .m

    #import "CardMathcingGame.h"
    @interface CardMatchingGame()
    @property (nonatomic, readwrite) NSInteger score;
    @property (nonatomic, strong) NSMutableArray *cards;
    @end
    
    @implementation
    - (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;
    }
    
    - (instancetype)init {
          return nil;
    }
    
    - (Card *)cardAtIndex: (NSUInteger)index {
      return (index < [self.cards count]) ? self.cards[index] : nil;
    }
    
    static const int MISMATCH_PENALTY = 2;
    static const int MATCH_BONUS = 2;
    static const int COST_TO_CHOOSE = 1;
    - (void)chooseCardAtIndex: (NSUInteger)index {
      Card *card = [self cardAtIndex: index];
          if (!card.isMatched) {
              if (card.isChosen) {
                  card.chosen = NO;
            } else {
                  // match against another card
                  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 -= MISMATCH_PENALTY;
                              otherCard.chosen = NO;
                        }
                          break;
                    }
                      
                }
                  self.score -= COST_TO_CHOOSE;
                  card.Chosen = YES;
            }
        }
    }
    @end
    

其他

  • firstObject/lastObject 方法和下標(biāo)訪問的差別在于,這兩種方法當(dāng)越界時(shí)會(huì)返回 nil,而下標(biāo)訪問會(huì)直接崩潰
  • 所有的對(duì)象一定要記得初!始!化!否則默認(rèn)是 nil
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容