Search code examples
iosobjective-cnsarraypoker

Poker Hand Ranking loop combinations


So I'm currently making a hand ranking program for my own personal experience growth. However, how I want to do it is with a 5 card out of 7 card evaluation.

So for example, I currently have an array with 7 values. I want to run 5 of the objects against my loop 21 times to determine which gave the highest result. Rather than programming a 7 card operation to run once.

My questions is, how do I run the loop with 5 out of the 7 objects so that all combinations will be accounted for and no repeats?

//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?
}

example

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

Solution

  • you can do it with nested for loops like:

    -(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);
            }
        }
    }
    

    example call is:

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

    it will loop 21 times exactly without repeated hands.

    EDIT:

    you can also check all of them once with using matrix:

    -(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
    }