0

この問題で 3 日間立ち往生しています。この質問を書き出すと、自分で問題を解決するよりも時間がかかると思いましたが、これではうまくいきません。できるだけ簡潔かつ論理的に説明しようと思います。Objective-C では、Cocos2D-iPhone 2.0、Xcode 4.6。

私はテキサス ホールデム ゲームを書いていますが、どのハンドがストレート フラッシュとロイヤル フラッシュであるかをゲームプレイ レイヤーに評価させようとしています。

(テキサス ホールデムやポーカー全般に不慣れな方のために説明すると、すべての可能なカードが配られたら、プレイヤーは合計 7 枚のカードから最高の 5 枚のハンドを作る必要があります。この例では、プレイヤーがストレート フラッシュ (同じスートの 5 枚のカードが順番に並んでいる) を持っているかどうかを判断する [例: 7、8、9、10、およびクラブのジャック、またはエース、デュース、3、4 、およびハートの 5 など。「ロイヤル」フラッシュは、単純に最高のストレート フラッシュであり、同じスートのテン、ジャック、クイーン、キング、およびエースです。])

Card クラスについて私が持っているのは、割り当てと移動のためのメソッドだけです。それらがうまく機能していると言って安心します。問題はありませんでした。

私のカードクラスの関連部分は次のとおりです。

Card.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Card : CCSprite {
    BOOL removedFromLayer, reachedDestination, isMoving;
    NSString *imageName;
    NSString *suit;
    NSNumber *rank;
}

@property BOOL removedFromLayer, reachedDestination, isMoving;
@property (nonatomic, retain) NSNumber *rank;
@property (nonatomic, retain) NSString *suit;
@property (nonatomic, retain) NSString *imageName;

// ... movement and animation methods

@end

実装:

#import "Card.h"
@implementation Card
@synthesize removedFromLayer, reachedDestination, isMoving, rank, suit, imageName;
    // ... implementations
@end

NSMutableArray クラスを拡張して、ポーカー ハンドの評価ロジックを含めました。最初に NSMutableArray のサブクラスを作成しようとしましたが、経験のあるプログラマーならわかるように、あまりうまくいきませんでした。コンソールの読み取り値からわかる限り、並べ替え方法は正常に機能しています。今のところ、ストレート フラッシュ方式に固執します。私はそれがかなり醜いことを認めます。(ハ。「かなり醜い」):

#import "NSMutableArray+HandAnalysis.h"
#import "Card.h"

#define cards1And2AreSequential (card1Rank == (card2Rank - 1))
#define cards2And3AreSequential (card2Rank == (card3Rank - 1))
#define cards3And4AreSequential (card3Rank == (card4Rank - 1))
#define cards4And5AreSequential (card4Rank == (card5Rank - 1))
#define cards5And6AreSequential (card5Rank == (card6Rank - 1))
#define cards6And7AreSequential (card6Rank == (card7Rank - 1))

#define cards1And2AreSameSuit (card1Suit == card2Suit)
#define cards2And3AreSameSuit (card2Suit == card3Suit)
#define cards3And4AreSameSuit (card3Suit == card4Suit)
#define cards4And5AreSameSuit (card4Suit == card5Suit)
#define cards5And6AreSameSuit (card5Suit == card6Suit)
#define cards6And7AreSameSuit (card6Suit == card7Suit)

#define firstCardIsAnAce [[firstCard valueForKey:@"rank"] intValue] == 1
#define lastCardIsAKing [[lastCard valueForKey:@"rank"] intValue] == 13
#define secondToLastCardIsAQueen [[secondToLastCard valueForKey:@"rank"] intValue] == 12
#define thirdToLastCardIsAJack [[thirdToLastCard valueForKey:@"rank"] intValue] == 11
#define fourthToLastCardIsATen [[fourthToLastCard valueForKey:@"rank"] intValue] == 10

// clubs (NOTE:  These do not terminate with a semi-colon)
#define firstCardIsAClub [[firstCard valueForKey:@"suit"] isEqualToString:@"club"]
#define lastCardIsAClub [[lastCard valueForKey:@"suit"] isEqualToString:@"club"]
#define secondToLastCardIsAClub [[secondToLastCard valueForKey:@"suit"] isEqualToString:@"club"]
#define thirdToLastCardIsAClub [[thirdToLastCard valueForKey:@"suit"] isEqualToString:@"club"]
#define fourthToLastCardIsAClub [[fourthToLastCard valueForKey:@"suit"] isEqualToString:@"club"]

