ios - How to compare between three objects in array? -


in card matching game (following stanford course) required create uiswitch change game mode between matching 2 cards 3 cards matching, have matching method looks this:

-(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; } 

it's array, i'm checking between 2 cards. how can improve method check 3 too, or create separate one?

this method checking cards have been flipped:

-(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) {              (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 %@", card.contents, othercard.contents];                     }                     break;                 }              }             self.score -= flip_cost;         }         card.faceup = !card.isfaceup;      } } 

thanks.

i assume know index of cards trying match. if have 3 indexes make matching function return bool value. can use nested if test third card. this.

if([self match:index1 card2:index2]){     if(self match:index1 card2:index3){         nslog(@"you have match");     } } else nslog@"no match"; 

your matching function like:

-(bool)match:(int)index1 card2:(int)index2{     //do matching here , return if yes or no accordingly } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -