ios - 扑克手排名循环组合

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

最佳答案

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

关于ios - 扑克手排名循环组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35132150/

相关文章:

ios - 禁止在 iOS 应用程序中生成崩溃日志

objective-c - Facebook iOS SDK : Retrieve User's Phone Number and Country

ios - 将 iOS objective-c 对象转换为 JSON 字符串

ios - Dispatch_async 停止 UITableView 重新加载

ios - Mac 和 iOS 应用程序之间的通用 iCloud 容器?

ios - 如何在 Xcode 上管理登录和注销过程

ios - 尝试存档时自定义 NavigationStack 导致 `error: Segmentation fault: 11` 时的解决方法建议?

objective-c - 如何查找 NSDate 是否在指定范围内?

iphone - 以字典为对象的NSDictionary,获取对象

ios - 比较 NSArray1 和 NSArray2 的对象