Objective c 计算所有可能的数字组合

Objective c 计算所有可能的数字组合,objective-c,Objective C,我有9块贴有相应值的1-9瓷砖。瓦片1=1、瓦片6=6等。用户可以按下等于模具总数的瓦片。当他们按确认移动时,这些故事就不能再使用了 我需要计算所有可能的数字组合,以检查用户是否可以再次访问。目前我是这样计算的,但我相信一定有更好的方法。 allTiles包含仍可用于计算的tile 检查所有组合的最佳方法是什么 - (void) getCombinations { NSMutableArray *combinations = [[NSMutableArray alloc] init]; fo

我有9块贴有相应值的1-9瓷砖。瓦片1=1、瓦片6=6等。用户可以按下等于模具总数的瓦片。当他们按确认移动时,这些故事就不能再使用了

我需要计算所有可能的数字组合,以检查用户是否可以再次访问。目前我是这样计算的,但我相信一定有更好的方法。
allTiles
包含仍可用于计算的tile

检查所有组合的最佳方法是什么

- (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];
}
} 
-(void)getcompositions{
NSMutableArray*组合=[[NSMutableArray alloc]init];
对于(int i=0;i<[allTiles count];i++){
对于(int j=i+1;j<[allTiles count];j++){
对于(int k=i+2;k<[allTiles count];k++){
TileView*first=[allTiles对象索引:i];
TileView*second=[allTiles对象索引:j];
TileView*third=[allTiles对象索引:k];
NSNumber*total=[NSNumber numberwhint:first.getTileValue+second.getTileValue];
NSNumber*total2=[NSNumber NUMBER WITHINT:
第一,getTileValue+
第二,getTileValue+
第三,getTileValue];
[组合添加对象:[NSNumber numberWithInt:first.getTileValue]];
[NSNumber-numberWithInt:second.getTileValue];
[NSNumber numberWithInt:third.getTileValue];
[对象:总计];
[对象:总计2];
}
}
}
if([组合包含对象:[NSNumber numberWithInt:[self diceTotal]]]){
NSLog(“仍然可用组合”);
}
否则{
NSString*message=[NSString stringWithFormat:@“您的分数:%i”,player1Score];
UIAlertView*警报=[[UIAlertView alloc]
initWithTitle:@“没有更多的组合了。”
信息:信息
代表:赛尔夫
取消按钮:@“转到播放器2”
其他按钮:无];
[警报显示];
[警报发布];
}
} 
下面是我的方法不起作用的一个场景的屏幕截图。

在您的示例中,玩家可以使用四个互动程序。是否包含分片是一个二进制选择(是或否),本例中有4个这样的选择,因此有2×2×2×2=24个可能的分片组合。一般来说,有2n个组合,其中有n个瓷砖可用

检查与目标求和的组合的最简单方法是简单地枚举从0到2n的整数。对于每个整数i,以二进制形式写入i,并将对应于二进制表示的1的分片值相加

我们将在本地数组中缓存平铺值,以避免反复发送
tileValue
消息

static int selectedNumbersSum(unsigned int selector, int const *numbers) {
    int sum = 0;
    for (int i = 0; selector != 0; i += 1, selector >>= 1) {
        if (selector & 1) {
            sum += numbers[i];
        }
    }
    return sum;
}

static BOOL tilesCanMakeTarget(NSArray *tiles, int target) {
    NSUInteger count = tiles.count;
    int numbers[count];
    for (int i = 0; i < count; ++i) {
        numbers[i] = [tiles[i] tileValue];
    }

    for (unsigned int step = 0, maxStep = 1 << count; step < maxStep; ++step) {
        if (selectedNumbersSum(step, numbers) == target)
            return YES;
    }
    return NO;
}

- (void)checkForEndOfPlayer1Turn {
    if (tilesCanMakeTarget(allTiles, 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 player 2"
            otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}
static int selectedNumbersSum(无符号int选择器,int const*numbers){
整数和=0;
对于(int i=0;选择器!=0;i+=1,选择器>>=1){
如果(选择器&1){
总和+=数字[i];
}
}
回报金额;
}
静态BOOL tilesCanMakeTarget(NSArray*tiles,int-target){
NSU整数计数=tiles.count;
整数[计数];
对于(int i=0;i对于(unsigned int step=0,maxStep=1在您的示例中,播放器有四个可用的磁贴。是否包含磁贴是一个二进制选择(是或否),在本例中有4个这样的选择,因此有2×2×2×2=24个可能的瓷砖组合。通常,有n个瓷砖可用时,有2n个组合

检查与目标求和的组合的最简单方法是简单地枚举从0到2n的整数。对于每个整数i,用二进制写入i,并将对应于二进制表示的1的分片值相加

我们将在本地数组中缓存平铺值,以避免反复发送
tileValue
消息

static int selectedNumbersSum(unsigned int selector, int const *numbers) {
    int sum = 0;
    for (int i = 0; selector != 0; i += 1, selector >>= 1) {
        if (selector & 1) {
            sum += numbers[i];
        }
    }
    return sum;
}

static BOOL tilesCanMakeTarget(NSArray *tiles, int target) {
    NSUInteger count = tiles.count;
    int numbers[count];
    for (int i = 0; i < count; ++i) {
        numbers[i] = [tiles[i] tileValue];
    }

    for (unsigned int step = 0, maxStep = 1 << count; step < maxStep; ++step) {
        if (selectedNumbersSum(step, numbers) == target)
            return YES;
    }
    return NO;
}

- (void)checkForEndOfPlayer1Turn {
    if (tilesCanMakeTarget(allTiles, 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 player 2"
            otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}
static int selectedNumbersSum(无符号int选择器,int const*numbers){
整数和=0;
对于(int i=0;选择器!=0;i+=1,选择器>>=1){
如果(选择器&1){
总和+=数字[i];
}
}
回报金额;
}
静态BOOL tilesCanMakeTarget(NSArray*tiles,int-target){
NSU整数计数=tiles.count;
整数[计数];
对于(int i=0;ifor(unsigned int step=0,maxStep=1@GabrielePetronella)这就是我的代码的基础,但是我认为除了添加更多for循环之外,可能还有一种更简单的计算可能性的方法。因此,需要明确的是,玩家可以选择任意数量的tile?例如,在你的示例中,玩家可以选择三个tile(1、3和5)达到目标数量(9)正确。这就是我遇到麻烦的地方…你真的需要所有组合的列表吗?或者你只需要知道是否存在任何组合?可能只需要知道它们是否存在。我添加了检查游戏是否结束的完整方法。它仍然没有完全起作用,我很难弄清楚解决此问题的最佳方法是什么。@GabrielePetronella这就是我的代码的基础,但是我认为除了添加更多for循环之外,还有一种更简单的计算可能性的方法。因此,为了清楚起见,玩家可以选择任意数量的tile?例如,在您的示例中,玩家可以选择三个tile(1、3和5)要达到目标数字(9)?正确。这就是我遇到麻烦的地方…你真的需要所有组合的列表吗?或者你只需要知道是否存在任何组合?可能只是kn