我正在尝试为tic-tac-toe实现minimax。问题是,它并不像预期的那样。我试图对其进行调试,并发现如果它没有选择正确的移动,那么该移动的得分是出于某种原因而且#34;负无穷大"。 这是代码:
`
function minimaxMove(board) {
let nextMove = null;
const mmRecurse = function (board, lastPlayer, depth) {
let winner = checkGameState(board);
if(winner == state.AIplayer) {
return -1000 + depth;
} else if(winner == state.playerIs) {
return depth - 1000;
}
let nextPlayer = lastPlayer == state.playerIs ? state.AIplayer : state.playerIs;
let moves = [], scores = [];
for(let i = 0; i < state.board.length; i++) {
let boardCopy = board.slice();
if(boardCopy[i] == "E") {
boardCopy[i] = nextPlayer;
moves.push(i);
scores.push(mmRecurse(boardCopy, nextPlayer, depth+1));
}
}
if(depth === 0) {
nextMove = moves[scores.indexOf(Math.max.apply(null,scores))];
console.log(moves, scores, nextMove);
} else {
if(depth == 9) {
return 0;
}
if(nextPlayer == state.AIplayer) {
return Math.max.apply(null, scores);
} else if(nextPlayer == state.playerIs){
return Math.min.apply(null, scores)
}
}
}
mmRecurse(board, state.playerIs, 0);
state.turn = state.playerIs;
return nextMove;
}
`
有时它工作正常,有时它没有选择正确的举动。