井字棋牌算法
Tic-tac-toe rate a board algorithm
我已经用 ai 实现了一个井字游戏,但现在我面临一个问题,如何评价一盘井字游戏?
也许在开始时我会描述它应该如何工作:
- 我们有 n 个井字游戏板(有不同的变体)
- 我们的 ai 应该评价哪个棋盘移动得最好 on/the 对对手来说最差
- Ai 通过极小极大算法计算着法(完成)
问题出在2。有什么办法可以"rate"一块板子吗?
我想说我不想让任何人给我写代码,只是为了帮我找算法什么的:)
感谢大家的帮助!
编辑#1
好的,我有一个 minimax 来玩一个棋盘,但如何评价许多棋盘并选择最好的棋盘。也许我没有说清楚我想要什么,所以我会展示它。
e = 空
* x | e | e e | o | e
* ---+---+--- ---+---+---
* x | e | e e | o | e
* ---+---+--- ---+---+---
* o | e | e x | x | e
现在,我的 minimax 算法的实现只是告诉我应该把我的标志放在哪里(假设是 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];
}
}
可以使用 Minimax 算法对井字棋的动作进行评分。该算法旨在执行评估的移动,切换到对手的角度(反过来也尝试做各自的最佳移动)并递归评估移动直到其中一名玩家获胜(这意味着已到达游戏树)。
虽然这个算法的基本版本的实现并不是非常困难,但是理解这个方法是至关重要的;请注意,'artificial intelligence' 基本上包括假设用所有可能的动作玩整个游戏 - 它不是启发式算法,而是精确算法。可以使用 Alpha-Beta-Pruning 来优化它,以节省评估不需要的游戏树子树。
这里有一个公式可以用来给看板评分:
value = (10 * x3 + 3 * x2 + x1) - (10 * o3 + 3 * o2 + o1)
其中:
- xN => 上面有
N
x's
而没有 o's
的 rows/columns/diagonals 的数量
- oN => 上面有
N
o's
而没有 x's
的 rows/columns/diagonals 的数量
这假设 max-player
是 X
。如果不是,您可以更改标志。
我已经用 ai 实现了一个井字游戏,但现在我面临一个问题,如何评价一盘井字游戏?
也许在开始时我会描述它应该如何工作:
- 我们有 n 个井字游戏板(有不同的变体)
- 我们的 ai 应该评价哪个棋盘移动得最好 on/the 对对手来说最差
- Ai 通过极小极大算法计算着法(完成)
问题出在2。有什么办法可以"rate"一块板子吗?
我想说我不想让任何人给我写代码,只是为了帮我找算法什么的:)
感谢大家的帮助!
编辑#1
好的,我有一个 minimax 来玩一个棋盘,但如何评价许多棋盘并选择最好的棋盘。也许我没有说清楚我想要什么,所以我会展示它。
e = 空
* x | e | e e | o | e
* ---+---+--- ---+---+---
* x | e | e e | o | e
* ---+---+--- ---+---+---
* o | e | e x | x | e
现在,我的 minimax 算法的实现只是告诉我应该把我的标志放在哪里(假设是 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];
}
}
可以使用 Minimax 算法对井字棋的动作进行评分。该算法旨在执行评估的移动,切换到对手的角度(反过来也尝试做各自的最佳移动)并递归评估移动直到其中一名玩家获胜(这意味着已到达游戏树)。
虽然这个算法的基本版本的实现并不是非常困难,但是理解这个方法是至关重要的;请注意,'artificial intelligence' 基本上包括假设用所有可能的动作玩整个游戏 - 它不是启发式算法,而是精确算法。可以使用 Alpha-Beta-Pruning 来优化它,以节省评估不需要的游戏树子树。
这里有一个公式可以用来给看板评分:
value = (10 * x3 + 3 * x2 + x1) - (10 * o3 + 3 * o2 + o1)
其中:
- xN => 上面有
N
x's
而没有o's
的 rows/columns/diagonals 的数量
- oN => 上面有
N
o's
而没有x's
的 rows/columns/diagonals 的数量
这假设 max-player
是 X
。如果不是,您可以更改标志。