Puzzle games
NSArray*adjacentBox(NSIntegerindex,NSIntegerdegree) {
// 先列出边角
NSUIntegerup_left =0;
NSUIntegerup_right = degree -1;
NSUIntegerleft_lower =pow(degree,2) - degree;
NSUIntegerright_lower =pow(degree,2) -1;
// 是边角
if(index == up_left) {
// 左上
return@[@(index +1),@(index + degree)];
}
if(index == up_right) {
// 右上
return@[@(index -1),@(index + degree)];
}
if(index == left_lower) {
// 左下
return@[@(index - degree),@(index +1)];
}
if(index == right_lower) {
// 右下
return@[@(index -1),@(index - degree)];
}
// 是边线
if(index > up_left && index < up_right) {
// 上边线
return@[@(index + degree),@(index -1),@(index +1)];
}
NSMutableArray *left_lines = [NSMutableArray arrayWithCapacity:0];
NSMutableArray *right_lines = [NSMutableArray arrayWithCapacity:0];
for(NSUIntegeri = degree; i < degree * (degree -1); i ++) {
if(i % degree ==0) {
[left_linesaddObject:@(i)];
[right_linesaddObject:@(i + degree -1)];
}
}
if([left_linescontainsObject:@(index)]) {
// 左边线
return@[@(index +1),@(index - degree),@(index + degree)];
}
if([right_linescontainsObject:@(index)]) {
// 右边线
return@[@(index -1),@(index - degree),@(index + degree)];
}
if(index > left_lower && index < right_lower) {
// 下边线
return@[@(index - degree),@(index -1),@(index +1)];
}
// 其他
return@[@(index - degree),@(index -1),@(index + degree),@(index +1)];