我正在遵循有关构建井字游戏AI(https://www.youtube.com/watch?v=P2TcQ3h0ipQ)的教程。包括用于minimax算法的函数。我想知道如何更改下面的minimax函数(用javascript编写),以使该算法仅评估向前一定数量的移动(即树的深度)?
function minimax(newBoard, player) {
var availSpots = emptySquares(newBoard);
if (checkWin(newBoard, huPlayer)) {
return {score: -10};
} else if (checkWin(newBoard, aiPlayer)) {
return {score: 10};
} else if (availSpots.length === 0) {
return {score: 0};
}
var moves = [];
for (let i = 0; i < availSpots.length; i ++) {
var move = {};
move.index = newBoard[availSpots[i]];
newBoard[availSpots[i]] = player;
if (player === aiPlayer)
move.score = minimax(newBoard, huPlayer).score;
else
move.score = minimax(newBoard, aiPlayer).score;
newBoard[availSpots[i]] = move.index;
if ((player === aiPlayer && move.score === 10) || (player === huPlayer && move.score === -10))
return move;
else
moves.push(move);
}
let bestMove, bestScore;
if (player === aiPlayer) {
bestScore = -1000;
for(let i = 0; i < moves.length; i++) {
if (moves[i].score > bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
} else {
bestScore = 1000;
for(let i = 0; i < moves.length; i++) {
if (moves[i].score < bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
}
return moves[bestMove];
}