2

私はスタンフォード大学のコースをたどっています。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です

私はObjectiveCを初めて使用するので、理解するのを手伝っていただければ幸いです。

本当にありがとう

4

1 に答える 1

1

これらをループで連鎖させて、すべてをチェックすることができます。それを行う非常に基本的な方法。各カードをループして、持っている「自己」カードと照合し、スコアを設定する代わりにスコアを増やします。

-(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がデリゲートになるメソッドでプロトコルを実行し、プロトコルメソッドを介して切り替えるように指示できます。また、カードのモデルをチェックして何を表示するかを決定する方法に応じて、2 枚ではなく 3 枚のカードが利用可能であると判断した場合は、切り替えることができます。

この演習のポイントは iOS プログラミングを学ぶことなので、良いスタートを切りたいと思います。あなたはプログラミングの初心者だと思います。もしそうなら、あなたの段階でのプログラミングがどれだけ試行錯誤しているかに驚かれることでしょう。最終的にはそれが第二の性質になります。

于 2013-03-14T16:05:21.850 に答える