java扫雷游戏中的Stackoverflow异常
我试图用Java制作一个扫雷游戏,但我一直遇到这个错误。此函数设置当前要单击的方块以及要单击的任何相邻方块,并递归地继续。当它用完方块时它应该停止,但即使我将字段大小设置为 2x2 且有 0 个地雷,它也会溢出。
public void setClicked(boolean clicked){
this.clicked = clicked;
if(adjacentMines == 0)
for(mine m : adjacent){
if(!m.isClicked() && !m.isMine()){
setClicked(true); //Should be m.setClicked(true);
}
}
}
问题解决了,我漏掉了“m”。在我的方法调用中。感谢大家的帮助。
I was trying to make a minesweeper game in Java, but I keep running into this error. This function sets the current square to clicked and any adjacent squares to be clicked and continues recursively. It should stop when it runs out of squares, but even when I set the field size to 2x2 with 0 mines, it overflows.
public void setClicked(boolean clicked){
this.clicked = clicked;
if(adjacentMines == 0)
for(mine m : adjacent){
if(!m.isClicked() && !m.isMine()){
setClicked(true); //Should be m.setClicked(true);
}
}
}
Problem solved, I was missing the "m." in my method call. Thanks to everyone for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要在相邻矿井上调用
setClicked
,而不是在原始矿井上,否则,您将在源矿井中一遍又一遍地调用setClicked(true)
You need to call
setClicked
on the adjacent mine, not on original mine, otherwise, you'll getsetClicked(true)
called over and over again for the source mine您正在对同一个矿井而不是相邻的矿井调用 setClicked。
更改为:
You are calling setClicked on the same mine instead of the adjacent ones.
Change to:
好吧,我只能猜测
setClicked()
方法是mine
的成员,但您不应该调用m.setClicked(true)
> 在你的条件中而不是仅仅setClicked(true)
?Well, I can only guess that
setClicked()
method is a member ofmine
, but shouldn't you be callingm.setClicked(true)
inside your condition instead of justsetClicked(true)
?很难从您的代码片段中看出,但我想说您不会迭代单元格。这意味着您会一遍又一遍地递归地检查同一个单元格(直到无穷大并超出woosh)。不过,查看周围的代码会非常有用。
Difficult to tell from your code snippet, but I would say you do not iterate over the cells. That means you are recursively checking the same cell over and over (to infinity and beyond woosh). The surrounding code would be very useful to see, though.