我的Minimax并不总是选择最佳动作

时间:2017-02-09 18:31:41

标签: javascript minimax

我正在尝试为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;
   }

`

有时它工作正常,有时它没有选择正确的举动。

1 个答案:

答案 0 :(得分:0)

原来我的问题是,如果有人需要它,我在检查返回什么时将数字1000更改为10。 此外,我没有任何绘制游戏状态的东西。如果您想查看来源on github

相关问题