我正在参加一个人工智能课程,我们需要创建一个玩Tic-Tac-Toe的AI。
教师指定在进行下一步动作时使用alpha-beta修剪来完成AI的决策过程。我此时遇到的问题是AI创建决策树并进行移动所需的时间。普通3x3很好,3x4和4x3需要一点时间,但4x4需要多分钟才能完成第一步,而且我还没有得到比这更大的游戏板的结果。
我使用的源代码:
/** Get next best move for computer. Return int[2] of {row, >col} */
@Override
int[] move() {
int[] result = minimax(2, mySeed, Integer.MIN_VALUE, >Integer.MAX_VALUE);
// depth, max-turn, alpha, beta
return new int[] {result[1], result[2]}; // row, col
}
/** Minimax (recursive) at level of depth for maximizing or >minimizing player
with alpha-beta cut-off. Return int[3] of {score, row, col} >*/
private int[] minimax(int depth, Seed player, int alpha, int >beta) {
// Generate possible next moves in a list of int[2] of {row, >col}.
List<int[]> nextMoves = generateMoves();
// mySeed is maximizing; while oppSeed is minimizing
int score;
int bestRow = -1;
int bestCol = -1;
if (nextMoves.isEmpty() || depth == 0) {
// Gameover or depth reached, evaluate score
score = evaluate();
return new int[] {score, bestRow, bestCol};
} else {
for (int[] move : nextMoves) {
// try this move for the current "player"
cells[move[0]][move[1]].content = player;
if (player == mySeed) { // mySeed (computer) is >maximizing player
score = minimax(depth - 1, oppSeed, alpha, beta)[0];
if (score > alpha) {
alpha = score;
bestRow = move[0];
bestCol = move[1];
}
} else { // oppSeed is minimizing player
score = minimax(depth - 1, mySeed, alpha, beta)[0];
if (score < beta) {
beta = score;
bestRow = move[0];
bestCol = move[1];
}
}
// undo move
cells[move[0]][move[1]].content = Seed.EMPTY;
// cut-off
if (alpha >= beta) break;
}
return new int[] {(player == mySeed) ? alpha : beta, >bestRow, bestCol};
}
}
如果需要,请
教师还建议使用迭代加深搜索,但我是一个不知道如何的假人。