Java 爪哇扫雷舰右侧未计算炸弹数量

Java 爪哇扫雷舰右侧未计算炸弹数量,java,logic,minesweeper,Java,Logic,Minesweeper,为了练习,我正在学习扫雷舰教程,但在计算每个广场周围的炸弹数量时,它不会正确计算炸弹数量,我也不完全确定问题出在哪里。我尝试过重新启动,在多个编译器中打开它,移动它,什么都没有。我看了又看,找不到任何逻辑错误 下面是我的计数代码。最后一个if语句是向右数平方的语句 btnAmt=10 背景是一个二维数组,它保存所有的值 如果错误不在这里,我可以发布完整的代码,但我很沮丧,因为似乎没有逻辑错误,而且所有其他方向都有效 //Count neightbouring mines for(int x =

为了练习,我正在学习扫雷舰教程,但在计算每个广场周围的炸弹数量时,它不会正确计算炸弹数量,我也不完全确定问题出在哪里。我尝试过重新启动,在多个编译器中打开它,移动它,什么都没有。我看了又看,找不到任何逻辑错误

下面是我的计数代码。最后一个if语句是向右数平方的语句

btnAmt=10

背景是一个二维数组,它保存所有的值

如果错误不在这里,我可以发布完整的代码,但我很沮丧,因为似乎没有逻辑错误,而且所有其他方向都有效

//Count neightbouring mines
for(int x = 0; x < background.length; x++){
    for(int y = 0; y < background[0].length; y++){
        int nCount = 0;
        if(background[x][y] != MINE){
            if((x > 0) && (y > 0) && (background[x-1][y-1] == MINE)){ //up and left
                nCount++;
            }
            if(y > 0 && background[x][y-1] == MINE){ //Up
                nCount++;
            }
            if(x < btnAmt-1 && y > 0 && background[x+1][y-1] == MINE){ // Up Right
                nCount++;
            }
            if(x>0 && background[x-1][y] == MINE){ //Left
                nCount++;
            }
            if(x>0 && y<btnAmt-1 && background[x-1][y+1] == MINE){ //Down and left
                nCount++;
            }
            if(x<btnAmt-1 && y<btnAmt-1 && background[x+1][y+1] == MINE){//Down and right
                nCount++;
            }
            if(x<btnAmt-1 && background[x+1][y] == MINE){ //Right
                nCount++;
            }
            background[x][y] = nCount;
        }
    }
}
您从未检查过背景[x][y+1]。这是正确的方向,你所评论的右背景[x+1][y]实际上是向下的


记住mat[i][j]按惯例表示矩阵mat的第i行和第j列。因此,向右转意味着向列中添加1,因此mat[i][j+1]。

我会尝试通过在周围的方块中循环并检查地雷计数,使其更符合逻辑,更容易跟随自己,但在决定我的循环边界后,考虑所有边。例如,类似这样的情况:

// count neighbouring mines
for (int x = 0; x < background.length; x++) {
    for (int y = 0; y < background[0].length; y++) {
        // if a MINE, we don't care about the count
        if (background[x][y] != MINE) {
            int nCount = 0;

            // find the left side of the boundary box
            int xMin = Math.max(x - 1, 0);
            // find the right side of the boundary box
            int xMax = Math.min(x + 1, background.length - 1);
            // find the y min side of the boundary box
            int yMin = Math.max(y - 1, 0);
            // find the y max side of the boundary box
            int yMax = Math.min(y + 1, background[0].length - 1);

            // now loop using the boundaries calculated above
            for (int x2 = xMin; x2 <= xMax; x2++) {
                for (int y2 = yMin; y2 <= yMax; y2++) {
                    // check to make sure not the same squre
                    if (x2 != x || y2 != y) { 
                        // if MINE, then increment nCount by 1
                        nCount += (background[x2][y2] == MINE ? 1 : 0);
                    }
                }
            }
            background[x][y] = nCount;
        }
    }
}

我们不想看到整个程序,但是一个编译、运行并显示您的问题的小子集,您的问题中的代码帖子将是理想的。没有倒计时?这可能属于@Lino:不,我可以毫无疑问地说,这不属于代码审查,因为他没有问如何使代码更好。相反,他问的是目前运行不正常的代码,这使得这个网站更适合这个问题。当然,根据我的第一条评论,这是可以改进的。非常感谢,我知道这是一个简单的错误。很抱歉我这么笨拙,我对编码还很陌生,对GUI的了解也很少,所以我有点不知所措。下一次我会在代码评审中加入类似的内容,我不知道我的fault@JekJek没有得到预期结果的问题不属于代码评审,提出这个问题的人是错的。另外,对StackOverflow表示感谢的标准方式是接受您认为最有帮助的答案。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MineSweeperFun {
    public static void main(String[] args) {
        MineSweeperModel model = new MineSweeperModel(36, 18, 60);
        model.displayGrid();
    }
}
public class MineSweeperModel {
    private static final int MINE = -1;
    private int[][] backgroundGrid;
    private int rows;
    private int cols;
    private int mineCount;

    public MineSweeperModel(int rows, int cols, int mineCount) {
        this.rows = rows;
        this.cols = cols;
        this.mineCount = mineCount;
        backgroundGrid = refreshGrid();
    }

    private int[][] refreshGrid() {
        int[][] grid = new int[rows][cols];
        insertRandomMines(grid);
        calculateNeighborCount(grid);
        return grid;
    }

