我正在试图找出用计算机玩Gomoku(5 x版本的tictactoe)的算法。 在这种情况下,我发现最常用的算法是Min-max(或Alpha-beta),但这些对我来说太难处理了。所以我决定使用以下代码,这些代码很容易理解但很耗时。 它显示了计算机如何做出合理的选择。
//------------------------------------------------------------
// computer_move() checks for all legal moves in the current |
// position. then for each of them it calls the dfs_search() |
// function to get the moves' score. And finally it returns |
// the move with the best score. |
//------------------------------------------------------------
int computer_move() //
{
int best_move; // best move so far
int best_score = -100; // best score so far
int score; // current score
int i;
for (i = 0; i < 16; ++i) { //
if (pos[i] == EMPTY) { // if a legal move can be made
pos[i] = COMPUTER; // mark the move
score = -dfs_search(HUMAN); //
pos[i] = EMPTY; // take back the move
if (score > best_score) {
best_score = score;
best_move = i;
}
}
}
printf("Computer's move: %d\n", best_move);
return best_move; // return the best move found
}
//------------------------------------------------------------
// dfs_search() gets the side to move, find all legal moves |
// for him, and for each move, it recursively calls itself. |
// It returns a score for the position. |
// This recursive function continues on each variation until |
// the game's end is found in that variation. Which means |
// that the variation continues until check_result() returns |
// a value other than CONTINUE. |
// Note that this can be done in tic-tac-toe, since it's a |
// deterministic game. For games like chess or checkers we |
// can't continue the variation until reaching the game's end |
// so we have to cut the variation at some point. |
//------------------------------------------------------------
int dfs_search(int player) //
{
int best_score = -100;
int score;
int result;
int i;
result = check_result(player);
if (result != CONTINUE) return result; // return the result
for (i = 0; i < 16; ++i) {
if (pos[i] == EMPTY) {
pos[i] = player;
score = -dfs_search(CHANGE_PLAYER(player)); //
pos[i] = EMPTY;
if (score > best_score)
best_score = score;
}
}
return best_score; // return the best score
}
对于3乘3矩阵,它运行得很好。然而,对于4乘4,离开下一块石头需要很长时间。由于长时间消耗的原因是前三个或四个决定,我认为只是让计算机只搜索人类最后选择(点)的最佳点将是一个解决方案。 在前三个或四个决策之后,上述正式算法将适用于剩下的几个点。你觉得怎么样?并提出修改当前算法的建议。
答案 0 :(得分:0)
您正在尝试解决整个游戏树问题。在3x3板上有9个!树中有362880个节点,这个节点足够小,可供计算机处理,但在4x4板上有16个节点! = 20922789888000(20.9万亿)个节点,在合理的时间内访问太多。
考虑实施一种搜索算法,该算法可以在不解决整个游戏树的情况下返回当前位置得分的合理估计值。对于GoMoku,我建议Monte Carlo Tree Search。它已成功应用于许多游戏,例如Go,并且以其香草形式,它不需要您根据固定深度最小 - 最大搜索及其变体(如alpha-beta)编写static evaluation function。< / p>