Question

I know this has been asked before, but I was not able to figure this one out.

I have a 7x7 board, for a connect-4ish game.

I defined this method, to implement Minimax's Alpha Beta pruning.

It should be returning me the heuristic, and setting the best move. But I'm always getting the best move, as being the last available move on my board....

IS there something here I might be missing?

Thank you!

private int alphaBeta(Node node, int depth, int alpha, int beta, bool max)
{

    if (depth == 0 || node.getBoard().noMorePlays())
    {
        return node.getBoard().heuristic(max ? 1 : 2);
    }

    if (max)
    {

        foreach (Node child in node.Children( (max ? 1 : 2)) )
        {

            alpha = Math.Max(alpha, alphaBeta(child, depth - 1, alpha, beta, !max));

            this.bestNode = child;
            if (beta <= alpha)
            {
                break;
            }

        }


        return alpha;
    }
    else
    {
        foreach (Node child in node.Children((max ? 1 : 2)))
        {

            beta = Math.Min(beta, alphaBeta(child, depth - 1, alpha, beta, !max));

            if (beta <= alpha)
            {
                break;
            }

        }

        return beta;
    }


}
Was it helpful?

Solution

There is no condition around this.bestNode = child;, so yes it always picks the last of the list.

I may have the logic inversed but it should look something like:

//alpha = Math.Max(alpha, alphaBeta(child, depth - 1, alpha, beta, !max));
int temp = alphaBeta(child, depth - 1, alpha, beta, !max);
if (temp > alpha)
{
    this.bestNode = child;
    alpha = temp;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top