iphone - 如何扩展我检查 2 张卡是否匹配到 3 张匹配的卡的方法?

标签 iphone ios objective-c cocoa-touch cocoa

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

#import "CardMatchingGame.h"
#import "PlayingCardsDeck.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)initWithCardCount:(NSUInteger)count usingDeck:(Deck *)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 *) 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;
    }
}


@end

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

#import "PlayingCards.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)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;

}
//more stuff...

W 已经用一个数组创建了它,所以我们将能够为更多对象扩展它,但现在我正试图弄清楚如何扩展它:/

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

我是 Objective-C 的新手,如果你能帮我弄明白,我将不胜感激。

非常感谢

最佳答案

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

-(int)match:(NSArray *)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: ,我会将其更改为 flipCardsAtIndexes:(NSArray*)indexes,其中 indexes 是 NSNumbers 的 NSArray。然后我会运行一个 for 循环检查并移除所有无法打出或面朝上的牌,并通过这些索引处的剩余牌来检查匹配,并检索匹配分数。

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

由于本练习的目的是学习 iOS 编程,我想给你一个良好的开端,你应该自己调整并弄清楚一些东西。我有一种感觉,您是编程方面的新手,如果您是,您会惊讶于在您的阶段有多少编程是反复试验的。最终它将成为第二天性。

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

相关文章:

ios - UIViewControllers 之间的自定义转换不起作用

iphone - iPhone 中的动态文本字段是否可行?

ios - iPhone 6 Flexbox 包装问题

iphone - 嵌入 UIView 中的 UITextField 不响应触摸事件

ios - 自定义搜索栏

iphone - AVAudioPlayer是否可以循环播放大型音频文件而没有听得见的间隙?

ios - 更改 UIScrollView 中的页数

ios - NSMutableString replaceOccurrencesOfString 替换整个单词

iphone - 删除 UITableView 中 UINavigationController 上方的灰色条

ios - UITableView 异步 UIImage 设置