扑克手排名循环组合

时间:2016-02-01 13:47:43

标签: ios objective-c nsarray poker

因此,我目前正在为自己的个人经历增长制作一个手部排名计划。但是,我想要做的是用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];

2 个答案:

答案 0 :(得分:1)

你可以使用嵌套的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
}

答案 1 :(得分:0)

可能不是最好的解决方案。但是,这就是我想出来的。

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];

}