OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何扩展我的方法来检查 2 张卡是否匹配到 3 张匹配的卡? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:40
标题: iphone - 如何扩展我的方法来检查 2 张卡是否匹配到 3 张匹配的卡?

我正在学习斯坦福的类(class),我们必须为应用程序构建一个方法来检查 2 张卡片是否匹配,这就是具有逻辑的模型的样子(查看那里的方法是 flipCardAtIndex):

#import "CardMatchingGame.h"
#import "layingCardsDeck.h"

@interface CardMatchingGame()

@property (readwrite, nonatomic) int score;
@property (strong, nonatomic) NSMutableArray *cards;
@property (strong, nonatomic) NSString *notification;
@end        


@implementation CardMatchingGame


-(NSMutableArray *) cards {

    if (!_cards) _cards = [[NSMutableArray alloc] init];
    return _cards;
}


-(id)initWithCardCountNSUInteger)count usingDeckDeck *)deck {

    self = [super init];

    if (self) {

        for (int i = 0; i < count; i++) {
            Card *card = [deck drawRandonCard];

            if (!card) {
                self = nil;
            } else {
                self.cards[i] = card;
            }
        }
    }
    return self;
}


-(Card *) cardAtIndexNSUInteger)index {

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


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

-(void) flipCardAtIndexNSUInteger)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;
    }
}


@end

这是整个游戏的类模型,得到了实际的匹配方法:

#import "layingCards.h"


@implementation PlayingCards

@synthesize suit = _suit;

//overriding the :match method of cards to give different acore if its only a suit match or a number match
-(int)matchNSArray *)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;

}
//more stuff...

W 已经用数组创建了它,所以我们可以为更多对象扩展它,但现在我想弄清楚如何扩展它:/

这是我的项目的 github https://github.com/NirOhayon/Matchismo

我是 Objective C 的新手,如果你能帮我弄清楚,我会很感激。

非常感谢



Best Answer-推荐答案


您可以使用循环将它们链接起来以检查它们。非常基本的方法。只需遍历每张卡片,并对照您拥有的“ self ”卡片检查它,然后增加分数而不是设置分数。

-(int)matchNSArray *)cardToMatch {

    int score = 0;

    for(int i = 0; i < cardToMatch.count; i++) {
        PlayingCards *aCard = cardToMatch[i];
        if ([aCard.suit isEqualToString: self.suit]) {
            score += 1;
        } else if (aCard.rank == self.rank) {
            score += 4;
        }
    }

    return score;

}

对于flipCardAtIndex: ,我会将其更改为flipCardsAtIndexesNSArray*)indexes,其中indexes 是NSNumber 的NSArray。然后我会运行一个 for 循环检查并删除任何无法播放或面朝上的牌,并通过这些索引处的剩余牌来检查匹配,并检索匹配分数。

告诉您的 View Controller 添加另一张卡取决于您如何设置 View Controller 。您可以在 View Controller 成为委托(delegate)的方法中执行协议(protocol),并通过协议(protocol)方法告诉它切换。它也可能比这更简单,这取决于它如何检查您的卡片模型以决定显示什么,如果它看到三张可用卡片而不是两张,它可以切换。

由于本练习的重点是学习 iOS 编程,我想给你一个良好的开端,你应该自己调整和解决一些问题。我感觉你是编程的新手,如果你是新手,你会惊讶于在你的阶段有多少编程是反复试验。最终它将成为第二天性。

关于iphone - 如何扩展我的方法来检查 2 张卡是否匹配到 3 张匹配的卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15413903/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4