Home > Blockchain >  Can't entirely understand behaviour of a section of minimax algorithm
Can't entirely understand behaviour of a section of minimax algorithm

Time:11-18

Can anyone explain me why I need to use if (currentSymbol === players.playerOne.symbol) instead of if (currentSymbol === players.playerTwo.symbol) when the latter one makes WAY more sense to me (but doesn't work unfortunately...). playerOne is a human player (minimizer), a playerTwo is AI (maximizer).

const minimax = (currentBoardArray, currentSymbol, players) => { //object {gameStats, boardArray, players}
        if (game.checkForWin(currentBoardArray) === true && currentSymbol === players.playerTwo.symbol) return {score: 1}
        else if (game.checkForWin(currentBoardArray) === true && currentSymbol === players.playerOne.symbol) return {score: -1}
        else if (game.checkForDraw(currentBoardArray) === true) return {score: 0};

        const moves = []; //moves is an array [[cords, score], [cords, score], ...]
        let availableMoves = availableSquares(currentBoardArray);

        for (let i=0; i<availableMoves.length; i  ) {

            let move = {};
            move.cords = [availableMoves[i][0], availableMoves[i][1]];
            
            currentBoardArray[availableMoves[i][0]][availableMoves[i][1]] = currentSymbol;
            
            if (currentSymbol === players.playerTwo.symbol) {
                let result = minimax(currentBoardArray, players.playerOne.symbol, players);
                move.score = result.score
            }
            else {
                let result = minimax(currentBoardArray, players.playerTwo.symbol, players);
                move.score = result.score
            }
            currentBoardArray[availableMoves[i][0]][availableMoves[i][1]] = 0;
            moves.push(move)
        }

        let bestTestPlay = null;

        if (currentSymbol === players.playerOne.symbol) { //moves is an array [[cords, score], [cords, score], ...]
            let bestScore = -Infinity;
            for (let i=0; i<moves.length; i  ) {
                if (moves[i].score > bestScore) {
                    bestScore = moves[i].score;
                    bestTestPlay = i
                }
            }
        } else {
            let bestScore = Infinity;
            for (let i=0; i<moves.length; i  ) {
                if (moves[i].score < bestScore) {
                    bestScore = moves[i].score;
                    bestTestPlay = i
                }
            }
        }
        return moves[bestTestPlay]
        }

I tried reviewing the code yet still can't find logic in that.

CodePudding user response:

Some analysis of the code:

currentBoardArray[availableMoves[i][0]][availableMoves[i][1]] = currentSymbol;

This statement indicates that at the start of the function, currentSymbol indicates the player that has the next move (since this statement performs that move).

Who is maximizing?

if (game.checkForWin(currentBoardArray) === true && currentSymbol === players.playerTwo.symbol) return {score: 1}

Here we test whether the game is over, and whether it would have been the second player's turn to move. If that is the case, then the first player won and the second player cannot play a move. The (positive) score indicates that the first player is the maximizing player.

So your assumption that player one was the minimizing player is wrong. Player one is the maximizing player.

  • Related