Java扫雷器堆栈溢出错误递归

Java扫雷器堆栈溢出错误递归,java,recursion,Java,Recursion,我想做一个控制台版的扫雷舰 尽管我目前在努力,但我无法确定扫雷舰的“洪水填充”部分,如果所选广场周围的邻居没有炸弹,我们现在必须检查这些相邻的广场以找到相邻的炸弹 以下代码适用于所选正方形与炸弹相邻的情况: // checking the adjacent cells, I made -1 = to the bomb value, rest of the cells // are default(0) public void sweep(int r, int c) { if (r &l

我想做一个控制台版的扫雷舰

尽管我目前在努力,但我无法确定扫雷舰的“洪水填充”部分,如果所选广场周围的邻居没有炸弹,我们现在必须检查这些相邻的广场以找到相邻的炸弹

以下代码适用于所选正方形与炸弹相邻的情况:

// checking the adjacent cells, I made -1 = to the bomb value, rest of the cells
// are default(0)
public void sweep(int r, int c) {

    if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length) 
        return;

    int minRow = 0, minCol = 0, maxRow = 0, maxCol = 0, neighborBomb = 0;


    // means if I clicked a bomb, end the program
    if (grid[r][c] == -1) {
        System.out.println("Your selection [" + r + ", " + c + "] contained a bomb. .\nGAME OVER");
        System.exit(0);
    }

    // Series of if/else to find the min & max row col size to avoid going out of
    // bounds
    if (r == 0)
        minRow = 0;
    else
        minRow = r - 1;

    if (r == grid.length - 1)
        maxRow = r;
    else
        maxRow = r + 1;

    if (c == 0)
        minCol = 0;

    else
        minCol = c - 1;

    if (c == grid[0].length - 1)
        maxCol = c;
    else
        maxCol = c + 1;
    //if the selected cell is 0 & has not been searched yet.
    if (grid[r][c] == 0 && recurseSearch[r][c] == false) {

        recurseSearch[r][c] = true;
        // search adjacent cells to see how many bombs surround the cell in question
        neighborBomb = 0;
        for (int row = minRow; row <= maxRow; row++) {

            for (int col = minCol; col <= maxCol; col++) {

                if (grid[row][col] == -1) {
                    neighborBomb++;
                }

            }
        }
    }
        // cell will now display how many bombs are adjacent
        if (neighborBomb > 0) {
            grid[r][c] = neighborBomb;
            return;
        }
        //HERE I WANT TO CHECK ALL ADJACENT SQUARES BUT IT WILL ONLY RUN
        //sweep(r+1, c) rather than all the surrounding squares
        else {
            sweep(r + 1, c);
            sweep(r - 1, c);
            sweep(r + 1, c + 1);
            sweep(r + 1, c - 1);
            sweep(r - 1, c + 1);
            sweep(r - 1, c - 1);
            sweep(r, c + 1);
            sweep(r, c - 1);
        }
}
它会重复那些

   sweep(r + 1, c);
   sweep(r - 1, c);

任何想法/建议都将有助于如何让我的递归实际检查原始r,c选择的每个相邻单元

检查后递归调用方法

//if the selected cell is 0 & has not been searched yet.
if (grid[r][c] == 0 && recurseSearch[r][c] == false) {
    recurseSearch[r][c] = true;
   // ...
}
因此,对已访问的字段重复递归


但是递归应该只发生在这个if块中。

检查后递归调用方法

//if the selected cell is 0 & has not been searched yet.
if (grid[r][c] == 0 && recurseSearch[r][c] == false) {
    recurseSearch[r][c] = true;
   // ...
}
因此,对已访问的字段重复递归


但是递归应该只发生在这个if块中。

如果一个单元格已经用
recurseSearch[][]
进行了处理,那么您保持跟踪的想法是不错的,但是您没有正确地中断递归

将第一个表达式更改为:

if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || recurseSearch[r][c]) 
    return;
recurseSearch[r][c] = true;
if(r<0 | | r>=grid.length | c<0 | | c>=grid[0]。length | |递归搜索[r][c])
返回;
递归搜索[r][c]=true;

如果一个单元格已经用
递归搜索[][]
处理过,那么您跟踪该单元格的想法是不错的,但是您没有正确地中断递归

将第一个表达式更改为:

if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || recurseSearch[r][c]) 
    return;
recurseSearch[r][c] = true;
if(r<0 | | r>=grid.length | c<0 | | c>=grid[0]。length | |递归搜索[r][c])
返回;
递归搜索[r][c]=true;

欢迎使用堆栈溢出!请带上,四处看看,并通读,尤其是和。请提供您收到的完整错误消息和/或堆栈strace。@TimothyTruckle感谢您的欢迎和有用的建议,我将尽我所能在将来遵循它们!你的解决方案是一种程序性的解决问题的方法。一般来说,过程方法没有什么错,但Java是一种面向对象的编程语言,如果你想成为一名优秀的Java程序员,你应该开始用面向对象的方法解决问题。欢迎使用堆栈溢出!请带上,四处看看,并通读,尤其是和。请提供您收到的完整错误消息和/或堆栈strace。@TimothyTruckle感谢您的欢迎和有用的建议,我将尽我所能在将来遵循它们!你的解决方案是一种程序性的解决问题的方法。一般来说,过程方法没有错,但Java是一种面向对象的编程语言,如果你想成为一名优秀的Java程序员,你应该开始用面向对象的方法解决问题。这让我找到了我想要的答案。我想澄清一下,所以在本例中,我在检查后使用了递归,这就是为什么递归没有正确执行?@WastingTime47“我想澄清一下,所以在本例中,我在检查后使用了递归,这就是为什么递归没有正确执行?”是的。这让我找到了我想要的答案。我想澄清一下,所以在本例中,我在检查后使用了递归,这就是为什么递归没有正确执行?@WastingTime47“我想澄清一下,所以在本例中,我在检查后使用了递归,这就是为什么递归没有正确执行?”是的。