我正在尝试使用minimax算法构建井字游戏。它还没有正常运行(这意味着它正在生成不是最佳的动作),我认为这是因为我没有考虑对面玩家的动作。我只是不太确定如何将其合并到我的代码中。就上下文而言,我正在从 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;
}
只需跟踪当前用户,在 for 循环结束后,您可以选择一个移动。因此,在 minmax 函数末尾返回选项之前,您更改当前用户(您创建一个全局变量,或者至少在 minmax 函数的范围之外。
重要的是,您要找到对手移动的最小值,而不是您为玩家正确找到的最大值。这个原则源于你的对手是一个完美的球员的想法,这意味着他总是会选择最适合他的动作。
所以总结一下:创建一个"全局"变量来保留当前玩家。当轮到主队球员时,返回最高分的移动。当轮到对手时,返回最低分数的移动。