【问题标题】:Minesweeper program recursion error扫雷程序递归错误
【发布时间】:2011-06-08 08:40:20
【问题描述】:

我正在用 Java 为学校创建一个扫雷程序,但在清除旁边没有任何地雷的方格时遇到了麻烦,该方格应该被禁用,并且所有周围的方格都显示出来,如果还有另一个方块没有接触炸弹,它会执行相同的操作。我收到堆栈溢出错误,我知道这与无限循环有关,但我找不到它卡在我的代码中的位置。

我不知道出了什么问题,所以任何建议都会有所帮助。

这是相关的一段代码(if 语句用于特殊情况,如果单击的方块位于棋盘边缘)

 private void doClear(int y, int x, JButton[][] bArray2, int gridy,int gridx)
{
    clicked--;
    bArray2[y][x].setBackground(lighterGray);
    bArray2[y][x].setEnabled(false);

    if (x > 0 && x<gridx-1 && y> 0 && y<gridy-1)
    {
        clearSquare(y-1, x-1,gridy, gridx, bArray2);
        clearSquare(y-1, x,gridy, gridx, bArray2);
        clearSquare(y-1, x+1,gridy, gridx, bArray2);
        clearSquare(y, x-1,gridy, gridx, bArray2);
        clearSquare(y, x+1,gridy, gridx, bArray2);
        clearSquare(y+1, x-1,gridy, gridx, bArray2);
        clearSquare(y+1, x,gridy, gridx, bArray2);
        clearSquare(y+1, x+1,gridy, gridx, bArray2);
    }
     if(y == 0 && x != 0 && x != gridx-1)  // top row check
                       {
                           clearSquare(y, x-1,gridy, gridx, bArray2);
                           clearSquare(y, x+1,gridy, gridx, bArray2);
                           clearSquare(y+1, x-1,gridy, gridx, bArray2);
                           clearSquare(y+1, x,gridy, gridx, bArray2);
                           clearSquare(y+1, x+1,gridy, gridx, bArray2);

                       } // ends top row check

                        if (y == 0 && x == 0) // corner check top left
                        {
                           clearSquare(y, x+1,gridy, gridx, bArray2);
                           clearSquare(y+1, x,gridy, gridx, bArray2);
                           clearSquare(y+1, x+1,gridy, gridx, bArray2);
                        } // ends top left corner check

                        if (y == 0 && x == gridx-1) // corner check top right row
                         {     
                           clearSquare(y, x-1,gridy, gridx, bArray2);
                           clearSquare(y+1, x-1,gridy, gridx, bArray2);
                           clearSquare(y+1, x,gridy, gridx, bArray2);                               
                        } // ends top right corner check

                        if (x == 0 && y != 0 && y != gridy-1)  //left column check
                        {
                           clearSquare(y-1, x,gridy, gridx, bArray2);
                           clearSquare(y-1, x+1,gridy, gridx, bArray2);
                           clearSquare(y, x+1,gridy, gridx, bArray2);
                           clearSquare(y+1, x,gridy, gridx, bArray2);
                           clearSquare(y+1, x+1,gridy, gridx, bArray2);  
                            } // ends left column check

                            if (x == gridx-1 && y != 0 && y != gridy-1)  // right column check
                            {
                           clearSquare(y-1, x-1,gridy, gridx, bArray2);
                           clearSquare(y-1, x,gridy, gridx, bArray2);
                           clearSquare(y, x-1,gridy, gridx, bArray2);
                           clearSquare(y+1, x-1,gridy, gridx, bArray2);
                           clearSquare(y+1, x,gridy, gridx, bArray2);
                            }// ends right column check

                       if(y == gridy-1 && x != 0 && x != gridx-1)
                       {
                           clearSquare(y-1, x-1,gridy, gridx, bArray2);
                           clearSquare(y-1, x,gridy, gridx, bArray2);
                           clearSquare(y-1, x+1,gridy, gridx, bArray2);
                           clearSquare(y, x-1,gridy, gridx, bArray2);
                           clearSquare(y, x+1,gridy, gridx, bArray2);
                       } // ends bottom row check

                       if (y == gridy-1 && x == 0)  // left bottom corner check
                       {
                           clearSquare(y-1, x,gridy, gridx, bArray2);
                           clearSquare(y-1, x+1,gridy, gridx, bArray2);
                           clearSquare(y, x+1,gridy, gridx, bArray2);

                        } // ends left bottom corner check

                        if (y == gridy-1 && x == gridx-1) // right bottom corner check
                        {
                           clearSquare(y-1, x-1,gridy, gridx, bArray2);
                           clearSquare(y-1, x,gridy, gridx, bArray2);
                           clearSquare(y, x-1,gridy, gridx, bArray2);
                        }//ends right bottom corner check
}

private void clearSquare(int y,int x, int gridy, int gridx, JButton[][] bArray2)
{
        int value = array[y][x];
        System.out.println(value);
        String text = bArray2[y][x].getText();
        if (text == "")
        {
        if (value == 0)
        {
            doClear(y, x, bArray2, gridy, gridx);
        }
        else{
            clicked--;
            bArray2[y][x].setText(""+value);
           }
       }

}

【问题讨论】:

  • 顺便说一句,如果您在顶部和底部有一个额外的行并且在左侧和右侧有一个额外的列,那么您需要的代码会少得多。保持这些隐藏设置它们的初始状态为清除。
  • 我已添加到我的答案中。事实证明,还有另一种方法可以在不添加额外行和列的情况下简化代码。

标签: java recursion stack-overflow


【解决方案1】:
private void doClear(int y, int x, JButton[][] bArray2, int gridy,int gridx)
{
    if (...already cleared...) {
       return;
    }

    ...
}

如果没有该检查,小区 A 将清除相邻小区 B,这将清除相邻小区 A,这将清除相邻小区 B,这...

您发布的代码可以替换为以下内容:

private void handleClick(JButton[][] bGrid, int gridy, int gridx, int y, int x, bool realClick) {
    if (x < 0 || x >= gridx || y < 0 || y >= gridx) {
        return;
    }

    JButton button = bGrid[y][x];
    if (!button.isEnabled()) {
        return;
    }

    if (realClick) {
        --clicked;
    }

    button.setBackground(lighterGray);
    button.setEnabled(false);

    if (...is a mine...) {
        ...
    } else {
        button.setText(array[y][x]);

        if (value == 0) {
            handleClick(bGrid, gridy, gridx, y-1, x-1, false);
            handleClick(bGrid, gridy, gridx, y-1, x,   false);
            handleClick(bGrid, gridy, gridx, y-1, x+1, false);
            handleClick(bGrid, gridy, gridx, y,   x-1, false);
            handleClick(bGrid, gridy, gridx, y,   x+1, false);
            handleClick(bGrid, gridy, gridx, y+1, x-1, false);
            handleClick(bGrid, gridy, gridx, y+1, x,   false);
            handleClick(bGrid, gridy, gridx, y+1, x+1, false);
        }
    }
}

array”需要重命名为有意义的东西!

更新:添加了简化代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多