C#C
C#3y ago
IdoS

❔ recursive calls result in stackoverflow

Hi I have a connect four game logic that I implemented and I try to find the problem.
I have a 6 x 7 array that is initialized with minus one for everyone on each turn I put the player's number where he chose to put say computer 0 player 1 the check is recursive in the form of an asterisk from where the player put the coin to check sequences of his coins.
I understand it's because a bad halt check but I don't understand why is that wrong
this is the code:
 private int IsHorizontalWin(int x, int y, int playerFlag)
    {
        if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
            return 0;
        return 1 + IsHorizontalWin(x + 1, y, playerFlag) + IsHorizontalWin(x - 1, y, playerFlag);
    }
    private int IsVerticalWin(int x, int y, int playerFlag)
    {
        if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
            return 0;
        return 1 + IsVerticalWin(x, y - 1, playerFlag) + IsVerticalWin(x, y + 1, playerFlag);
    }

    private int IsDiagonalRightWin(int x, int y, int playerFlag)
    {
        if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
            return 0;
        return 1 + IsDiagonalRightWin(x + 1, y - 1, playerFlag) + IsDiagonalRightWin(x - 1, y + 1, playerFlag);

    }

    private int IsDiagonalLeftWin(int x, int y, int playerFlag)
    {
        if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
            return 0;
        return 1 + IsDiagonalLeftWin(x + 1, y + 1, playerFlag) + IsDiagonalLeftWin(x - 1, y - 1, playerFlag);

    }
Was this page helpful?