// diamonds
#define firstCardIsADiamond [[firstCard valueForKey:@"suit"] isEqualToString:@"diamond"]
#define lastCardIsADiamond [[lastCard valueForKey:@"suit"] isEqualToString:@"diamond"]
#define secondToLastCardIsADiamond [[secondToLastCard valueForKey:@"suit"] isEqualToString:@"diamond"]
#define thirdToLastCardIsADiamond [[thirdToLastCard valueForKey:@"suit"] isEqualToString:@"diamond"]
#define fourthToLastCardIsADiamond [[fourthToLastCard valueForKey:@"suit"] isEqualToString:@"diamond"]

// hearts
#define firstCardIsAHeart [[firstCard valueForKey:@"suit"] isEqualToString:@"heart"]
#define lastCardIsAHeart [[lastCard valueForKey:@"suit"] isEqualToString:@"heart"]
#define secondToLastCardIsAHeart [[secondToLastCard valueForKey:@"suit"] isEqualToString:@"heart"]
#define thirdToLastCardIsAHeart [[thirdToLastCard valueForKey:@"suit"] isEqualToString:@"heart"]
#define fourthToLastCardIsAHeart [[fourthToLastCard valueForKey:@"suit"] isEqualToString:@"heart"]

// spades
#define firstCardIsASpade [[firstCard valueForKey:@"suit"] isEqualToString:@"spade"]
#define lastCardIsASpade [[lastCard valueForKey:@"suit"] isEqualToString:@"spade"]
#define secondToLastCardIsASpade [[secondToLastCard valueForKey:@"suit"] isEqualToString:@"spade"]
#define thirdToLastCardIsASpade [[thirdToLastCard valueForKey:@"suit"] isEqualToString:@"spade"]
#define fourthToLastCardIsASpade [[fourthToLastCard valueForKey:@"suit"] isEqualToString:@"spade"]


@implementation NSMutableArray (HandAnalysis)

-(NSMutableArray *)sortByRank {

    NSSortDescriptor *rankSD = [[NSSortDescriptor alloc] initWithKey:@"rank" ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObject:rankSD];
    NSArray *sortedHand = [self sortedArrayUsingDescriptors:sortDescriptors];
    NSMutableArray *handOrganizedByRank = [NSMutableArray arrayWithArray:sortedHand];

    for (Card* card in handOrganizedByRank) {
        NSLog(@"%@", [card valueForKey:@"rank"]);
    }

    return handOrganizedByRank;
}

-(NSMutableArray *)sortBySuit {

    NSSortDescriptor *suitSD = [[NSSortDescriptor alloc] initWithKey:@"suit" ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObject:suitSD];
    NSArray *sortedHand = [self sortedArrayUsingDescriptors:sortDescriptors];
    NSMutableArray *handOrganizedBySuit = [NSMutableArray arrayWithArray:sortedHand];

    for (Card* card in handOrganizedBySuit) {
        NSLog(@"%@", [card valueForKey:@"suit"]);
    }

    return handOrganizedBySuit;
}

