gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:19:48 24 4
gpt4 key购买 nike

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

 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com