代码片段用于计算tictactoe游戏中某个位置的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;
}
答案 0 :(得分:1)
你的程序从WINNING_POSITION
(我猜对手的胜利)到minRating
开始,然后循环移动,试图找到最大伤害的移动,最小化{{1} }。
当minRating
返回EvaluatePosition
时,此举会导致您的对手在每种情况下失败,因此,搜索可以终止并且此移动被视为最佳移动。
如果没有明显的LOSING_POSITION
,那么您的算法会根据静态评估选择“最佳”移动。