-(BOOL)isStraightFlush {

    [self sortByRank];
    [self sortBySuit];

    NSLog(@"Amount of cards in the array:  %d", [self count]);

    // this section is for if there are 7 cards in the array total.
    if ([self count] == 7) {

        int card1Rank = [[[self objectAtIndex:0] valueForKey:@"rank"] intValue];
        int card2Rank = [[[self objectAtIndex:1] valueForKey:@"rank"] intValue];
        int card3Rank = [[[self objectAtIndex:2] valueForKey:@"rank"] intValue];
        int card4Rank = [[[self objectAtIndex:3] valueForKey:@"rank"] intValue];
        int card5Rank = [[[self objectAtIndex:4] valueForKey:@"rank"] intValue];
        int card6Rank = [[[self objectAtIndex:5] valueForKey:@"rank"] intValue];
        int card7Rank = [[[self objectAtIndex:6] valueForKey:@"rank"] intValue];

        id card1Suit = [[self objectAtIndex:0] valueForKey:@"suit"];
        id card2Suit = [[self objectAtIndex:1] valueForKey:@"suit"];
        id card3Suit = [[self objectAtIndex:2] valueForKey:@"suit"];
        id card4Suit = [[self objectAtIndex:3] valueForKey:@"suit"];
        id card5Suit = [[self objectAtIndex:4] valueForKey:@"suit"];
        id card6Suit = [[self objectAtIndex:5] valueForKey:@"suit"];
        id card7Suit = [[self objectAtIndex:6] valueForKey:@"suit"];

        // if all cards (1-7) are straight flush
        if ((cards1And2AreSequential && cards2And3AreSequential && cards3And4AreSequential && cards4And5AreSequential && cards5And6AreSequential && cards6And7AreSequential && cards1And2AreSameSuit && cards2And3AreSameSuit && cards3And4AreSameSuit && cards4And5AreSameSuit && cards5And6AreSameSuit && cards6And7AreSameSuit)) {
            NSLog(@"Player has a seven-card straight flush.  %d items in the array.", [self count]);

            return TRUE;
        }

        // (6-card straight flushes)  if cards 1-6 or 2-7 are straight flush
        else if ((cards1And2AreSequential && cards2And3AreSequential && cards3And4AreSequential && cards4And5AreSequential && cards5And6AreSequential && cards1And2AreSameSuit && cards2And3AreSameSuit && cards3And4AreSameSuit && cards4And5AreSameSuit && cards5And6AreSameSuit) ||  (cards2And3AreSequential && cards3And4AreSequential && cards4And5AreSequential && cards5And6AreSequential && cards6And7AreSequential && cards2And3AreSameSuit && cards3And4AreSameSuit && cards4And5AreSameSuit && cards5And6AreSameSuit && cards6And7AreSameSuit)) {
            NSLog(@"Player has a six-card straight flush.  %d in the array.", [self count]);

            return TRUE;
        }

        // (5-card straight flushes)  if 1-5 or 2-6 or 3-7 are straight flush
        else if ((cards1And2AreSequential && cards2And3AreSequential && cards3And4AreSequential && cards4And5AreSequential && cards1And2AreSameSuit && cards2And3AreSameSuit && cards3And4AreSameSuit && cards4And5AreSameSuit) ||  (cards2And3AreSequential && cards3And4AreSequential && cards4And5AreSequential && cards5And6AreSequential && cards2And3AreSameSuit && cards3And4AreSameSuit && cards4And5AreSameSuit && cards5And6AreSameSuit) || (cards3And4AreSequential && cards4And5AreSequential && cards5And6AreSequential && cards6And7AreSequential && cards3And4AreSameSuit && cards4And5AreSameSuit && cards5And6AreSameSuit && cards6And7AreSameSuit)) {

            NSLog(@"Player has a straight flush.  %d in the array.", [self count]);

            return TRUE;
        } else {
        NSLog(@"Player does not have a straight flush.  %d in the array.", [self count]);
        return  FALSE;
        }
    }

    // this section is for if there are 6 cards in the array total.
    else if ([self count] == 6) {
        int card1Rank = [[[self objectAtIndex:0] valueForKey:@"rank"] intValue];
        int card2Rank = [[[self objectAtIndex:1] valueForKey:@"rank"] intValue];
        int card3Rank = [[[self objectAtIndex:2] valueForKey:@"rank"] intValue];
        int card4Rank = [[[self objectAtIndex:3] valueForKey:@"rank"] intValue];
        int card5Rank = [[[self objectAtIndex:4] valueForKey:@"rank"] intValue];
        int card6Rank = [[[self objectAtIndex:5] valueForKey:@"rank"] intValue];

        id card1Suit = [[self objectAtIndex:0] valueForKey:@"suit"];
        id card2Suit = [[self objectAtIndex:1] valueForKey:@"suit"];
        id card3Suit = [[self objectAtIndex:2] valueForKey:@"suit"];
        id card4Suit = [[self objectAtIndex:3] valueForKey:@"suit"];
        id card5Suit = [[self objectAtIndex:4] valueForKey:@"suit"];
        id card6Suit = [[self objectAtIndex:5] valueForKey:@"suit"];

            // added
            // (6-card straight flush)  if cards 1-6 is straight flush
            if ((cards1And2AreSequential && cards2And3AreSequential && cards3And4AreSequential && cards4And5AreSequential && cards5And6AreSequential && cards1And2AreSameSuit && cards2And3AreSameSuit && cards3And4AreSameSuit && cards4And5AreSameSuit && cards5And6AreSameSuit)) {
                NSLog(@"Player has a six-card straight flush.  %d in the array.", [self count]);

                return TRUE;
            }
            // (5-card straight flushes)  if cards 1-5 or cards 2-6 make a straight flush
            else if ((cards1And2AreSequential && cards2And3AreSequential && cards3And4AreSequential && cards4And5AreSequential && cards1And2AreSameSuit && cards2And3AreSameSuit && cards3And4AreSameSuit && cards4And5AreSameSuit) ||  (cards2And3AreSequential && cards3And4AreSequential && cards4And5AreSequential && cards5And6AreSequential && cards2And3AreSameSuit && cards3And4AreSameSuit && cards4And5AreSameSuit && cards5And6AreSameSuit)) {
                NSLog(@"Player has a straight flush.  %d in the array.", [self count]);

                return TRUE;
            } else {
            NSLog(@"Player does not have a straight flush.  %d in the array.", [self count]);
            return  FALSE;
            }
        }

    // if only 5 cards in the array, only one possible straight flush
    else if ([self count] == 5) {
        int card1Rank = [[[self objectAtIndex:0] valueForKey:@"rank"] intValue];
        int card2Rank = [[[self objectAtIndex:1] valueForKey:@"rank"] intValue];
        int card3Rank = [[[self objectAtIndex:2] valueForKey:@"rank"] intValue];
        int card4Rank = [[[self objectAtIndex:3] valueForKey:@"rank"] intValue];
        int card5Rank = [[[self objectAtIndex:4] valueForKey:@"rank"] intValue];

        id card1Suit = [[self objectAtIndex:0] valueForKey:@"suit"];
        id card2Suit = [[self objectAtIndex:1] valueForKey:@"suit"];
        id card3Suit = [[self objectAtIndex:2] valueForKey:@"suit"];
        id card4Suit = [[self objectAtIndex:3] valueForKey:@"suit"];
        id card5Suit = [[self objectAtIndex:4] valueForKey:@"suit"];

        if (cards1And2AreSequential && cards2And3AreSequential && cards3And4AreSequential && cards4And5AreSequential && cards1And2AreSameSuit && cards2And3AreSameSuit && cards3And4AreSameSuit && cards4And5AreSameSuit) {
            NSLog(@"Player has a straight flush.  %d in the array.", [self count]);

            return TRUE;
        } else {
            NSLog(@"Player does not have a straight flush.  %d in the array.", [self count]);
            return FALSE;
        }
    }
    NSLog(@"Something is wrong in the isStraightFlush method.");

    return 0;
}

