2013-03-12 51 views
2

在我的卡配對遊戲(以下斯坦福課程)創建一個UISwitch,這將改變匹配兩張牌三張牌匹配的遊戲模式我需要的,現在我已經有一個匹配方法,看起來像這樣:如何比較數組中的三個對象?

-(int)match:(NSArray *)cardToMatch { 

    int score = 0; 

    if (cardToMatch.count == 1) { 
     PlayingCards *aCard = [cardToMatch lastObject]; 

     if ([aCard.suit isEqualToString: self.suit]) { 
      score = 1; 
     } else if (aCard.rank == self.rank) { 
      score = 4; 
     } 

    } 

    return score; 
} 

它已經是一個數組,但我只在兩張卡之間進行檢查。我該如何改進這種方法來檢查三個,還是創建一個單獨的?

這也是正在檢查已翻牌的方法:

-(Card *) cardAtIndex:(NSUInteger)index { 

    return (index < self.cards.count) ? self.cards[index] : nil; 
} 


#define FLIP_COST 1 
#define MISMATCH_PENALTY 2 
#define BONUS 4 

-(void) flipCardAtIndex:(NSUInteger)index { 



    Card *card = [self cardAtIndex:index]; 

    if (!card.isUnplayable) { 

     if (!card.isFaceUp) { 

      for (Card *otherCard in self.cards) { 

       if (otherCard.isFaceUp && !otherCard.isUnplayable) { 

        int matchScore = [card match:@[otherCard]]; 

        if (matchScore) { 

         otherCard.unplayble = YES; 
         card.unplayble = YES; 

         self.notification = [NSString stringWithFormat:@"%@ & %@ match!", card.contents, otherCard.contents]; 

         self.score += matchScore * BONUS; 
        } else { 
         otherCard.faceUp = NO; 
         self.score -= MISMATCH_PENALTY; 
         self.notification = [NSString stringWithFormat:@"%@ did not matched to %@", card.contents, otherCard.contents]; 
        } 
        break; 
       } 

      } 
      self.score -= FLIP_COST; 
     } 
     card.faceUp = !card.isFaceUp; 

    } 
} 

感謝。

+0

這不回答你的問題,但可能會讓你覺得:你爲什麼要使用字符串比較匹配的西裝?比較字符串是相當「昂貴」的,所以你可能想用一個'enum'來代表正則表達式,因爲比較它們(整數)是微不足道的。 – trojanfoe 2013-03-13 07:51:39

+0

這是一個好主意:)謝謝。你也許也有一些解決我的問題..?即使我用你使用枚舉的建議,我該如何比較3個對象..?讓我瘋狂@trojanfoe – JohnBigs 2013-03-13 07:55:51

+0

,這是我第一次發佈的東西,沒有人迴應.. weird @trojanfoe – JohnBigs 2013-03-13 07:56:34

回答

0

我假設你已經知道你試圖匹配的牌的索引。所以如果你有三個索引,你的匹配函數會返回一個bool值。然後你可以使用一個嵌套的if來測試第三張牌。它看起來像這樣。

if([self match:index1 card2:index2]){ 
    if(self match:index1 card2:index3){ 
     NSLog(@"You have a match"); 
    } 
} 
else [email protected]"No match"; 

您匹配的功能將類似於:

-(BOOL)match:(int)index1 card2:(int)index2{ 
    //Do your matching here and return if YES or NO accordingly 
}