我一直在使用 negamax 玩连接四。我注意到的是,如果我添加 alpha-beta,它有时会给出“错误”的结果,因为在做出失败的举动时,我认为它不应该以我正在搜索的深度进行。如果我删除 alpha-beta 它会按预期播放。alpha-beta 能否切断一些实际可行的分支(尤其是在深度有限的情况下)?这是代码以防万一:
int negamax(const GameState& state, int depth, int alpha, int beta, int color)
{
//depth end reached? or we actually hit a win/lose condition?
if (depth == 0 || state.points != 0)
{
return color*state.points;
}
//get successors and optimize the ordering/trim maybe too
std::vector<GameState> childStates;
state.generate_successors(childStates);
state.order_successors(childStates);
//no possible moves - then it's a terminal state
if (childStates.empty())
{
return color*state.points;
}
int bestValue = -extremePoints;
int v;
for (GameState& child : childStates)
{
v = -negamax(child, depth - 1, -beta, -alpha, -color);
bestValue = std::max(bestValue, v);
alpha = std::max(alpha, v);
if (alpha >= beta)
break;
}
return bestValue;
}