Othello Minimax 算法在 React.js 中不起作用

问题描述

我遇到了一个问题,即 AI 播放器总是播放它遇到的第一个可用移动。我试图让 AI 使用 Minimax 算法。你觉得我能怎么解决

这是代码(带解释):

在展示代码之前的一点见解:

  • 我试图以这样一种方式设置它,即 AI 播放器(无论它是什么颜色)是最大化播放器。因此,如果玩家回合是白色,但白色是 AI 玩家,则 isMaximizing = true。
  • 我想指出这一点,这样玩家在颜色方面的转向和玩家在最大化方面的转向就不会混淆。

我最初使用它的地方:

// AI Hard - Return Minimax
function minimaxAI(){
    // Here's where the minimax function is initially called

    const AI = (ourPlayer === "white") ? "black" : "white"
    const squares = history[history.length - 1].slice()

    // AI is the maximizing player so we get -Infinity,it'll try to increase the value of bestscore
    let bestscore = -Infinity
    let bestMove = null
    // Perform minimax algorithm for each valid move and pick the best score
    
    // Get the valid moves for the AI player
    const AIValidMoves = checkSquaresForSides()[(AI === "black") ? 0 : 1 ]
    for (var aimove=0;aimove<AIValidMoves.length;aimove++){

        // Play the AI moves and generate the new board
        const crds = AIValidMoves[aimove].turned
        crds.unshift(AIValidMoves[aimove].coordinates)
        const newBoard = handleMove(crds,squares,AI,false,false)

        // Check minimax for the new board
        const score = minimax(newBoard,4,false)
        
        // If score is higher than the current one,replace the highest score and the according coordinates
        if (score > bestscore || aimove === 0) {
            bestscore = score
            bestMove = crds
        }
    }
    console.log('Final Value:')
    console.log(bestscore)

    // Play the move(this time on the real board)
    const upcomingAI = handleMove(bestMove)

    // Set it as the latest of the collection of squares
    setHistory(upcomingAI)

    // Useless to kNow for this issue
    setStepNumber(upcomingAI.length - 1)
}

Minimax 算法本身:

function minimax(board,depth,isMaximizing) {
    const AI = (ourPlayer === "white") ? "black" : "white"
    const stones = setStoneCount(board)
    
    // If there's a winner,return Infinity,0 or -Infinity depending on whether AI won,it is a tie or our player won
    if (stones[0] === 0 || stones[1] === 0 || stones[0] + stones[1] === 64) {
        // Return the score of the move if the game is over
        let score = (stones[(AI === "black") ? 1 : 0] === 0) ? Infinity : (stones[stones[0] === stones[1]]) ? 0 : -Infinity
        return score
    } else if (depth === 0) {
        // If the maximal depth is reached,then evaluate the current board by counting all the stones for the both sides and subtracting one from the other appropriately
        const squares = setStoneCount(board)
        let score = 0
        if (AI === "black") {
            score = squares[0] - squares[1]
        } else if (AI === "white") {
            score = squares[1] - squares[0]
        }
        return score
    }
    if (isMaximizing) {
        // Perform minimax if depth !== 0 and isMaximizing,just like we did initially
        let bestscore = -Infinity
        
        // Get the valid moves for the maximizing player
        const AIValidMoves = checkSquaresForSides()[(AI === "black") ? 0 : 1 ] // AI Moves because isMaximizing = true
        for (var aimove=0;aimove<AIValidMoves.length;aimove++){
            const crds = AIValidMoves[aimove].turned
            crds.unshift(AIValidMoves[aimove].coordinates)
            const newBoard = handleMove(crds,board,false)
            // check for isMaximizing
            let maximizingNext = !isMaximizing
            
            // If there's no move to make for the one side,player turn might stay the same. Here,I check for those situations
            const available = checkSquaresForSides(newBoard)
            if (available[0].length === 0) {
                if (AI === "white") {
                    maximizingNext = true
                } else {
                    maximizingNext = false
                }
            } else if (available[1].length === 0){
                if (AI === "black") {
                    maximizingNext = true
                } else {
                    maximizingNext = false
                }
            }
            
            // Perform minimax for the new board(depth is one less)
            const score = minimax(newBoard,depth - 1,maximizingNext)
            
            // If current score is higher than the highest one caught yet,it should be replaced
            if (score > bestscore) {
                bestscore = score
            }
        }
        return bestscore
    } else {
        // Perform minimax if depth !== 0 and isMaximizing
        let bestscore = Infinity

        // Get the valid squares for the minimizing player
        const PlayerValidMoves = checkSquaresForSides()[(AI === "black") ? 1 : 0 ] // Player Moves because isMaximizing = false

        // Play each move one-by-one for the minimizing player
        for (var playerMove=0;playerMove<PlayerValidMoves.length;playerMove++) {
            const crds = PlayerValidMoves[playerMove].turned
            crds.unshift(PlayerValidMoves[playerMove].coordinates)
            const newBoard = handleMove(crds,ourPlayer,false)
            // check for isMaximizing
            let maximizingNext = !isMaximizing
            
            // Just like the prevIoUs one,check whether there's an exceptional situation to be considered with the player turn
            const available = checkSquaresForSides(newBoard)
            if (available[0].length === 0) {
                if (AI === "white") {
                    maximizingNext = true
                } else {
                    maximizingNext = false
                }
            } else if (available[1].length === 0){
                if (AI === "black") {
                    maximizingNext = true
                } else {
                    maximizingNext = false
                }
            }
            
            // Perform minimax for the new board
            const score = minimax(newBoard,maximizingNext)

            // If the current score is lower than the lowest one obtained so far,it should be replaced
            if (score < bestscore) {
                bestscore = score
            }
        }
        return bestscore
    }
}

它最初确实有效,但是当我对达到深度时评估板的方式不满意时。我改了,现在不行了。我不想回到上一个,因为它不准确。但是,为什么 AI 玩家在遇到第一个动作时却没有正确评估?

解决方法

我解决了,只是把板子评估部分改成了这样:

// The initial score
let score = 0
// value board for each square
const sq_val = [
    [160,-20,20,5,160],[-20,-40,-5,-20],[20,15,3,20],[5,5],[160,160]
]

// Our stones' positions are looked for and the according values are added or substracted depending on whether the player found is the maximizing player or the minimizing player(AI Player: Maximizing,Our Player: Minimizing)
for (var row=0;row<8;row++)
    for (var col=0;col<8;col++) {
        if (board[row][col] === AI) {
            score += sq_val[row][col]
        } else if (board[row][col] === ourPlayer) {
            score -= sq_val[row][col]
        }
    }
}
// The score is returned
return score

顺便说一下,算法现在已经设置好了,但它不是那么准确,因为我需要增加它的深度;为了做到这一点,我必须首先实施 alpha-beta 修剪,这样我们的计算机就不会过热。同时使函数异步在性能方面会更好。