@end

そして、以下は私の CCGamePlayLayer からのものです。このレイヤーは、デッキの作成、シャッフル、およびディールを処理します。この場合、ロードされたデッキで、毎回ロイヤル フラッシュが得られます。まず、2 つのオフスーツ デュースと 10、J、Q、K、A のクラブで構成されるデッキを作成します。次に、シャッフル メソッドを呼び出し、プレイヤーのホール カード 2 枚、フロップのカード 3 枚、ターンのカード 1 枚、リバーのカード 1 枚を含むボードを配ります。次に、evaluateHand メソッドを呼び出して、プレイヤーが 7 枚のカードで作ることができる最高のハンドを決定します。プレーヤーが持っているハンドの種類が特定されると、evaluateHand メソッドは、ハンドが何であるかをプレーヤーに伝える UIAlertView を表示するだけです。

CCGamePlayLayer.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#import <UIKit/UIKit.h>
#import "NSMutableArray+HandAnalysis.h"

@interface CCGamePlayLayer : CCLayer <CCTargetedTouchDelegate> {

    NSMutableArray *deck;
}

@property (nonatomic, retain) NSMutableArray *deck;
    @property (nonatomic, retain) hasPair, hasTwoPair, hasSet, hasStraight, hasFlush, hasBoat, hasQuads, hasStraightFlush, hasRoyalFlush;

CCGamePlayLayer.m

#import "CCGamePlayLayer.h"
#import "ItemsViewController.h"
#import "MGAppDelegate.h"
#import "Card.h"

@implementation CCGamePlayLayer

@synthesize deck;
@synthesize hasPair, hasTwoPair, hasSet, hasStraight, hasFlush, hasBoat, hasQuads, hasStraightFlush, hasRoyalFlush;

-(id)init {
    if (self = [super init]) {

        //  ... Make the buttons and background

        [self makeLoadedRoyalFlushDeck];
        [self shuffle];

        NSLog(@"Here's the shuffled deck, from the init method:");

        for (Card *c in deck) {
            NSLog(@"%@", c.imageName);
        }

        [self dealLoadedHoleCards];
    }
    return self;
}

