扑克手排名循环组合

Poker Hand Ranking loop combinations

所以我目前正在为我自己的个人经验增长做一个手牌排名程序。但是,我想做的是用 7 张卡片评估中的 5 张卡片。

例如,我目前有一个包含 7 个值的数组。我想 运行 5 个对象针对我的循环 21 次以确定哪个对象给出了最高结果。而不是将 7 张卡操作编程为 运行 一次。

我的问题是,我如何 运行 包含 7 个对象中的 5 个的循环,以便考虑所有组合并且不重复?

//Create 7 Card Array

_sevenCardArray = [[NSArray alloc] initWithObjects:_deck[0],_deck[1],_deck[2],_deck[3],_deck[4],_deck[6],_deck[8], nil];

for (int i = 0; i < 21; i++) {
    //Runs 5 of the 7 cards against the hand determinator?
}

例子

NSArray *combinations = [[NSArray alloc] initWithObjects:
                         @"01234",
                         @"01235",
                         @"01236",
                         @"01254",
                         @"01264",
                         @"01534",
                         @"01634",
                         @"05234",
                         @"06234",
                         @"51234",
                         @"61234",
                         @"56234",
                         @"51634",
                         @"51264",
                         @"51236",
                         @"05634",
                         @"05264",
                         @"05236",
                         @"01564",
                         @"01536",
                         @"01256",
                         nil];

可能不是最佳解决方案。然而,这就是我想出的。

NSArray *combinations = [[NSArray alloc] initWithObjects:
                         @"0 1 2 3 4",
                         @"0 1 2 3 5",
                         @"0 1 2 3 6",
                         @"0 1 2 5 4",
                         @"0 1 2 6 4",
                         @"0 1 5 3 4",
                         @"0 1 6 3 4",
                         @"0 5 2 3 4",
                         @"0 6 2 3 4",
                         @"5 1 2 3 4",
                         @"6 1 2 3 4",
                         @"5 6 2 3 4",
                         @"5 1 6 3 4",
                         @"5 1 2 6 4",
                         @"5 1 2 3 6",
                         @"0 5 6 3 4",
                         @"0 5 2 6 4",
                         @"0 5 2 3 6",
                         @"0 1 5 6 4",
                         @"0 1 5 3 6",
                         @"0 1 2 5 6",
                         nil];

for (int i = 0; i < [combinations count]; i++) {

    NSString *value = combinations[i];

    NSArray *split = [value componentsSeparatedByString:@" "];

    NSArray *newArray = [[NSArray alloc] initWithObjects:_sevenCardArray[[split[0] intValue]],_sevenCardArray[[split[1] intValue]],_sevenCardArray[[split[2] intValue]],_sevenCardArray[[split[3] intValue]],_sevenCardArray[[split[4] intValue]], nil];

}

您可以使用嵌套的 for 循环来实现,例如:

-(void)pokerHandsCheck:(NSArray *)cards
{
    for (int i = 0; i < 6; i++)
    {
        for (int j = i+1; j < 7;j++ )
        {
            NSMutableArray *checkvals = [cards mutableCopy];
            NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init];
            [mutableIndexSet addIndex:i];
            [mutableIndexSet addIndex:j];
            [checkvals removeObjectsAtIndexes:mutableIndexSet];
            //check availibility here with checkvals.eg:[self checkset:checkvals];
            NSLog(@"%@", checkvals);
        }
    }
}

示例调用是:

[self pokerHandsCheck:@[@"1",@"10",@"11",@"12",@"13",@"5",@"2"]];

不重复手将精确循环21次

编辑:

您也可以使用矩阵检查所有这些:

-(void)checkCards:(NSArray *)cards
{
    int matrix[4][13];

    for (int i = 0; i<4; i++) {
        for (int j = 0; j<13; j++) {
            matrix[i][j] = 0;
        }
    }

    for (NSNumber *n in cards) {
        //assumed that you have 52 carded deck.values from 0..51
        //or if you have a class card with value and type ;
        //enumerate type 0 to 3, and value 0 to 12. directly set matrix[card.type][card.value] = 1;
        int p = [n intValue];
        matrix[p/13][p%13] = 1;// you filled the places with ones in an array
    }
    //for example for the kent check you can do
    for (int i = 0; i<13; i++) {
        if (matrix[0][i] == matrix[1][i] && matrix[0][i] == matrix[2][i] &&
            matrix[0][i] == matrix[3][i] && matrix[0][i] == 1 ) {
            //kent here
            NSLog(@"kent %d", i);
            break;
        }
    }

    //for all the case you can do matrix operations
}