Java扫雷器在炸弹周围添加数字的方法不正确

Java扫雷器在炸弹周围添加数字的方法不正确,java,minesweeper,Java,Minesweeper,我为我的java扫雷游戏编写了这个方法,它应该检查一组坐标周围的点,然后计算附近有多少炸弹 public void numberMines(){ int count = 0; int x = 0; int y = 0; int xMin = x-1; int xMax = x+1; int yMin = y-1; int yMax = y+1; if (x == 0){ xMin = 0; } if

我为我的java扫雷游戏编写了这个方法,它应该检查一组坐标周围的点,然后计算附近有多少炸弹

public void numberMines(){
    int count = 0;
    int x = 0;
    int y = 0;
    int xMin = x-1;
    int xMax = x+1;
    int yMin = y-1;
    int yMax = y+1; 
    if (x == 0){
        xMin = 0;
    }
    if (y == 0){
        yMin = 0;   //these restrictions take care of the spots in the edges
    }
    if (x == rows){
        xMax = rows;
    }
    if (y == columns){
        yMax = columns;
    }
    //first 2 loops go through every spot on the board
    for (x=0; x<rows; x++){
        for (y=0; y<columns; y++){
            //if the spot selected is not a bomb, for loops check spaces surrounding it
            if (mineBoard[x][y] != bomb){
                for (int i = xMin; i <=xMax; i++){
                    for (int j = yMin; j <=yMax; j++){
                        if (mineBoard[i][j] == bomb){
                            count++;
                        }
                    }
                }
            }

            if (count > 0){       //converts them characters
                mineBoard[x][y] = (char)(count + '0');
                count = 0;
            }
         }
    }
}
public void numberMines(){
整数计数=0;
int x=0;
int y=0;
int xMin=x-1;
int xMax=x+1;
int-yMin=y-1;
int yMax=y+1;
如果(x==0){
xMin=0;
}
如果(y==0){
yMin=0;//这些限制用于处理边上的斑点
}
如果(x==行){
xMax=行;
}
if(y==列){
yMax=列;
}
//前2个回路穿过电路板上的每个点

对于(x=0;x移动此代码块:

int xMin = x-1;
int xMax = x+1;
int yMin = y-1;
int yMax = y+1; 
if (x == 0){
    xMin = 0;
}
if (y == 0){
    yMin = 0;   //these restrictions take care of the spots in the edges
}
if (x == rows){
    xMax = rows;
}
if (y == columns){
    yMax = columns;
}
在for循环的内部:

for (x=0; x<rows; x++){
    for (y=0; y<columns; y++){
       //Insert code here <---

但是在
x
/
y
循环中仍然有整个块。当
x
y
处于最大值时,你不会得到越界错误,因为你永远不会计算
xMax
yMax

避免在方法开始时声明所有变量,最好在使用它们时将它们声明为接近。要解决您的问题,您需要在循环中计算count、xMin、xMax、yMin和yMax,如下所示:

public void numberMines(){
    //first 2 loops go through every spot on the board
    for (int x=0; x<rows; x++){
        for (int y=0; y<columns; y++){
            int count = 0;
            //if the spot selected is not a bomb, for loops check spaces surrounding it
            if (mineBoard[x][y] != bomb){
                for (int i = (x == 0 ? 0 : x-1); i <= (x == rows ? rows : x+1); i++){
                    for (int j = (y == 0 ? 0 : y-1); j <= (y == rows ? rows : y+1); j++){
                        if (mineBoard[i][j] == bomb){
                            count++;
                        }
                    }
                }
            }

            if (count > 0){       //converts them characters
                mineBoard[x][y] = (char)(count + '0');
            }
         }
    }
}
public void numberMines(){
//前2个回路穿过电路板上的每个点

对于(intx=0;xbut),如果我在其中添加代码块,它会显示一个越界error@FacundoEzequielCandido-请参阅底部的更新-
x
将永远不等于
-其最大可能值为
行-1
y
的类似语句。
public void numberMines(){
    //first 2 loops go through every spot on the board
    for (int x=0; x<rows; x++){
        for (int y=0; y<columns; y++){
            int count = 0;
            //if the spot selected is not a bomb, for loops check spaces surrounding it
            if (mineBoard[x][y] != bomb){
                for (int i = (x == 0 ? 0 : x-1); i <= (x == rows ? rows : x+1); i++){
                    for (int j = (y == 0 ? 0 : y-1); j <= (y == rows ? rows : y+1); j++){
                        if (mineBoard[i][j] == bomb){
                            count++;
                        }
                    }
                }
            }

            if (count > 0){       //converts them characters
                mineBoard[x][y] = (char)(count + '0');
            }
         }
    }
}