简单的扫雷游戏中的stackoverflow异常

问题描述 投票:0回答:2

我正在调用一个递归函数(左键单击正方形),我找不到问题所在。

它应该将背景图像更改为正确的数字,将其设置为“按下”,如果正方形附近的地雷数量为0,它将左键单击它附近的所有正方形。

“板”是2d方阵(Mine类)

private void leftClick(int x, int y)
{
    if (!board[x][y].pressed)
    {
        if (board[x][y].bomb == true)
        {
            board[x][y].BackgroundImage = Properties.Resources.bomb;
            if (MessageBox.Show("You lost! new game?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Form.ActiveForm.Hide();
                Form form2 = new MineSweeper.mainForm();
                form2.ShowDialog();
            }
            else
                Application.Exit();
        }
        else
        {
            switch (board[x][y].numOfMines)
            {
                case 0:
                    board[x][y].BackgroundImage = Properties.Resources._0;
                    if (x - 1 >= 0 && y - 1 >= 0 && x - 1 < row && y - 1 < column && board[x-1][y-1].pressed==false)
                        leftClick(x - 1, y - 1);
                    if (x >= 0 && y - 1 >= 0 && x < row && y - 1 < column && board[x][y - 1].pressed == false)
                        leftClick(x, y - 1);
                    if (x + 1 >= 0 && y - 1 >= 0 && x + 1 < row && y - 1 < column && board[x + 1][y - 1].pressed == false)
                        leftClick(x + 1, y - 1);
                    if (x - 1 >= 0 && y >= 0 && x - 1 < row && y < column && board[x - 1][y].pressed == false)
                        leftClick(x - 1, y);
                    if (x + 1 >= 0 && y >= 0 && x + 1 < row && y < column && board[x + 1][y].pressed == false)
                        leftClick(x + 1, y);
                    if (x - 1 >= 0 && y + 1 >= 0 && x - 1 < row && y + 1 < column && board[x - 1][y + 1].pressed == false)
                        leftClick(x - 1, y + 1);
                    if (x >= 0 && y + 1 >= 0 && x < row && y + 1 < column && board[x ][y + 1].pressed == false)
                        leftClick(x, y + 1);
                    if (x + 1 >= 0 && y + 1 >= 0 && x + 1 < row && y + 1 < column && board[x + 1][y + 1].pressed == false)
                        leftClick(x + 1, y + 1);
                    break;
                case 1:
                    board[x][y].BackgroundImage = Properties.Resources._1;
                    break;
                case 2:
                    board[x][y].BackgroundImage = Properties.Resources._2;
                    break;
                case 3:
                    board[x][y].BackgroundImage = Properties.Resources._3;
                    break;
                case 4:
                    board[x][y].BackgroundImage = Properties.Resources._4;
                    break;
                case 5:
                    board[x][y].BackgroundImage = Properties.Resources._5;
                    break;
                case 6:
                    board[x][y].BackgroundImage = Properties.Resources._6;
                    break;
                case 7:
                    board[x][y].BackgroundImage = Properties.Resources._7;
                    break;
                case 8:
                    board[x][y].BackgroundImage = Properties.Resources._8;
                    break;
            }
        }
        board[x][y].pressed = true;
    }         
}
c# winforms recursion stack-overflow
2个回答
3
投票

在左击其他地雷之前设置board[x][y].pressed = true;。如果你不这样做,其他地雷可能会再次离开原矿。

if (!board[x][y].pressed)
{
    board[x][y].pressed = true;
    ... do the other stuff here
}

0
投票

除了Olivier指出的那个还有另一个递归问题,你还没有命中,几乎肯定不会在测试中。

if (MessageBox.Show("You lost! new game?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Form.ActiveForm.Hide();
                Form form2 = new MineSweeper.mainForm();
                form2.ShowDialog();
            }

而不是以相同的形式再次玩游戏,而是创建一个新表单并隐藏旧表单。虽然每次游戏只递归一层,但它正在耗尽GDI资源(它们不是无限的,如果你泄漏它们就很容易用完),因此如果用户玩足够的游戏就会受到打击。

© www.soinside.com 2019 - 2024. All rights reserved.