Search code examples
c#recursionstack-overflowminesweeper

recursive stackoverflow minesweeper c#


I am writing a game of minesweeper. Below is code for 3 methods in minesweeper. The first method is to check all the spaces around the button pressed and to count how many bombs are around it. The next method is to be called recursively, in order that if the user pressed a button with 0 buttons around it, it will open all of the squares that also indicate 0 squares around it. The third method is to check that it will be in bound the check. The empty space recursive call is getting me a stackoverflow error, what am I doing wrong?

Thanks!

   private int GameLogicChecker(int x, int y)
    {
        int count = 0;
        if (_grid[x, y] != -1)
        {
            if (x + 1 < SizeX)
            {   //Right
                if (_grid[x + 1, y] == -1)
                    count++;
            }
            if (x - 1 > 0)
            {   //Left
                if (_grid[x - 1, y] == -1)
                    count++;
            }
            if (y + 1 < SizeY)
            {   //Upper
                if (_grid[x, y + 1] == -1)
                    count++;
            }
            if (y - 1 > 0)
            {   //Lower
                if (_grid[x, y - 1] == -1)
                    count++;
            }
            if (x + 1 < SizeX && y + 1 < SizeY)
            {   //Right-Upper
                if (_grid[x + 1, y + 1] == -1)
                    count++;
            }
            if (x + 1 < SizeX && y - 1 > 0)
            {   //Right-Lower
                if (_grid[x + 1, y - 1] == -1)
                    count++;
            }
            if (x - 1 > 0 && y + 1 < SizeY)
            {   //Left-Upper
                if (_grid[x - 1, y + 1] == -1)
                    count++;
            }
            if (x - 1 > 0 && y - 1 > 0)
            {   //Left-Lower
                if (_grid[x - 1, y - 1] == -1)
                    count++;
            }
        }
        return count;
    }

    void OpenEmptySpace(int x, int y)
    {
        for (var k = -1; k <= 1; k++)
        {
            for (var l = -1; l <= 1; l++)
            {
                if (CheckBounds(x + k, y + l) && GameLogicChecker(x + k, y + l) == 0)
                {
                    _buttons[x + k, y + l].Text = "0";
                    OpenEmptySpace(x + k, y + l);
                }
            }
        }
    }

    private bool CheckBounds(int x, int y)
    {
        return x >= 0 && x < SizeX && y >= 0 && y < SizeY;
    }

Solution

  • For k = 0 and l = 0, you are calling yourself again and again and again...


    Thanks to @BenVoigt for pointing out that two zeroes adjacent to each other will also lead to infinite recursion. So, in order to solve that one method is to create a boolean grid too and set a particular cell's value to true if it has been run through once. Assuming the grid is called Explored, I've added the condition for it in the code below.


    If you insist on your current code, try changing the condition to:

    if (CheckBounds(x + k, y + l) 
        && GameLogicChecker(x + k, y + l) == 0 
        && !(k == 0 && l == 0)
        && !Explored[x + k, y + l])
    {
        Explored[x + k, y + l] = true;
        _buttons[x + k, y + l].Text = "0";
        OpenEmptySpace(x + k, y + l);
    }