2016-05-30 82 views
-1

我正在嘗試使用最小最大化算法來構建一個井字遊戲。它不能正常運行(意味着它產生的動作不是最優的),我認爲這是因爲我沒有考慮到對手的動作。我只是不太清楚如何將這個結合到我的代碼中。對於上下文,我從http://neverstopbuilding.com/minimax工作。如何跟蹤當前玩家最小最大化算法

這是我的代碼。輔助方法都是自己的功能,但我沒有在這裏包括它們。

// this variable stores the optimum next move. 
var choice; 
// this stands for 'computer mark', the computer goes second and plays as 'x' 
var cmark = 'X'; 
// mark of human player. Currently not integrated into the algorithm. 
var pmark = 'O' 
// 'game' is an array which starts as [0,1,2,3,4,5,6,7,8], each number corresponding 
//to a space on the tic tac toe board. 
function minimax(game){ 
    // this is the last state of the recursion, it checks if the game has ended 
    // score() returns +10 if the computer wins, -10 for loss, 0 for tie 
    if (gameOver(game)){ 
     return score(game);  
    } 
    // this array stores all the possible scores so we can later search for the highest. 
    var scores = []; 
    //this array stores the moves that correspond to the scores array 
    var moves = []; 
    // loops through every open move. 
    //HOW DO I MAKE IT SO IT ALTERNATES BETWEEN HUMAN AND COMPUTER MOVES 
    for (var i = 0; i<game.length; i++){ 
     if (open(game[i])){ 
     //game[i] is a number corresponding to a space on the board. 
     moves.push(game[i]); 
     //create a new variable representing the game state if this move is chosen 
     var possibleGame = game; 
     possibleGame[i] = cmark; 
     //run minimax on this game state,thus obtaining scores for all possible outcomes. 
     scores.push(minimax(possibleGame)); 
     } 
    } 
//this is another place where I need to add something for opposite player? 
//find the maximum score from the scores list. this will be returned. 
var maxScore = Math.max(...scores); 
var maxScoreIndex = scores.indexOf(maxScore); 
//find the move with the same index as the maximum score. this will be stored as 'choice' 
choice = moves[maxScoreIndex]; 
return maxScore; 
} 
+0

你覺得'var possibleGame = game; possibleGame [i] = cmark;'是嗎?這將更新'遊戲'太... – nnnnnn

+0

**它沒有正常運行,但是超級模糊。你幾乎沒有解釋函數*應該做什麼*並且你根本沒有提及*它如何做不到*。看看[我如何問一個好問題?](http://stackoverflow.com/help/how-to-ask) – DelightedD0D

+0

感謝您的反饋。我試圖澄清。 – Galen

回答

0

只要跟蹤當前用戶,for循環結束後,您選擇一個移動。所以就在minmax函數結束之前返回一個選項之前,你需要改變當前用戶(你創建一個全局變量,或者至少在minmax函數的範圍之外)。

重要的是你找到最小值而不是你爲球員找到的最大價值,這個原則來源於你的對手是一個完美的球員,這意味着他總是會選擇最適合他的球員。

所以總結一下:創建一個保持當前球員的'全局'變量,當主場球員轉向時,返回最高分的移動,當它是對手的回合時,返回最小分數的移動