java - Minesweeper flood-fill 不断收到堆栈溢出错误

标签 java swing flood-fill

我正在尝试使用洪水填充来清理扫雷游戏中的开放区域。我做了一个简单的洪水填充函数,但我不断收到堆栈溢出错误 代码

 public void revealEmpty(int givenIndex){
        if(buttons.get(givenIndex).isEnabled())
            open(givenIndex);

            if(gm.surroundingbombs(givenIndex)==0){

                if(gridsize>givenIndex+gridwidth)
                    revealEmpty(givenIndex+gridwidth);

                if(gridsize>givenIndex+gridwidth+1)
                    revealEmpty(givenIndex+gridwidth+1);

                if(gridsize>givenIndex+gridwidth-1)
                    revealEmpty(givenIndex+gridwidth-1);

                if(gridsize<givenIndex-gridwidth)
                    revealEmpty(givenIndex-gridwidth);

                if(gridsize<givenIndex-gridwidth+1)
                    revealEmpty(givenIndex-gridwidth+1);
                if(gridsize<givenIndex-gridwidth-1)

                    revealEmpty(givenIndex-gridwidth-1);

                 if(gm.rightEdge(givenIndex,gridwidth)){//checks if the button pressed is on the right edge

                        revealEmpty(givenIndex+1);
                }

                 if(gm.leftEdge(givenIndex,gridwidth)){//checks if the button pressed ison the left edge

                    revealEmpty(givenIndex-1);
                }



            }   
            else{
                return;
            }



    }

这是用于“打开”网格中的单元格的代码

public void open(int Bindex){
        Font f = new Font("Arial", Font.BOLD, 26);//font for the buttons
        Font f2 = new Font("Arial", Font.BOLD, 15);//font for the move tracker
        if(gm.surroundingbombs(Bindex)!=0){
            buttons.get(Bindex).setBorder(BorderFactory.createBevelBorder(1, Color.LIGHT_GRAY, Color.DARK_GRAY));
            buttons.get(Bindex).setIcon(null);
            if(gm.surroundingbombs(Bindex)!=0)
            buttons.get(Bindex).setText(Integer.toString(gm.surroundingbombs(Bindex)));
            if(small)
            buttons.get(Bindex).setFont(f2);
            else
            buttons.get(Bindex).setFont(f);
            buttons.get(Bindex).setBorderPainted(true);
            buttons.get(Bindex).setEnabled(false);
            buttons.get(Bindex).setContentAreaFilled(true);
            buttons.get(Bindex).setBackground(Color.LIGHT_GRAY);
        }
        else
        buttons.get(Bindex).setBorder(BorderFactory.createBevelBorder(1, Color.LIGHT_GRAY, Color.DARK_GRAY));
        buttons.get(Bindex).setIcon(null);
        buttons.get(Bindex).setBorderPainted(true);
        buttons.get(Bindex).setEnabled(false);
        buttons.get(Bindex).setContentAreaFilled(true);
        buttons.get(Bindex).setBackground(Color.LIGHT_GRAY);


    }

我对它的工作原理有一些了解,特别是我跟踪访问过的单元格的方式不起作用,但我对此一无所知。

最佳答案

这里的问题是您检查所有周围的 JButton,无论它们是否已经显示。要修复递归调用,请尝试更改if 语句以包围整个代码。像这样:

    public void revealEmpty(int givenIndex){
    if(buttons.get(givenIndex).isEnabled()) { //If statement opens here
        open(givenIndex);

        if (gm.surroundingbombs(givenIndex) == 0) {

            if (gridsize > givenIndex + gridwidth)
                revealEmpty(givenIndex + gridwidth);

            if (gridsize > givenIndex + gridwidth + 1)
                revealEmpty(givenIndex + gridwidth + 1);

            if (gridsize > givenIndex + gridwidth - 1)
                revealEmpty(givenIndex + gridwidth - 1);

            if (gridsize < givenIndex - gridwidth)
                revealEmpty(givenIndex - gridwidth);

            if (gridsize < givenIndex - gridwidth + 1)
                revealEmpty(givenIndex - gridwidth + 1);
            if (gridsize < givenIndex - gridwidth - 1)

                revealEmpty(givenIndex - gridwidth - 1);

            if (gm.rightEdge(givenIndex, gridwidth)) {//checks if the button pressed is on the right edge

                revealEmpty(givenIndex + 1);
            }

            if (gm.leftEdge(givenIndex, gridwidth)) {//checks if the button pressed ison the left edge

                revealEmpty(givenIndex - 1);
            }


        } else {
            return;
        }
    } else { //And ends here
        return;
    }


}

根据您提供的信息,我不能 100% 确定这是否会解决您的问题,因此请检查并告诉我它是否有效。

关于java - Minesweeper flood-fill 不断收到堆栈溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48456702/

相关文章:

java - JComponent 和组件左对齐

algorithm - 了解如何使用最少数量的矢量线填充多边形

java - 捕获异常后,模拟器仍然显示 "Application has stopped unexpectedly "

java - 测试 PostgreSQL 中的任何相关行

java - 输入对话框没有图标,只有 OK 选项

java - 计时器无法正确触发

algorithm - 更有效地找到差异较小的最大区域

android - 如何在android中实现flood-fill算法?

java - 如何在带有 Java 的 WebDriver 中使用 XPATH 单击此元素?

java - 排除同时受多个 spring @Profiles 控制的 bean