-(NSMutableArray *)makeLoadedRoyalFlushDeck {
    Card *aceC = [Card spriteWithFile:@"pbwb1c.png"];
    [aceC setValue:@"Ac" forKey:@"imageName"];
    [aceC setValue:[NSNumber numberWithInt:1] forKey:@"rank"];
    [aceC setValue:@"club" forKey:@"suit"];

    Card *tenC = [Card spriteWithFile:@"pbwbtc.png"];
    [tenC setValue:@"Tc" forKey:@"imageName"];
    [tenC setValue:[NSNumber numberWithInt:10] forKey:@"rank"];
    [tenC setValue:@"club" forKey:@"suit"];

    Card *jackC = [Card spriteWithFile:@"pbwbjc.png"];
    [jackC setValue:@"Jc" forKey:@"imageName"];
    [jackC setValue:[NSNumber numberWithInt:11] forKey:@"rank"];
    [jackC setValue:@"club" forKey:@"suit"];

    Card *queenC = [Card spriteWithFile:@"pbwbqc.png"];
    [queenC setValue:@"Qc" forKey:@"imageName"];
    [queenC setValue:[NSNumber numberWithInt:12] forKey:@"rank"];
    [queenC setValue:@"club" forKey:@"suit"];

    Card *kingC = [Card spriteWithFile:@"pbwbkc.png"];
    [kingC setValue:@"Kc" forKey:@"imageName"];
    [kingC setValue:[NSNumber numberWithInt:13] forKey:@"rank"];
    [kingC setValue:@"club" forKey:@"suit"];

    Card *deuceS = [Card spriteWithFile:@"pbwb2s.png"];
    [deuceS setValue:@"2s" forKey:@"imageName"];
    [deuceS setValue:[NSNumber numberWithInt:2] forKey:@"rank"];
    [deuceS setValue:@"spade" forKey:@"suit"];

    Card *deuceD = [Card spriteWithFile:@"pbwb2d.png"];
    [deuceD setValue:@"2d" forKey:@"imageName"];
    [deuceD setValue:[NSNumber numberWithInt:2] forKey:@"rank"];
    [deuceD setValue:@"diamond" forKey:@"suit"];

    deck = [NSMutableArray arrayWithObjects: deuceS, deuceD, aceC, tenC, jackC, queenC, kingC, nil];

    for (Card *c in deck) {
        NSLog(@"%@", c.imageName);
    }

    return deck;
}

-(void)shuffle {
    NSLog(@"[self.deck count] == %d", [self.deck count]);
    for (int i = [self.deck count] - 1; i > 0; i--) {
        [self.deck exchangeObjectAtIndex:(arc4random_uniform([self.deck count] - 1)) withObjectAtIndex:i];
    }
    NSLog(@"The deck has been shuffled.");
    NSLog(@"Here is the shuffled deck:");

    for (Card *c in deck) {
        NSLog(@"%@", c.imageName);
    }
}

