基于文本的Minesweeper克隆在Java中的空白扩展

时间:2018-06-01 01:20:33

标签: java minesweeper

当问题出现时,我正在用Java制作基于文本的Minesweeper克隆。 如果你点击一个黑色空间,应该显示该空白区域周围的所有空白区域以及该空白区域周围的所有空白区域等等。

到目前为止的代码:

  public void exposeAround(){
    //Row = inputRow and Column = inputColumn
    for(int row = Row-1; row < Row+1; row++){
        for(int col = Column-1; col < Column+1; col++){
            if((row < 0) || (row > 8) || (col < 0) || (col < 8)){
                continue;
            } else{
                if(mines[row][col] == '.'){
                    board[row][col] = mines[row][col];
                    System.out.println("Test");
                    exposeAround();
                } else{
                    board[row][col] = mines[row][col];
                }
            }   
        }
    }
}

它不会抛出任何错误,但在运行此方法后它不会执行任何,即使在我运行printBoard();方法

之后也是如此

如果您对我的代码的其他部分有任何疑问,请与我们联系。 我试图找到这个答案,但我能找到的唯一的东西是基于GUI的扫雷

编辑:谢谢大家的帮助,我解决了我的问题。

1 个答案:

答案 0 :(得分:0)

您可以使用简单的Floodfill算法打开空白区域。

看起来像这样:

public void exposeAround(int x1, int y1){
    //if position (x1, y1) is a bomb or number
    //    return
    //if outOfBounds(x1, y1)
    //    return
    //open up location (x1, y1)
    //exposeAround(x1-1, y1)    - flood north
    //exposeAround(x1+1, y1)    - flood south
    //exposeAround(x1-1, y1-1)  - flood west
    //exposeAround(x1-1, y1+1)  - flood east
}

所以它会“泛滥”所有4个方向。一次1个方向。当它到达边界(炸弹,数量,2D阵列的边界)时,它会回溯到最后的好位置以继续“泛滥”过程。