作爲一個練習項目,我在JSFiddle上製作了Tic-Tac-Toe遊戲(因爲已經不夠了,對不對?),然後我開始添加無與倫比的AI。在大多數情況下,它可以工作,但是有一些組合(例如,將X設置爲5,9,3或5,7或9),導致計算機無法正確計算最佳移動。Javascript中的Minimax工作不正常
上的jsfiddle項目:https://jsfiddle.net/jd8x0vjz/
及相關函數開始行63:
function evaluateMove(move, player, depth) {
var gameStatus = evaluateGameStatus(move); //get status of current board
if (gameStatus < 2 && player)
return -1; //if human won, return -1
if (gameStatus < 2 && !player)
return 1; //if human lost, return 1
var returnValue = 0 //value to be returned later
for (var z = 0; z < 3; z++) { //loop for row
for (var s = 0; s < 3; s++) { //loop for column
if (move[z][s]) //if current slot has an x or o,
continue; //skip it
var nextMove = cloneGameStatus(move); //create temporary array with base of current grid
nextMove[z][s] = !player ? "x" : "o"; //assign first free field the appropriate symbol
var value = evaluateMove(nextMove, !player, depth+1); //recursion but with switched player, to add the correct icon afterwards
if ((value > returnValue) && player)
returnValue = value;
if ((value < returnValue) && !player)
returnValue = value;
}
}
return returnValue; //return value of current simulation
}
我覺得最後兩個if子句是導致這些問題,因爲電腦無法計算正確的值(在調試器中可觀察到),但它們有時會被覆蓋,但我不確定這是否真的是問題的根源。任何幫助或提示,將不勝感激!
編輯:問題解決了!如果不是第一個,請在下面尋找我的答案。