    private void insertRandomMines(int[][] grid) {
        List<Integer> intList = new ArrayList<>();
        for (int i = 0; i < rows * cols; i++) {
            intList.add(i);
        }
        Collections.shuffle(intList);
        for (int i = 0; i < mineCount; i++) {
            int value = intList.remove(0);
            int row = value / cols;
            int col = value % cols;
            grid[row][col] = MINE;
        }
    }

    public void displayGrid() {
        for (int row = 0; row < backgroundGrid.length; row++) {
            for (int col = 0; col < backgroundGrid[row].length; col++) {
                int value = backgroundGrid[row][col];
                String txt = ".";
                if (value == MINE) {
                    txt = "*";
                } else if (value > 0) {
                    txt = "" + value;
                }
                System.out.printf("%4s", txt);
            }
            System.out.println();
        }
    }

    private void calculateNeighborCount(int[][] background) {
     // count neighboring mines
        for (int x = 0; x < background.length; x++) {
            for (int y = 0; y < background[0].length; y++) {
                // if a MINE, we don't care about the count
                if (background[x][y] != MINE) {
                    int nCount = 0;

                    // find the left side of the boundary box
                    int xMin = Math.max(x - 1, 0);
                    // find the right side of the boundary box
                    int xMax = Math.min(x + 1, background.length - 1);
                    // find the y min side of the boundary box
                    int yMin = Math.max(y - 1, 0);
                    // find the y max side of the boundary box
                    int yMax = Math.min(y + 1, background[0].length - 1);

                    // now loop using the boundaries calculated above
                    for (int x2 = xMin; x2 <= xMax; x2++) {
                        for (int y2 = yMin; y2 <= yMax; y2++) {
                            // check to make sure not the same squre
                            if (x2 != x || y2 != y) { 
                                // if MINE, then increment nCount by 1
                                nCount += (background[x2][y2] == MINE ? 1 : 0);
                            }
                        }
                    }
                    background[x][y] = nCount;
                }
            }
        }
    }
}
   1   1   1   1   1   .   .   .   .   .   .   1   1   1   .   .   .   .
   *   1   2   *   2   .   .   .   .   .   .   1   *   1   1   1   1   .
   1   1   2   *   2   .   .   1   1   1   .   1   1   1   1   *   1   .
   .   .   1   1   1   .   .   1   *   1   .   .   .   .   2   2   2   .
   .   .   .   1   1   1   .   1   1   1   .   1   1   1   2   *   2   .
   .   1   1   2   *   1   .   .   .   .   1   2   *   2   3   *   3   1
   .   1   *   3   2   1   .   1   1   1   1   *   2   2   *   3   *   1
   .   1   2   *   1   .   .   1   *   2   2   2   1   1   1   2   1   1
   .   .   1   1   1   .   .   1   1   3   *   2   .   .   .   1   1   1
   .   .   .   .   .   .   .   .   .   2   *   2   .   .   .   1   *   1
   1   2   2   1   .   .   .   .   .   2   2   2   .   1   1   2   2   2
   2   *   *   2   1   .   .   .   .   1   *   2   1   2   *   1   1   *
   *   3   3   *   1   .   .   1   1   2   1   2   *   2   2   2   2   1
   1   1   1   1   1   .   .   1   *   1   .   1   1   1   1   *   2   1
   .   .   .   1   1   1   .   1   1   1   .   .   .   .   1   1   2   *
   .   .   .   1   *   1   .   .   .   .   .   .   .   1   1   1   1   1
   .   .   .   1   1   1   .   .   1   1   1   1   1   2   *   2   1   1
   .   .   1   1   1   .   .   .   1   *   1   1   *   3   2   3   *   1
   .   .   1   *   1   .   .   .   1   1   2   2   2   2   *   2   1   1
   .   .   1   1   1   1   1   1   .   1   2   *   1   1   1   1   .   .
   .   .   .   .   .   1   *   1   .   1   *   2   1   .   .   .   .   .
   1   1   1   .   .   1   1   1   .   1   2   2   1   .   .   .   .   .
   1   *   1   .   .   .   .   .   .   .   1   *   1   .   .   .   .   .
   1   1   1   .   .   .   .   1   1   1   1   1   1   .   .   .   .   .
   .   .   .   .   .   .   .   1   *   1   .   .   .   .   .   .   .   .
   .   .   .   .   .   1   1   2   1   1   .   .   .   .   .   .   .   .
   .   .   .   .   1   2   *   1   .   .   .   .   .   .   .   .   .   .
   .   .   .   .   1   *   2   1   .   .   .   .   .   .   .   .   .   .
   .   1   1   1   1   1   1   .   .   .   .   .   .   .   .   .   .   .
   .   1   *   1   .   .   .   .   1   1   1   1   1   2   1   1   .   .
   .   1   1   1   .   .   .   .   1   *   1   1   *   2   *   1   .   .
   .   .   .   1   1   1   .   .   1   2   2   2   1   2   1   1   .   .
   1   1   .   1   *   2   1   1   .   1   *   2   2   2   1   .   .   .
   *   1   .   1   2   3   *   1   1   2   2   2   *   *   2   1   1   .
   1   1   .   .   1   *   2   1   1   *   1   2   3   4   3   *   1   .
   .   .   .   .   1   1   1   .   1   1   1   1   *   2   *   2   1   .