我已经用AI实现了井字游戏,但现在我面临一个问题,如何评估井字游戏的棋盘?也许一开始我应该描述它应该如何工作:
我想说我不希望任何人为我编写代码,只是帮我找到算法或其他东西:)
感谢大家的帮助!
编辑#1
好的,我有一个minimax可以玩棋盘,但如何评估许多棋盘并选择最佳的呢?也许我没有清楚地表达我的意思,所以我会展示它。
e = 空
- 我们有n个井字游戏棋盘(具有不同的变体)
- 我们的AI应该评估哪个棋盘是最好移动的/对手最差的
- AI通过minimax算法计算移动(完成)
我想说我不希望任何人为我编写代码,只是帮我找到算法或其他东西:)
感谢大家的帮助!
编辑#1
好的,我有一个minimax可以玩棋盘,但如何评估许多棋盘并选择最佳的呢?也许我没有清楚地表达我的意思,所以我会展示它。
e = 空
* x | e | e e | o | e
* ---+---+--- ---+---+---
* x | e | e e | o | e
* ---+---+--- ---+---+---
* o | e | e x | x | e
现在,我的极小化极大算法实现只是告诉我应该把标记(比如o)放在哪里,但我需要告诉它放在哪个棋盘上,那么如何使用它来评估整个棋盘以选择放在哪里?
极小化极大算法代码:
minimax : function(tempBoard,depth){
if (CheckForWinner(tempBoard) !== 0)
return score(tempBoard, depth);
depth+=1;
var scores = new Array();
var moves = new Array();
var availableMoves = Game.emptyCells(tempBoard);
var move, possibleGame, maxScore, maxScoreIndex, minScore,minScoreIndex;
for(var i=0; i < availableMoves.length; i++) {
move = availableMoves[i];
possibleGame = Game.getNewBoard(move,tempBoard);
scores.push(Ai.minimax(possibleGame, depth));
moves.push(move);
tempBoard = Game.undoMove(tempBoard, move);
}
if (Game.turn === "ai") {
maxScore = Math.max.apply(Math, scores);
maxScoreIndex = scores.indexOf(maxScore);
choice = moves[maxScoreIndex];
return scores[maxScoreIndex];
} else {
minScore = Math.min.apply(Math, scores);
minScoreIndex = scores.indexOf(minScore);
choice = moves[minScoreIndex];
return scores[minScoreIndex];
}
}
return scores[minScoreIndex]
和return scores[maxScoreIndex]
,但并没有返回玩家应该下棋的位置以达到这个得分。 - Omar Sharaki