I have 9 tiles labeled 1-9 with corresponding values. Tile 1=1, tile 6=6, etc. A user can press down tiles that equal the sum of the die. When they press confirm move, those tales can not be used anymore.
I need to compute every possible combination of numbers to check whether or not the user can go again. Currently I am computing it like this, but I'm sure there has to be a better way.
allTiles
contains the tiles that are still available to be computed.
What would be the best way to check for all combinations?
- (void) getCombinations {
NSMutableArray *combinations = [[NSMutableArray alloc] init];
for (int i = 0; i < [allTiles count]; i++) {
for (int j = i + 1; j < [allTiles count]; j++) {
for (int k = i+2; k < [allTiles count]; k++) {
TileView *first = [allTiles objectAtIndex:i];
TileView *second = [allTiles objectAtIndex:j];
TileView *third = [allTiles objectAtIndex:k];
NSNumber *total = [NSNumber numberWithInt:first.getTileValue + second.getTileValue];
NSNumber *total2 = [NSNumber numberWithInt:
first.getTileValue +
second.getTileValue +
third.getTileValue];
[combinations addObject:[NSNumber numberWithInt:first.getTileValue]];
[combinations addObject:[NSNumber numberWithInt:second.getTileValue]];
[combinations addObject:[NSNumber numberWithInt:third.getTileValue]];
[combinations addObject:total];
[combinations addObject:total2];
}
}
}
if ([combinations containsObject:[NSNumber numberWithInt:[self diceTotal]]]) {
NSLog(@"STILL COMBINATION AVAILABLE");
}
else {
NSString *message = [NSString stringWithFormat:@"Your score: %i", player1Score];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"No more combinations left."
message: message
delegate:self
cancelButtonTitle:@"Go to player2"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
Here is a a screenshot of one of scenarios where my method doesn't work.