Home > other >  Get minimax move in string
Get minimax move in string

Time:08-01

I'm following this pseudocode to write my code in Java. Everything is clear and works as expected except for actually capturing the best move. It seems that my added if condition inside the maximizing player condition returns the last playable move in the current board. So it always plays down. How can I track the best possible move?

Here's the pseudocode

function minimax(position, depth, alpha, beta, maximizingPlayer)
    if depth == 0 or game over in position
        return static evaluation of position
 
    if maximizingPlayer
        maxEval = -infinity
        for each move of validMoves
            eval = minimax(child, depth - 1, alpha, beta false)
            maxEval = max(maxEval, eval)
            alpha = max(alpha, eval)

            if depth == 3
                bestmove = move (Is this correct?)

            if beta <= alpha
                break
        return maxEval
 
    else
        minEval =  infinity
        for each move of validMoves
            eval = minimax(child, depth - 1, alpha, beta true)
            minEval = min(minEval, eval)
            beta = min(beta, eval)
            if beta <= alpha
                break
        return minEval
 
 
// initial call
minimax(currentPosition, 3, -∞,  ∞, true)

CodePudding user response:

You're not actually checking that the move is better than the others, so you're just getting the last move.

You need to check the the move's score is higher than that of the others seen before it. Since you already have maxEval which contains the highest score seen so far, you can simply add the check that eval == maxEval before updating bestmove.

  • Related