我正在编写一个程序来玩跳棋,每一步都是定时的。我正在使用 alpha-beta 修剪来找到最佳移动。我只能深入决策树,因为:
- 决策树对于跳棋者来说是巨大的
- 有时间限制我必须决定搬家
有没有办法在我用完时间时立即从调用堆栈中逃脱,即使堆栈上有很多帧?我想过抛出一个异常,但这似乎是一个糟糕的决定。
这就是我现在正在做的,但它不够快
public Board play(Board board) {
ArrayList<Board> actions = board.findPossibleMoves();
if (actions.size() == 0) {
return new Board();
}
int depth = 3;
// Each move has 1 second (1000ms)
TimeLimit tl = new TimeLimit(1000);
double alpha = Double.NEGATIVE_INFINITY;
double beta = Double.POSITIVE_INFINITY;
Board bestSoFar = actions.get(0);
double v = Double.NEGATIVE_INFINITY;
for (Board b : actions) {
if (tl.isTimeUp())
return bestSoFar;
double result = minValue(b, depth, alpha, beta, tl);
if (result >= v) {
bestSoFar = b;
v = result;
}
if (v >= beta) return b;
alpha = Math.max(alpha, v);
}
return bestSoFar;
}
private double maxValue(Board board, int depth, double alpha, double beta, TimeLimit tl) {
if (tl.isTimeUp())
return score(board);
...
}
private double minValue(Board board, int depth, double alpha, double beta, TimeLimit tl) {
if (tl.isTimeUp())
return score(board);
...
}