-(void)evaluateHand {
    if (playerHoleCard1 && playerHoleCard2 && !flop1 && !flop2 && !flop3 && !turn && !river) {
        UIAlertView *holeCardsAlert = [[UIAlertView alloc] initWithTitle:@"Whoa!" message:@"You've only been dealt your hole cards!  Slow down, there, killer!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [holeCardsAlert show];
    }
    else if (playerHoleCard1 && playerHoleCard2 && flop1 && flop2 && flop3 && !turn && !river) {
        UIAlertView *flopAlert = [[UIAlertView alloc] initWithTitle:@"FLOP" message:@"You need to wait until the board is completely dealt out to evaluate your hand." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [flopAlert show];
    }
    else if (playerHoleCard1 && playerHoleCard2 && flop1 && flop2 && flop3 && turn && !river) {
        UIAlertView *turnAlert = [[UIAlertView alloc] initWithTitle:@"TURN" message:@"Almost there.  One card to go, and then I'll tell you what kind of hand you have." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [turnAlert show];
    }
    else if (playerHoleCard1 && playerHoleCard2 && flop1 && flop2 && flop3 && turn && river) {

        // put the player's 2 hole cards and the 5 board cards in an array for sorting
        NSMutableArray *allCards = [NSMutableArray arrayWithObjects:flop1, flop2, flop3, turn, river, playerHoleCard1, playerHoleCard2, nil];

        // first check for higher hands
        // royal flush
        id sequentialHand           = [allCards sortByRank];
        id sequentialAndSuitedHand  = [sequentialHand sortBySuit];

        // Check for straight flush first
        hasRoyalFlush = [sequentialAndSuitedHand isStraightFlush];

        // Checking for straight or flush
        hasStraight = [sequentialHand isStraight];
        hasFlush = [sequentialAndSuitedHand isFlush];

        // Checking for quads, we check the original array
        hasQuads = [allCards isQuads];

        // Check for SF
        if (hasStraight == TRUE && hasFlush == TRUE) {
            if (hasRoyalFlush == TRUE) {
                hasStraightFlush = FALSE;
                hasStraight = FALSE;
                hasFlush = FALSE;
                NSLog(@"hasRoyalFlush == TRUE");
            } else if (hasRoyalFlush == FALSE) {
                NSLog(@"hasRoyalFlush == FALSE");
            }
        }
        else if (hasRoyalFlush == FALSE) {
                hasStraight = FALSE;
                hasFlush = FALSE;
                UIAlertView *straightFlushAlert = [[UIAlertView alloc] initWithTitle:@"STRAIGHT FLUSH" message:@"You have a straight flush." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [straightFlushAlert show];
            } else if (hasStraightFlush == FALSE) {
                NSLog(@"hasStraightFlush == FALSE");
            } else {
                NSLog(@"Some problem in determining whether hasStraightFlush is TRUE or FALSE.");
            }

        // check for straight
        if (hasStraight == TRUE) {
            UIAlertView *straightAlert = [[UIAlertView alloc] initWithTitle:@"STRAIGHT" message:@"You have a straight." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [straightAlert show];

            NSLog(@"Player has a straight.");
        }

        if (hasFlush == TRUE) {
            UIAlertView *flushAlert = [[UIAlertView alloc] initWithTitle:@"FLUSH" message:@"You have a flush." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [flushAlert show];

            NSLog(@"Player has a flush.");
        }

        if (hasQuads == TRUE) {
            hasPair = FALSE;
            hasTwoPair = FALSE;
            hasStraight = FALSE;
            hasFlush = FALSE;
            UIAlertView *quadsAlert = [[UIAlertView alloc] initWithTitle:@"QUADS" message:@"You have four of a kind." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [quadsAlert show];
        }
    }
}

@end

これを実行して手持ちのハンドを評価しようとすると、コンソールから次のような結果が得られます。

2013-03-14 10:27:53.045 TexasHoldemGame[7600:c07] sortByRank called.
2013-03-14 10:27:53.045 TexasHoldemGame[7600:c07] 1
2013-03-14 10:27:53.046 TexasHoldemGame[7600:c07] 2
2013-03-14 10:27:53.047 TexasHoldemGame[7600:c07] 2
2013-03-14 10:27:53.048 TexasHoldemGame[7600:c07] 10
2013-03-14 10:27:53.049 TexasHoldemGame[7600:c07] 11
2013-03-14 10:27:53.049 TexasHoldemGame[7600:c07] 12
2013-03-14 10:27:53.050 TexasHoldemGame[7600:c07] 13
2013-03-14 10:27:53.050 TexasHoldemGame[7600:c07] For thoroughness, here it is with both values and suits:
2013-03-14 10:27:53.051 TexasHoldemGame[7600:c07] 1 of club
2013-03-14 10:27:53.051 TexasHoldemGame[7600:c07] 2 of diamond
2013-03-14 10:27:53.052 TexasHoldemGame[7600:c07] 2 of spade
2013-03-14 10:27:53.053 TexasHoldemGame[7600:c07] 10 of club
2013-03-14 10:27:53.053 TexasHoldemGame[7600:c07] 11 of club
2013-03-14 10:27:53.054 TexasHoldemGame[7600:c07] 12 of club
2013-03-14 10:27:53.054 TexasHoldemGame[7600:c07] 13 of club
2013-03-14 10:27:53.055 TexasHoldemGame[7600:c07] sortBySuit called.
2013-03-14 10:27:53.055 TexasHoldemGame[7600:c07] club
2013-03-14 10:27:53.056 TexasHoldemGame[7600:c07] club
2013-03-14 10:27:53.056 TexasHoldemGame[7600:c07] club
2013-03-14 10:27:53.057 TexasHoldemGame[7600:c07] club
2013-03-14 10:27:53.057 TexasHoldemGame[7600:c07] club
2013-03-14 10:27:53.058 TexasHoldemGame[7600:c07] diamond
2013-03-14 10:27:53.058 TexasHoldemGame[7600:c07] spade
2013-03-14 10:27:53.059 TexasHoldemGame[7600:c07] For thoroughness, here it is with both values and suits:
2013-03-14 10:27:53.059 TexasHoldemGame[7600:c07] 1 of club
2013-03-14 10:27:53.060 TexasHoldemGame[7600:c07] 10 of club
2013-03-14 10:27:53.061 TexasHoldemGame[7600:c07] 11 of club
2013-03-14 10:27:53.061 TexasHoldemGame[7600:c07] 12 of club
2013-03-14 10:27:53.062 TexasHoldemGame[7600:c07] 13 of club
2013-03-14 10:27:53.062 TexasHoldemGame[7600:c07] 2 of diamond
2013-03-14 10:27:53.063 TexasHoldemGame[7600:c07] 2 of spade
2013-03-14 10:27:53.063 TexasHoldemGame[7600:c07] isStraightFlush bool method called.
2013-03-14 10:27:53.064 TexasHoldemGame[7600:c07] sortByRank called.
2013-03-14 10:27:53.064 TexasHoldemGame[7600:c07] 1
2013-03-14 10:27:53.065 TexasHoldemGame[7600:c07] 2
2013-03-14 10:27:53.066 TexasHoldemGame[7600:c07] 2
2013-03-14 10:27:53.067 TexasHoldemGame[7600:c07] 10
2013-03-14 10:27:53.067 TexasHoldemGame[7600:c07] 11
2013-03-14 10:27:53.069 TexasHoldemGame[7600:c07] 12
2013-03-14 10:27:53.070 TexasHoldemGame[7600:c07] 13
2013-03-14 10:27:53.070 TexasHoldemGame[7600:c07] For thoroughness, here it is with both values and suits:
2013-03-14 10:27:53.071 TexasHoldemGame[7600:c07] 1 of club
2013-03-14 10:27:53.072 TexasHoldemGame[7600:c07] 2 of diamond
2013-03-14 10:27:53.073 TexasHoldemGame[7600:c07] 2 of spade
2013-03-14 10:27:53.073 TexasHoldemGame[7600:c07] 10 of club
2013-03-14 10:27:53.073 TexasHoldemGame[7600:c07] 11 of club
2013-03-14 10:27:53.074 TexasHoldemGame[7600:c07] 12 of club
2013-03-14 10:27:53.074 TexasHoldemGame[7600:c07] 13 of club
2013-03-14 10:27:53.076 TexasHoldemGame[7600:c07] sortBySuit called.
2013-03-14 10:27:53.077 TexasHoldemGame[7600:c07] club
2013-03-14 10:27:53.078 TexasHoldemGame[7600:c07] club
2013-03-14 10:27:53.079 TexasHoldemGame[7600:c07] club
2013-03-14 10:27:53.080 TexasHoldemGame[7600:c07] club
2013-03-14 10:27:53.082 TexasHoldemGame[7600:c07] club
2013-03-14 10:27:53.082 TexasHoldemGame[7600:c07] diamond
2013-03-14 10:27:53.083 TexasHoldemGame[7600:c07] spade
2013-03-14 10:27:53.083 TexasHoldemGame[7600:c07] For thoroughness, here it is with both values and suits:
2013-03-14 10:27:53.084 TexasHoldemGame[7600:c07] 1 of club
2013-03-14 10:27:53.084 TexasHoldemGame[7600:c07] 10 of club
2013-03-14 10:27:53.085 TexasHoldemGame[7600:c07] 11 of club
2013-03-14 10:27:53.086 TexasHoldemGame[7600:c07] 12 of club
2013-03-14 10:27:53.087 TexasHoldemGame[7600:c07] 13 of club
2013-03-14 10:27:53.088 TexasHoldemGame[7600:c07] 2 of diamond
2013-03-14 10:27:53.089 TexasHoldemGame[7600:c07] 2 of spade
2013-03-14 10:27:53.090 TexasHoldemGame[7600:c07] Amount of cards in the array:  7
2013-03-14 10:27:53.092 TexasHoldemGame[7600:c07] Player does not have a straight flush.  7 in the array.
2013-03-14 10:27:53.092 TexasHoldemGame[7600:c07] isStraight called.
2013-03-14 10:27:53.093 TexasHoldemGame[7600:c07] Back in the isStraight method again.
2013-03-14 10:27:53.094 TexasHoldemGame[7600:c07] We're about to start iterating through the array, and here are the ranks in the array, before elimination:
2013-03-14 10:27:53.095 TexasHoldemGame[7600:c07] Rank:  1
2013-03-14 10:27:53.096 TexasHoldemGame[7600:c07] Rank:  2
2013-03-14 10:27:53.098 TexasHoldemGame[7600:c07] Rank:  2
2013-03-14 10:27:53.098 TexasHoldemGame[7600:c07] Rank:  10
2013-03-14 10:27:53.099 TexasHoldemGame[7600:c07] Rank:  11
2013-03-14 10:27:53.099 TexasHoldemGame[7600:c07] Rank:  12
2013-03-14 10:27:53.099 TexasHoldemGame[7600:c07] Rank:  13
2013-03-14 10:27:53.102 TexasHoldemGame[7600:c07] This is how many items are in the array, currently:  6
2013-03-14 10:27:53.103 TexasHoldemGame[7600:c07] After elimination:
2013-03-14 10:27:53.104 TexasHoldemGame[7600:c07] Rank:  1
2013-03-14 10:27:53.104 TexasHoldemGame[7600:c07] Rank:  2
2013-03-14 10:27:53.105 TexasHoldemGame[7600:c07] Rank:  10
2013-03-14 10:27:53.105 TexasHoldemGame[7600:c07] Rank:  11
2013-03-14 10:27:53.105 TexasHoldemGame[7600:c07] Rank:  12
2013-03-14 10:27:53.106 TexasHoldemGame[7600:c07] Rank:  13
2013-03-14 10:27:53.106 TexasHoldemGame[7600:c07] There are 5 or more cards.  Proceed.
2013-03-14 10:27:53.107 TexasHoldemGame[7600:c07] tempHand objectAtIndex:0 == 1
2013-03-14 10:27:53.107 TexasHoldemGame[7600:c07] tempHand objectAtIndex:1 == 2
2013-03-14 10:27:53.108 TexasHoldemGame[7600:c07] tempHand objectAtIndex:2 == 10
2013-03-14 10:27:53.143 TexasHoldemGame[7600:c07] tempHand objectAtIndex:3 == 11
2013-03-14 10:27:53.144 TexasHoldemGame[7600:c07] tempHand objectAtIndex:4 == 12
2013-03-14 10:27:53.144 TexasHoldemGame[7600:c07] tempHand objectAtIndex:5 == 13
2013-03-14 10:27:53.145 TexasHoldemGame[7600:c07] You have a straight.
2013-03-14 10:27:53.145 TexasHoldemGame[7600:c07] isFlush bool method called.
2013-03-14 10:27:53.146 TexasHoldemGame[7600:c07] FLUSH!!!  FLUSH!!!  FLUSH!!!
2013-03-14 10:27:53.146 TexasHoldemGame[7600:c07] isQuads bool method called.
2013-03-14 10:27:53.147 TexasHoldemGame[7600:c07] Print out the array, so we know what we're dealing with:
2013-03-14 10:27:53.148 TexasHoldemGame[7600:c07] 12 of club
2013-03-14 10:27:53.148 TexasHoldemGame[7600:c07] 1 of club
2013-03-14 10:27:53.149 TexasHoldemGame[7600:c07] 13 of club
2013-03-14 10:27:53.150 TexasHoldemGame[7600:c07] 2 of diamond
2013-03-14 10:27:53.150 TexasHoldemGame[7600:c07] 2 of spade
2013-03-14 10:27:53.151 TexasHoldemGame[7600:c07] 11 of club
2013-03-14 10:27:53.152 TexasHoldemGame[7600:c07] 10 of club
2013-03-14 10:27:53.152 TexasHoldemGame[7600:c07] Number of Aces:  1
Number of Deuces:  2
Number of Threes:  0
Number of Fours:  0
Number of Fives:  0
Number of Sixes:  0
Number of Sevens:  0
Number of Eights:  0
Number of Nines:  0
Number of Tens:  1
Number of Jacks:  1
Number of Queens:  1
Number of Kings:  1
2013-03-14 10:27:53.153 TexasHoldemGame[7600:c07] PLayer does not have quads.
2013-03-14 10:27:53.153 TexasHoldemGame[7600:c07] hasRoyalFlush == FALSE
2013-03-14 10:27:53.522 TexasHoldemGame[7600:c07] Player has a straight.
2013-03-14 10:27:53.523 TexasHoldemGame[7600:c07] Player has a flush.

そして、ご想像のとおり、画面に 2 つの UIAlertView がポップアップ表示されます。「STRAIGHT」と「FLUSH」のアラートビューです。isStraightFlush と別の方法で上記の方法を試しました。メソッド内に一時配列を作成し、受信配列を一時配列のゲームプレイ レイヤーに格納し、スーツごとにカードを 4 つの個別の配列 (clubArray、diamondArray、heartArray、 spadeArray) を作成し、上で行ったのと同じように、配列を繰り返し処理して、カードが順番に並べられているかどうかを確認します。アレイのカードが 5 枚未満の場合、もちろん、ストレート フラッシュは不可能です。

何が間違っているのかわかりません。ばかげた長い投稿で申し訳ありません。どんな助けでも大歓迎です。

4

1 に答える 1

1

問題はここにあります:

[self sortByRank];
[self sortBySuit];

これらのメソッドは、配列をその場で変更するのではなく、並べ替えられた配列を返します。これを破棄します。

私の提案: NSMutableArray でカテゴリを作成する代わりに、 のような NSMutableArray プロパティを持つ Hand クラスを作成しますcards。次に-sortByRank、そのプロパティを再割り当てし、ロジックの残りの部分で-sortBySuitに置き換えます。selfself.cards

注:おっと。を呼び出す前にランクとスーツでソートしているので、これは問題ではないと思います-isStraightFlush。それでも、これらの行は混乱を招くだけです。

より良いモデルクラスを構築するという私の提案はまだ有効であるため、この回答は残しておきます。

追加:また、NSNumber および NSString オブジェクトでポインター比較を行うべきではありません。それが実際に問題になる可能性があります。

card1.suit == card2.suit
card1.rank == (card2.rank - 1)

代わりに値を比較します。

[card1.suit isEqualToString:card2.suit]
card1.rank.integerValue == card2.rank.integerValue - 1
于 2013-03-14T18:52:05.970 に答える