minesweeper flood-fill不断出现堆栈溢出错误

时间:2018-01-26 06:34:12

标签: 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;
            }



    }

这是使用top&#34; open&#34;网格中的单元格

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);


    }

我对它的工作原理有所了解,特别是我跟踪访问过的细胞的方式不起作用,但我完全不知道。

1 个答案:

答案 0 :(得分:1)

这里的问题是你检查所有周围的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%确定这是否能解决您的问题,请检查并告诉我它是否有效。