62,614
社区成员
发帖
与我相关
我的任务
分享
// 极小值极大值搜索函数
static int MinMaxSearch(int depth, int alpha, int beta) {
int i, genCount, value, bestValue;
int[] allMoves = new int[MAX_GEN_MOVES];
int eatCount;
int[] eatTable = new int[2]; // 还原局面时用
// 如果搜索到指定深度,则返回局面评估值
if (depth == 0) {
return evaluatePosition();
}
// 初始化最佳值
bestValue = -INFINITY_VALUE; // 负无穷
genCount = generateAllMoves(allMoves);
for (i = 0; i < genCount; i++) {
eatCount = makeOneMove(allMoves[i], eatTable); // 走棋
theDepth++;
value = -MinMaxSearch(depth - 1, -beta, -alpha); // 递归
undoOneMove(allMoves[i], eatCount - 1, eatTable); // 还原
theDepth--;
if (value >= beta) {
return value;
}
if (value > bestValue) {
bestValue = value;
if (depth == search_depth) { // 如果是根节点 保存最佳走法
bestMove = allMoves[i];
}
if (value > alpha) {
alpha = value;
}
}
}
// 如果是杀棋,就根据距杀棋的步数给出评价
if (currentPlayer == BLACK) { // 如果是黑方
if (bestValue == INFINITY_VALUE) {
return INFINITY_VALUE - theDepth;
}
} else { // 是白方
if (bestValue == -INFINITY_VALUE) {
return theDepth - INFINITY_VALUE;
}
}
return bestValue; // 返回找到的最佳局面评分
}