Ios 如何比较数组中的三个对象?

Ios 如何比较数组中的三个对象?,ios,objective-c,cocoa-touch,cocoa,nsarray,Ios,Objective C,Cocoa Touch,Cocoa,Nsarray,在我的卡片匹配游戏(斯坦福课程之后)中,我需要创建一个UISwitch,将两张卡片匹配之间的游戏模式更改为三张卡片匹配,现在我已经有了一个如下的匹配方法: -(int)match:(NSArray *)cardToMatch { int score = 0; if (cardToMatch.count == 1) { PlayingCards *aCard = [cardToMatch lastObject]; if ([aCard.suit

在我的卡片匹配游戏(斯坦福课程之后)中,我需要创建一个
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;

    }
}
-(卡片*)卡片索引:(NSU整数)索引{
返回(索引

谢谢。

我想你已经知道你要匹配的卡片的索引了。因此,如果有三个索引,则使匹配函数返回bool值。然后,您可以使用嵌套if测试第三张卡。它看起来像这样

if([self match:index1 card2:index2]){
    if(self match:index1 card2:index3){
        NSLog(@"You have a match");
    }
}
else NSLog@"No match";
您的匹配函数类似于:

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

这并不能回答您的问题,但可能会让您思考:为什么要使用字符串比较来匹配西装?比较字符串非常“昂贵”,因此您可能希望使用
enum
来表示匹配,因为比较它们(整数)非常简单。这是一个好主意:)谢谢。你对我的问题有什么解决办法吗。。?即使我同意你使用enum的建议,我如何比较3个对象。。?让我发疯的@trojanfoe,这是我第一次发布一些东西,但没有人响应。奇怪的@trojanfoeWell在伪代码中这只是
obj1==obj2&&obj2==obj3
。是的,但是当我尝试做类似于'obj1[0]==obj2[1]'的事情时,我也不能做
obj1[0]。适合
@trojanfoe