我有 9 个标记为 1-9 的图块,并带有相应的值。牌 1=1,牌 6=6,等等。用户可以按下等于骰子总和的牌。当他们按下确认移动时,那些故事就不能再使用了。
我需要计算每个可能的数字组合,以检查用户是否可以再次访问。目前我正在这样计算它,但我确信必须有更好的方法。
allTiles
包含仍可用于计算的图块。
检查所有组合的最佳方法是什么?
- (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];
}
}
这是我的方法不起作用的场景之一的屏幕截图。