我正在尝试找出用计算机玩 Gomoku(5 x 5 版本的 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 x 3 矩阵,它工作得很好。然而,对于 4 x 4,留下下一块石头需要很长时间。由于耗时长的原因是前三四个决定,我认为只让计算机只在人类最后一个选择(点)周围搜索最佳点将是一个解决方案。在前三四个决定之后,上面的正式算法将适用于剩下的几个点。你怎么看?并给出一些修改当前算法的建议。