我正在为我的黑白棋游戏研究 Minimax 算法,但不知道如何评估棋盘

问题描述

我的 Minimax 算法运行良好,我什至通过记录值的层次结构来检查它。但是,当我尝试评估板时(当我最初作为函数参数提供的深度被命中时),我这样做的方式效率低下。我也试过另一种方式(我会把它放在本文下面),但这一次,它是错误的。

这是评估的初始代码(我认为它是原始的(有点)因为它确实评估了可以采取的潜在行动,而不是整个董事会):

// Here's the board with every square evaluated
const sq_val = [
    [160,-20,20,5,160],[-20,-40,-5,-20],[20,15,3,20],[5,5],[160,160]
]

let elementVals = (AI === "black") ? 0 : 1
if (!isMaximizing) {
    elementVals = (AI === "black") ? 1 : 0
}
const movesAvailable = checkSquaresForSides(my_game_board)[elementVals] //All the potential moves for the current player (AI if it is the maximizing player's turn,our player if not)
let bestscore = 0 // The value that we'll return

// In this code,it checks the value of every potential move and set the value of bestscore to the highest one
for (var moveMax=0;moveMax<movesAvailable.length;moveMax++) {
    const coord = movesAvailable[moveMax].coordinates
    if (coord) {
        const value = sq_val[coord[0]][coord[1]]
        bestscore += value
    }
}

// If it is the minimizing player's turn,since it's best move would be the worst case for us,get the opposite of the value
bestscore = (isMaximizing) ? bestscore : -bestscore

// Return the value
return bestscore

这是我尝试的另一种方法

// declare the maximizing evaluation board
const sq_val = [
    [100,100],[100,100]
]
// Get all the stones on the board (black and white seperated)
const allStonesSep = []
for (var row=0;row<8;row++) {
    for (var col=0;col<8;coL++) {
        let elem = board[row][col]
        if (isMaximizing) {
            if (elem === AI) {allStonesSep.push(sq_val[row][col])}
        } else {
            if (elem !== ourPlayer) {allStonesSep.push(sq_val[row][col])}
        }

    }
}

// declare the bestscore
let bestscore = allStonesSep.reduce((a,b) => a + b,0)

if (!isMaximizing) {
    bestscore = -bestscore
}
// Handle the value depending on the maximizing player color and the value of maximizing
// Return the value
return bestscore

我觉得没必要放极小极大算法代码;但如果您认为有必要,请发表评论

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)