0

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

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

// Here's the board with every square evaluated
const sq_val = [
    [160, -20,  20,   5,   5,  20, -20, 160],
    [-20, -40,  -5,  -5,  -5,  -5, -40, -20],
    [20,  -5,  15,   3,   3,  15,  -5,  20],
    [5,  -5,   3,   3,   3,   3,  -5,   5],
    [5,  -5,   3,   3,   3,   3,  -5,   5],
    [20,  -5,  15,   3,   3,  15,  -5,  20], 
    [-20, -40,  -5,  -5,  -5,  -5, -40, -20],   
    [160, -20,  20,   5,   5,  20, -20, 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, -20,  20,   5,   5,  20, -20, 100],
    [-20, -40,  -5,  -5,  -5,  -5, -40, -20],
    [20,  -5,  15,   3,   3,  15,  -5,  20],
    [5,  -5,   3,   3,   3,   3,  -5,   5],
    [5,  -5,   3,   3,   3,   3,  -5,   5],
    [20,  -5,  15,   3,   3,  15,  -5,  20], 
    [-20, -40,  -5,  -5,  -5,  -5, -40, -20],   
    [100, -20,  20,   5,   5,  20, -20, 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

我不认为放置 Minimax 算法代码是必要的;但如果您认为需要它,请发表评论。

4

0 回答 0