如何跟踪当前玩家minimax算法

时间:2016-05-30 02:08:17

标签: javascript jquery algorithm minimax

我正在尝试使用minimax算法构建一个tic-tac-toe游戏。它还没有正常运作(意味着它产生了不是最佳的动作),我认为这是因为我没有考虑到对手的动作。我只是不太确定如何将其合并到我的代码中。对于上下文,我的工作是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; 
}

1 个答案:

答案 0 :(得分:0)

只需跟踪当前用户,在for循环结束后,您选择一个移动。因此,在minmax函数结束时返回选项之前,您将更改当前用户(您创建一个全局变量,或者至少在minmax函数的范围之外。

重要的是你找到对手所做的移动的最小值,而不是你为玩家正确找到的最大值。这个原则源于你的对手是一个完美的球员,这意味着他总是会选择最适合他的球员。

总而言之:创建一个全球性的&#39;变量保持当前的球员。当家庭播放器转动时,返回最高分的移动。如果是对手的回合,则返回最低得分的移动。