我正在尝试创建一个函数,该函数将从更大的数组中检查 3x3 数组元素块中的特定元素。
9...84.6.
..4..75.8
.3.......
3....1...
.7.5.6.4.
...4....2
.......5.
5.97..2..
.8.21...4
我想传入一个编号为 0-8 的框,并仅在所选框中查找 x。拼图数组可能与上述类似。
protected static boolean box(int box, int x){
//box is a 3x3 subset of puzzle
// 012
// 345 <--- boxes numbered as such
// 678
boolean present = false;
int coordR = 0, coordC = 0;
switch (box){
case 0:
coordR = 0;
coordC = 0;
case 1:
coordR = 0;
coordC = 3;
case 2:
coordR = 0;
coordC = 6;
case 3:
coordR = 3;
coordC = 0;
case 4:
coordR = 3;
coordC = 3;
case 5:
coordR = 3;
coordC = 6;
case 6:
coordR = 6;
coordC = 0;
case 7:
coordR = 6;
coordC = 3;
case 8:
coordR = 6;
coordC = 6;
}
System.out.print("Box " + box + " -\t");
for (int i = coordR; i < 3; i++){
for (int j = coordC; j < 3; j++){
if (puzzle[i][j] == x){
present = true;
}
System.out.print(puzzle[i][j]);
}
}
System.out.println("");
return present;
}
有没有更有效的方法来做到这一点/使这项工作?