【问题标题】:Javascript Tic Tac Toe AI - not selecting optimum spaceJavascript Tic Tac Toe AI - 未选择最佳空间
【发布时间】:2020-10-27 09:32:11
【问题描述】:

我一直在努力为井字游戏项目创建无与伦比的 AI。

我一直在谷歌上搜索并使用 The Coding Train 教程 - 但是,人工智能似乎仍在选择一个随机方块。 虽然选哪个方格后面肯定有逻辑,但我想不通是什么逻辑!

这是我的代码笔 - https://codepen.io/kath-ldn/pen/oNLZqrp,我已经复制了下面的相关代码。

如果有人可以看看这个,我将不胜感激。我已经卡了好几天了,无法弄清楚问题是什么。

//functions to assess winning combos
function equals3(a, b, c) {
    return a === b && b === c && a !== "";
}

function checkWinner(){
    let winner = null;

    if (equals3(gameBoard[0], gameBoard[1], gameBoard[2])) {
        winner = gameBoard[0];
    };
    if (equals3(gameBoard[3], gameBoard[4], gameBoard[5])) {
        winner = gameBoard[3];
    };
    if (equals3(gameBoard[6], gameBoard[7], gameBoard[8])) {
        winner = gameBoard[6];
    };
    if (equals3(gameBoard[0], gameBoard[3], gameBoard[6])) {
        winner = gameBoard[0];
    };
    if (equals3(gameBoard[1], gameBoard[4], gameBoard[7])) {
        winner = gameBoard[1];
    };
    if (equals3(gameBoard[2], gameBoard[5], gameBoard[8])) {
        winner = gameBoard[0];
    };
    if (equals3(gameBoard[0], gameBoard[4], gameBoard[8])) {
        winner = gameBoard[0];
    };
    if (equals3(gameBoard[2], gameBoard[4], gameBoard[6])) {
        winner = gameBoard[2];
    };

    let openSpots = 0;
    for (let i = 0; i < gameBoard.length; i++) {
        if (gameBoard[i] === "") {
            openSpots = openSpots + 1;
        };
    };

    if (winner === null && openSpots === 0) {
        return 'tie';
    } else {
        return winner;
    };
};

let scores = {
  X: 10,
  O: -10,
  tie: 0
};

//function to create impossible-to-beat AI using minimax algorithm
function bestMove() {
    let bestScore = -Infinity;
    let move;
    for (let i = 0; i < gameBoard.length; i++) {
      if (gameBoard[i] === "") {
        gameBoard[i] = AI;
        let score = minimax(gameBoard, 0, false);
        gameBoard[i] = "";
        if (score > bestScore) {
        bestScore = score;
        move = i;
        }
      }    
    }
    gameBoard[move] = AI;
};

//minimax function
function minimax(gameBoard, depth, isMaximizing){
  let result = checkWinner();
  if (result !== null) {
    return scores[result];
  }

  if (isMaximizing) {
    let bestScore = -Infinity;
    for (let i = 0; i < gameBoard.length; i++) {
      if (gameBoard[i] === "") {
        gameBoard[i] = AI;
        let score = minimax(gameBoard, depth + 1, false);
        gameBoard[i] = "";
        bestScore = Math.max(score, bestScore);
      }
    }
    return bestScore;
  } else {
    let bestScore = Infinity;
    for (let i = 0; i < gameBoard.length; i++) {
      if (gameBoard[i] === "") {
        gameBoard[i] = human;
        let score = minimax(gameBoard, depth + 1, true);
        gameBoard[i] = "";
        bestScore = Math.min(score, bestScore);
      }
    }
    return bestScore;
  }
};

【问题讨论】:

    标签: javascript artificial-intelligence minimax


    【解决方案1】:

    您正在遍历棋盘并为每个方格调用 minimax,这不是必需的,而且会非常慢。你只需要像这样调用它一次:

    move, score = minimax(gameBoard, 8, true)
    

    我不确定是否需要它,但我会从 checkWinner 函数返回一个 0,而不是返回一个 'tie' 字符串。似乎很难从例如选择一个最大/最小值。一个 1 和一个 'te' 否则。

    然后在您的极小极大函数中,您需要更改一些内容以实际返回它找到的最佳移动。对于任何编程语言问题,我很抱歉,我习惯了 Python。无论如何,我希望你明白我的意思:

    //minimax function
    function minimax(gameBoard, depth, isMaximizing){
      let result = checkWinner();
      if (result !== null) {
        return None, scores[result];
      }
    
      if (isMaximizing) {
        let bestScore = -Infinity;
        for (let i = 0; i < gameBoard.length; i++) {
          if (gameBoard[i] === "") {
            gameBoard[i] = AI;
            let score = minimax(gameBoard, depth - 1, false)[1];
            gameBoard[i] = "";
            if score > bestScore:
                bestScore = score
                bestMove = gameBoard[i]
          }
        }
        return bestMove, bestScore;
      } else {
        let bestScore = Infinity;
        for (let i = 0; i < gameBoard.length; i++) {
          if (gameBoard[i] === "") {
            gameBoard[i] = human;
            let score = minimax(gameBoard, depth - 1, true)[1];
            gameBoard[i] = "";
            if score < bestScore:
                bestScore = score
                bestMove = gameBoard[i]
          }
        }
        return bestMove, bestScore;
      }
    };
    

    【讨论】:

    • 非常感谢您的帮助,非常感谢!我使用了这个(并查看了另一个教程),现在有了一些有用的东西。它位于同一个 codepen 链接,以防其他人被困在同一件事上并偶然发现此页面。
    • 很高兴它成功了!如果您对解决方案感到满意,请接受答案 (stackoverflow.com/help/someone-answers) :)
    猜你喜欢
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多