【问题标题】:Which algorithms are available to solve Tic Tac Toe?哪些算法可用于解决井字游戏?
【发布时间】:2012-05-22 15:22:23
【问题描述】:
【问题讨论】:
标签:
tic-tac-toe
alpha-beta-pruning
【解决方案1】:
尝试在一定深度进行截断...我认为您不需要超过 4 或 5 的深度才能做出完美的移动。
(java for 3*3 one-dim board with depth):
int minimax(int turn, int depth) { // turn(side to move) is 1 or -1
int val = -turn; // worst value for that turn
int result = NULVAL;
if (wins()) return turn; // evaluate board (board is a field)
if (depth == 0) return DRAW;
for (int i = 0; i < 9; i++) {
if (board[i] == EMPTY) {
board[i] = turn; // make move
result = minimax(-turn, depth - 1);
board[i] = EMPTY; // delete move
if (result == turn)
return turn; // sees win
if (result == DRAW)
val = result;
}
}
// if result keeps NULVAL: couldn't make a move (board is full)
if (result == NULVAL) return DRAW;
return val;
}