2016-05-30 8 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

**まだ正しく機能していません**超曖昧です。あなたは関数*が何をすべきかをほとんど説明していません。 [良い質問をするにはどうすればいいですか]を見てください。 – DelightedD0D

+0

フィードバックをいただきありがとうございます。私は明確にしようとした。 – Galen

答えて

0

for-loopの終了後に現在のユーザーを追跡するだけで、移動を選択します。したがって、minmax関数の最後に選択肢を返す前に、グローバル変数を作成するか、少なくともminmax関数のスコープの外にある現在のユーザを変更してください。

最小値この原理は、対戦相手が完璧なプレイヤーであるという考えから導き出されます。つまり、彼が常に最善の動きを選ぶことを意味します。

結論として:現在のプレイヤーを維持する「グローバル」変数を作成するホームプレイヤーのターンには最大得点の移動を返し、相手のターンでは最小スコアの移動を返します。

関連する問題