重构 alphaBetaPruning 暂时改为在循环中判断先后手

This commit is contained in:
CalciteM 2019-06-30 12:29:22 +08:00
parent 7273f29974
commit f5573e9f06
1 changed files with 27 additions and 44 deletions

View File

@ -539,57 +539,40 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
// 排序子节点树
sortChildren(node);
// 根据演算模型执行 MiniMax 检索,对先手,搜索 Max
if (chessTemp.whosTurn() == NineChess::PLAYER1) {
minMax = -INF_VALUE;
for (auto child : node->children) {
dataStack.push(chessTemp.context);
chessTemp.command(child->move);
value = alphaBetaPruning(depth - 1, alpha, beta, child);
chessTemp.context = dataStack.top();
dataStack.pop();
// 根据演算模型执行 MiniMax 检索,对先手,搜索 Max, 对后手,搜索 Min
minMax = chessTemp.whosTurn() == NineChess::PLAYER1 ? -INF_VALUE : INF_VALUE;
for (auto child : node->children) {
dataStack.push(chessTemp.context);
chessTemp.command(child->move);
value = alphaBetaPruning(depth - 1, alpha, beta, child);
chessTemp.context = dataStack.top();
dataStack.pop();
if (chessTemp.whosTurn() == NineChess::PLAYER1) {
// 取最大值
if (value > minMax)
minMax = value;
if (value > alpha)
alpha = value;
// 剪枝返回
if (alpha >= beta)
break;
}
// 取最大值
node->value = minMax;
}
// 对后手搜索Min
else {
minMax = INF_VALUE;
for (auto child : node->children) {
dataStack.push(chessTemp.context);
chessTemp.command(child->move);
value = alphaBetaPruning(depth - 1, alpha, beta, child);
chessTemp.context = dataStack.top();
dataStack.pop();
minMax = std::max(value, minMax);
alpha = std::max(value, alpha);
} else {
// 取最小值
if (value < minMax)
minMax = value;
if (value < beta)
beta = value;
// 剪枝返回
if (alpha >= beta)
break;
minMax = std::min(value, minMax);
beta = std::min(value, beta);
}
// 取最小值
node->value = minMax;
// 剪枝返回
if (alpha >= beta)
break;
}
node->value = minMax;
#ifdef DEBUG_AB_TREE
node->alpha = alpha;
node->beta = beta;
node->minMax = minMax;
#endif
// 删除“孙子”节点,防止层数较深的时候节点树太大
#ifndef DEBUG_AB_TREE
for (auto child : node->children) {