我已经用人工智能实现了井字棋,但现在面临一个问题,如何评价井字棋游戏的棋盘?
也许一开始我会描述它应该如何工作:
- 我们有 n 个井字棋游戏板(有不同的变体)
- 我们的人工智能应该评估哪个棋盘最适合继续前进/最适合对手
- Ai 通过极小极大算法计算移动(完成)
问题在于2。有没有办法对一块板进行“评级”?
我想说我不想让任何人给我写代码,只是为了帮助我找到算法或其他东西:)
感谢各位的帮助!
Edit #1
好吧,我有一个极小极大玩棋盘,但如何评价许多棋盘并选择哪个是最好的。也许我没有清楚地说出我想要什么,所以我会展示它。
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];
}
}