Connect-four Minimax algorithm recursion

91 Views Asked by At

I'm working on making a Connect-four game where the AI is the minimax algorithm.

I'm stuck at the point where if depth == 0, the function just calls itself to the infinite and I have no clue what to do.

public static int Minimax(int depth, int alpha, int beta, bool maximizingPlayer) 
{
    Console.WriteLine(bestCol);
    mxcopy(); //to copy the original board sot he ai can play with
    int eval = 0;
    int score=0; // call the evaluation function to get the initial score

    if (depth == 0)
    {
        score = evaluate(maximizingPlayer ? 1 : -1);
    }
    if (maximizingPlayer)
    {
        eval = evaluate(1); //evaluates how favourble the position is for the maximizing player
        int bestScore = int.MinValue / 2; // initialize bestScore to a very low value
        for (int column = 0; column < 7; column++)
        {
            for (int row = 5; row >= 0; row--)
            {
                if (mx[row, column] != 0)
                {
                    continue;
                }
                mx[row, column] = 1; // make the move
                if (depth > 0)
                {
                    score = Minimax(depth - 1, alpha, beta, !maximizingPlayer);
                }
                 // recursively call Minimax with the next depth
                score = score + eval;
                mx[row, column] = 0; // undo the move
                if (score > bestScore)
                {
                    bestScore = score;
                    bestRow = row;
                    bestCol = column;
                }
                alpha = Math.Max(alpha, score);
                if (beta <= alpha)
                {
                    break; // beta pruning
                }
            }
        }
        return bestScore;
    }
    else
    {
        eval = evaluate(-1); //evaluate how favourble the current position is for the minimizing player
        int bestScore = int.MaxValue / 2; // initialize bestScore to a very high value
        for (int column = 0; column < 7; column++)
        {
            for (int row = 5; row >= 0; row--)
            {
                if (mx[row, column] != 0)
                {
                    continue;
                }
                mx[row, column] = -1; // make the move
                if (depth > 0)
                {
                    score = Minimax(depth - 1, alpha, beta, !maximizingPlayer);
                }
                mx[row, column] = 0; // undo the move
                score = score + eval;
                if (score < bestScore)
                {
                    bestScore = score;
                    bestRow = row;
                    bestCol = column;
                }
                bestScore = Math.Min(bestScore, score); // update the best score
                beta = Math.Min(beta, score);
                if (beta <= alpha)
                {
                    break; // alpha pruning
                }
            }
        }

        return bestScore;
    }
}

when depth hits 0 it just returns the evaluation of the current player.

I tried using terminal states so when the game is over, or there are no more moves left to do, the game should continue as usual, but I don't know how to implement it.

I think something is wrong with the code structure, what is called when, and in generally how I implemented this algorithm

0

There are 0 best solutions below