首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java MineSweeper不计算右边的炸弹

Java MineSweeper不计算右边的炸弹
EN

Stack Overflow用户
提问于 2018-05-22 06:18:35
回答 2查看 593关注 0票数 0

为了练习,我遵循了扫雷教程,但它不会计算炸弹在每个方块周围的炸弹数量,我不是完全确定是什么问题。我尝试过重新启动,在多个编译器中打开它,到处移动它,什么都没有。我看了又看,我找不到任何逻辑错误。

下面是我的计数代码。最后一条if语句是对右边的平方进行计数的语句。

btnAmt = 10

background是一个包含所有矿值的二维数组。

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

代码语言:javascript
复制
//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;
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-05-22 06:57:30

你从来没有检查过background[x][y+1]。这是“正确”的方向,您所评论的“正确”(background[x+1][y])实际上是向下的。

请记住,mat[i][j] (按照惯例)表示矩阵mati-th行和j-th列。因此,向右表示将该列加1,因此为mat[i][j+1]

票数 1
EN

Stack Overflow用户

发布于 2018-05-22 06:59:59

我会尝试通过循环遍历周围的方块和检查more计数来使其更具逻辑性和更容易跟随自己,但在确定我的循环边界应该是什么之后,考虑到所有的边。例如,如下所示:

代码语言:javascript
复制
// 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;
        }
    }
}

作为概念证明,下面的程序使用上面的代码,没有经过修改,并且可以正常工作:

代码语言:javascript
复制
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();
    }
}

代码语言:javascript
复制
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;
                }
            }
        }
    }
}

输出示例:

代码语言:javascript
复制
   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   .
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50457277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档