Java “扫雷舰洪水填充”不断出现堆栈溢出错误
我试图在扫雷游戏中使用洪水填充来清除开阔区域。我做了一个简单的泛洪填充函数,但我一直得到堆栈溢出错误 代码Java “扫雷舰洪水填充”不断出现堆栈溢出错误,java,swing,flood-fill,Java,Swing,Flood Fill,我试图在扫雷游戏中使用洪水填充来清除开阔区域。我做了一个简单的泛洪填充函数,但我一直得到堆栈溢出错误 代码 我对它的工作原理有些了解,特别是我跟踪我访问的手机的方式不起作用,但我对此一无所知。这里的问题是,你要检查所有周围的按钮,不管它们是否已经被显示出来。要修复递归调用,请尝试更改if-语句以包围整个代码。像这样: public void revealEmpty(int givenIndex){ if(buttons.get(givenIndex).isEnabled()) {
我对它的工作原理有些了解,特别是我跟踪我访问的手机的方式不起作用,但我对此一无所知。这里的问题是,你要检查所有周围的
按钮,不管它们是否已经被显示出来。要修复递归调用,请尝试更改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;
}
}
public void retainempty(int-givenIndex){
if(buttons.get(givenIndex.isEnabled()){//if语句在此处打开
开放(给定索引);
如果(gm.surroundingbombs(给定指数)=0){
如果(gridsize>GivinIndex+gridwidth)
复仇(给定指数+网格宽度);
如果(gridsize>GivinIndex+gridwidth+1)
复仇(给定指数+网格宽度+1);
如果(gridsize>GivinIndex+gridwidth-1)
复仇(给定指数+网格宽度-1);
if(网格大小<给定索引-网格宽度)
复仇(给定指数-网格宽度);
if(网格大小<给定索引-网格宽度+1)
复仇(给定指数-网格宽度+1);
if(网格大小<给定索引-网格宽度-1)
复仇(给定指数-网格宽度-1);
if(gm.righedge(givenIndex,gridwidth)){//检查按钮是否位于右边缘
报复(给定指数+1);
}
if(gm.leftEdge(givenIndex,gridwidth)){//检查按钮是否按在左边缘上
报复性(给定指数-1);
}
}否则{
返回;
}
}else{//到此结束
返回;
}
}
根据您提供的信息,我不能100%确定这是否能解决您的问题,因此请检查它并告诉我它是否有效。这里的问题是,您正在执行递归调用,但从未退出该方法。请尽快查看以获得更好的帮助,张贴or。这会停止该功能的工作,但我很确定它不工作的原因是我检查按钮是否已打开的方式不工作。
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);
}
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;
}
}