Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get minimax move in string

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading