该代码段用于计算象棋游戏中一个位置的bestMove。除了for循环中的条件(表示minRating!= LOSING_POSITION)外,我几乎了解了代码的所有部分。此代码来自给定伪代码的实现。
moveT FindBestMove(stateT state, int depth, int & rating) {
for (*each possible move or until you find a forced win*) {
*Make the move.
Evaluate the resulting position, adding one to the depth indicator.
Keep track of the minimum rating so far, along with the corresponding move.
Retract the move to restore the original state.*
}
*Store the move rating into the reference parameter.
Return the best move.*
}
我无法将for循环的第二个条件与给定的代码匹配,该代码说直到找到强制胜利。我找不到此事实与minRating!= LOSING_POSITION之间的相似之处
moveT FindBestMove(stateT state, int depth, int & rating) {
Vector<moveT> moveList;
GenerateMoveList(state, moveList);
int nMoves = moveList.size();
if (nMoves == 0) Error("No moves available");
moveT bestMove;
int minRating = WINNING_POSITION + 1;
for (int i = 0; i < nMoves && minRating != LOSING_POSITION; i++) {
moveT move = moveList[i];
MakeMove(state, move);
int curRating = EvaluatePosition(state, depth + 1);
if (curRating < minRating) {
bestMove = move;
minRating = curRating;
}
RetractMove(state, move);
}
rating = -minRating;
return bestMove;
}
int EvaluatePosition(stateT state, int depth) {
int rating;
if (GameIsOver(state) || depth >= MAX_DEPTH) {
return EvaluateStaticPosition(state);
}
FindBestMove(state, depth, rating);
return rating;
}
最佳答案
您的程序开始于将WINNING_POSITION
(我想是为您的对手获胜)分配给minRating
,然后循环遍历这些动作,试图找到损害最大的动作,从而将minRating
最小化。
当EvaluatePosition
返回LOSING_POSITION
时,表示此举将导致您的对手在每种情况下均输,因此可以终止搜索并将此举视为最佳举动。
如果没有明显的LOSING_POSITIONS
,则您的算法会根据静态评估选择“最佳”举动。
关于c++ - 了解Negamax的约束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13559698/