java扫雷游戏中的Stackoverflow异常

发布于 2024-11-19 22:06:47 字数 454 浏览 4 评论 0原文

我试图用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丶视觉 2024-11-26 22:06:47

您需要在相邻矿井上调用 setClicked,而不是在原始矿井上,否则,您将在源矿井中一遍又一遍地调用 setClicked(true)

public void setClicked(boolean clicked){
    this.clicked = clicked;
    if(adjacentMines == 0)
        for(mine m : adjacent){
            if(!m.isClicked() && !m.isMine()){
                m.setClicked(true); // setClicked should be called on the adjacent mine, not on itself!
            }
        }
}

You need to call setClicked on the adjacent mine, not on original mine, otherwise, you'll get setClicked(true) called over and over again for the source mine

public void setClicked(boolean clicked){
    this.clicked = clicked;
    if(adjacentMines == 0)
        for(mine m : adjacent){
            if(!m.isClicked() && !m.isMine()){
                m.setClicked(true); // setClicked should be called on the adjacent mine, not on itself!
            }
        }
}
草莓酥 2024-11-26 22:06:47

您正在对同一个矿井而不是相邻的矿井调用 setClicked。

更改为:

public void setClicked(boolean clicked){
    this.clicked = clicked;
    if(adjacentMines == 0)
        for(mine m : adjacent){
            if(!m.isClicked() && !m.isMine()){
                //missing the "m."
                m.setClicked(true);
            }
        }
}

You are calling setClicked on the same mine instead of the adjacent ones.

Change to:

public void setClicked(boolean clicked){
    this.clicked = clicked;
    if(adjacentMines == 0)
        for(mine m : adjacent){
            if(!m.isClicked() && !m.isMine()){
                //missing the "m."
                m.setClicked(true);
            }
        }
}
吻泪 2024-11-26 22:06:47

好吧,我只能猜测 setClicked() 方法是 mine 的成员,但您不应该调用 m.setClicked(true) > 在你的条件中而不是仅仅 setClicked(true)

Well, I can only guess that setClicked() method is a member of mine, but shouldn't you be calling m.setClicked(true) inside your condition instead of just setClicked(true)?

夏末 2024-11-26 22:06:47

很难从您的代码片段中看出,但我想说您不会迭代单元格。这意味着您会一遍又一遍地递归地检查同一个单元格(直到无穷大并超出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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文