[Solved] Increase stack size c#


What you can do is: If Creation fails pass back a bool and try again from scratch.

public void Generate()
{
    Fill();
    while(!Fleet())
    {
        // I assume Fill clears your board again?!
        Fill();
    }
}

public bool Fleet()
{
    return Ship2(Utility.R(1,9),Utility.R(1,9),Utility.R(4)) &&
           Ship3(Utility.R(1,9),Utility.R(1,9),Utility.R(4)) &&
           Ship3(Utility.R(1,9),Utility.R(1,9),Utility.R(4)) &&
           Ship4(Utility.R(1,9),Utility.R(1,9),Utility.R(4)) &&
           Ship5(Utility.R(1,9),Utility.R(1,9),Utility.R(4));
}

public bool Ship2(int x, int y, int r)
{
    if (r == 0)
    {
        return CreateShip(x, y) &&
               CreateShip(x, (y + 1));
    }

    if (r == 1)
    {
        return CreateShip(x, y) &&
               CreateShip(x, (y + -1));
    }

    if (r == 2)
    {
        return CreateShip(x, y) &&
               CreateShip((x-1), (y));
    }

    if (r == 3)
    {
        return CreateShip(x, y) &&
               CreateShip((x+1), (y));
    }

    return false;
}

public bool CreateShip(int x, int y)
{
    if (x <= 9 && y <= 9 && x >= 0 && y >= 0)
    {
        if (Board[x, y] == 'L')
        {
            return false;
        }
        else
        {
            Board[x, y] = 'L';
        }
    }
    else
    {
        return false;
    }

    return true;
}

Note: This will still not work, if the input parameters to CreateShip never change. You will now not see any Exception but the Generate method will never finish.

solved Increase stack size c#