英文:
Unable to change values of elements around a point in a 2d Array
问题
我目前正在编写一个用Java运行扫雷游戏的程序,但是这个特定的部分一直在给我带来问题。我正在尝试编辑一个类似这样的小的二维数组:{ { 0, -1 }, { 0, 0 } };并且它应该被编辑成一个看起来像这样的场地:{ { 1, -1 }, { 1, 1 } };但是据我观察,它目前没有改变任何值。基本上,我只是想在与炸弹(值为-1)相邻的八个单元格中添加1,除非该单元格本身就是炸弹(-1),但是迄今为止我没有任何运气。我非常感谢您能提供的任何帮助!
public static void setHint(int[][] field)
{
for (int x=0; x < field.length - 1; x++) {
for (int y=0; y < field[0].length - 1; y++) {
if (field[x][y] == -1) {
if (field[x-1][y-1] != -1) {
field[x-1][y-1] += 1;
}
if (field[x-1][y+1] != -1) {
field[x-1][y+1] += 1;
}
if (field[x+1][y-1] != -1) {
field[x+1][y-1] += 1;
}
if (field[x+1][y+1] != -1) {
field[x+1][y+1] += 1;
}
if (field[x-1][y] != -1) {
field[x-1][y] += 1;
}
if (field[x+1][y] != -1) {
field[x+1][y] += 1;
}
if (field[x][y-1] != -1) {
field[x][y-1] += 1;
}
if (field[x][y+1] != -1) {
field[x][y+1] += 1;
}
}
}
}
}
英文:
I am currently coding a program that will run minesweeper in java, but this certain section has been giving me issues. I am trying to edit a small 2d array, like: { { 0, -1 }, { 0, 0 } }; and it should edit it into a field that looks like this: { { 1, -1 }, { 1, 1 } }; but is currently not changing any of the values as far as I can tell. Basically, I am just trying to add 1 to the eight cells surrounding a bomb (value of -1), unless that cell is itself a bomb (-1), but have not had any luck so far. I would greatly appreciate any help you could offer!
public static void setHint(int[][] field)
{
for (int x=0; x < field.length - 1; x++) {
for (int y=0; y < field[0].length - 1; y++) {
if (field[x][y] == -1) {
if (field[x-1][y-1] != -1) {
field[x-1][y-1] += 1;
}
if (field[x-1][y+1] != -1) {
field[x-1][y+1] += 1;
}
if (field[x+1][y-1] != -1) {
field[x+1][y-1] += 1;
}
if (field[x+1][y+1] != -1) {
field[x+1][y+1] += 1;
}
if (field[x-1][y] != -1) {
field[x-1][y] += 1;
}
if (field[x+1][y] != -1) {
field[x+1][y] += 1;
}
if (field[x][y-1] != -1) {
field[x][y-1] += 1;
}
if (field[x-1][y-1] != -1) {
field[x-1][y-1] += 1;
}
}
}
}
}
答案1
得分: 0
field.length 是2。因此条件 x < field.length - 1
仅对 x=0
成立。y
的情况类似。因此,你只会评估场地 (0,0)
。由于 field[0][0]
的值为0,所以 if 条件为假,因此不会执行任何操作。
因此,你可以首先通过更改循环的结束条件来进行修改:
for (int x=0; x < field.length; x++) {
for (int y=0; y < field[0].length; y++) {
你还需要验证是否没有在矩阵外进行测试。当 x=0
时,在 field[x-1][y]
上进行检查将导致 IndexOutOfBoundException
,而在边缘位置(其中 x=0
、x=length-1
、y=0
、y=length-1
)的所有字段都将出现这种情况。
英文:
field.length is 2. So condition x < field.length -1
is only true for x=0
. The same for y
. So you only evaluate the field (0,0)
. Since the value of field[0][0]
is 0, the if condition is false so nothing is executed.
So you can start by changing the end condition of the loops:
for (int x=0; x < field.length; x++) {
for (int y=0; y < field[0].length; y++) {
You also have to verify if you are not testing outside the matrix. When x=0, checking on field[x-1][y] will result in an IndexOutOfBoundException and this will be the case for all fields at the sides (where x=0, x=length-1, y=0,y=length-1)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论