Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript minimax算法tic tac toe,并不总是给出最佳的移动_Javascript_Algorithm_Tic Tac Toe_Minimax - Fatal编程技术网

javascript minimax算法tic tac toe,并不总是给出最佳的移动

javascript minimax算法tic tac toe,并不总是给出最佳的移动,javascript,algorithm,tic-tac-toe,minimax,Javascript,Algorithm,Tic Tac Toe,Minimax,我一直在尝试在我的井字游戏中使用minimax算法,以使我的AI无敌。然而,它并不总是返回最佳移动 AI = X; human = "O" var board = ["x", "x", "-", "-", "o", "o", "-", "-", "-"] 它给出了指数[2]以上的板作为最佳移动,这是正确的 然而,在下面的棋盘上,它给出的答案是索引[3],这将允许人类玩家在其回合中获胜 var boardB = ["x","x","

我一直在尝试在我的井字游戏中使用minimax算法,以使我的AI无敌。然而,它并不总是返回最佳移动

AI = X; human = "O"

var board =  ["x", "x", "-",
               "-", "o", "o",
               "-", "-", "-"]
它给出了指数[2]以上的板作为最佳移动,这是正确的

然而,在下面的棋盘上,它给出的答案是索引[3],这将允许人类玩家在其回合中获胜

var boardB = ["x","x","o",
             "-","o","-",
             "-","_","-"];

var player = 'x';
var opponent = 'o';

function isMovesLeft(board){
    for (var i = 0; i<board.length; i++){
        if (board[i] =='-'){
            return 'true';
        }
        else{
            return 'false';
        }
    }
}



function evaluate(){
    for (var i = 0; i < board.length; i += 3) {
        if (board[i] === board[i + 1] && board[i + 1] === board[i + 2]) {
            if (board[i] == player){
                return +10;  
            }
            else if(board[i]== opponent){
                return -10;
            } 
        }
    }
    for (var j = 0; j < board.length; j++) {
        if (board[j] === board[j + 3] && board[j + 3] === board[j + 6]) {
            if (board[j] == player){
                return +10;  
            }
            else if(board[j] == opponent){
                return -10;
            } 
        }
    }


  if ((board[4]==board[0] && board[4]==board[8]) || (board[4]==board[2] && board[4]==board[6])) {
    if (board[4]==player){
        return +10;
    }
    else if (board[4]==opponent){
        return -10;
    }
 }

return 0;
}



function minimax(board, depth, isMax){
    var score = evaluate(board);

    if (score == 10){
        return score;
    }

    if (score == -10){
        return score;
    }

    if (isMovesLeft(board) =="false"){
        return 0;
    }

    if (isMax == "true"){
        var best = -1000;

        for (var i = 0; i< board.length; i++){
            if (board[i]=='-'){
                board.splice(i, 1, player);
                var value = minimax(board, depth+1, "false");
                best = Math.max(best, value);

                board.splice(i, 1, "-");
            }
         }
         return best;  
     }

    else if (isMax == 'false'){
        var best = 1000;
        for (var i = 0; i<board.length; i++){
            if (board[i]=='-'){
             board.splice(i, 1, opponent);
             var value = minimax(board, depth+1, "true");
             best = Math.min(best, value);

             board.splice(i, 1, "-");
            }
         }
        return best;
      }
}

function findBestMove(board){
    var bestVal = -1000;
    var bestMove= -1;

    for (var i = 0; i<board.length; i++){
        if (board[i]=='-'){
            board.splice(i, 1, player);
            var moveVal = minimax(board, 0, "false");

            board.splice(i, 1, "-");

            if (moveVal > bestVal)
            {
                bestMove = i;
                bestVal = moveVal;
            }
        }
    }
    alert("bestVal is : " + bestVal + "<br> best Move is : " + bestMove;)
}
var boardB=[“x”、“x”、“o”,
“-”、“o”、“-”,
"-","_","-"];
var player='x';
var对手='o';
功能isMovesLeft(板){

对于(var i=0;i如果您看到每个
i
moveVal
,您将看到它是零。
这是因为在minimax函数中调用
isMovesLeft
,当false时返回0。
程序的问题是
isMovesLeft
总是返回
false

您应该将其更改为:

function isMovesLeft(board){
    for (var i = 0; i<board.length; i++){
        if (board[i] =='-'){
            return 'true';
        }
    }
return false;
}  
功能isMovesLeft(板){

对于(var i=0;iThanks@Mahesh!它对上面的棋盘有效。但是,如果我再次将棋盘更改为不同的顺序,AI将阻止人类玩家的移动以阻止人类玩家获胜。下面的棋盘答案是索引[3]。
var board=[“x”、“o”、“x”,“-”,“o”,“-”,“-”,“”,“-”];
我认为minimax函数只给出了数字最少的索引,在这种情况下[3],当它遇到一个0的最佳分数时。@Marci Banez我试过那块板,它可能对你不起作用,因为在索引7中,你用了“u”(下划线)而不是“-”(连字符)。如果你将
isMovesLeft
函数更改为我在回答中提到的,那么你应该得到
bestMove
as 7,它有效地阻止了
0
获胜。你是对的!!非常感谢!我在这段代码中错过了很多东西。现在一切都很好。但是,你之前提到过,我可以返回
深度和分数一起。我想知道我如何实现这一点,我也不太确定它将如何提高AI的效率。