Question

The snippet of code is constructed for calculating the bestMove of a position in a tictactoe game . I got almost every part of the code , except the condition in the for loop , which says that minRating != LOSING_POSITION . This code comes from a implementation of the given pseudocode .

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.*
}

I could not match the second condition of the for loop with the given code which says that until you find a forced win. I could not find the similarity between this fact and that 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;
}
Was it helpful?

Solution

Your program starts from assigning WINNING_POSITION (win for your opponent, I suppose) to minRating and then looping through the moves, trying to find the move with the maximum damage, minimizing minRating.

When EvaluatePosition returns LOSING_POSITION than means this move leads to your opponent losing in every case, thus, search can be terminated and this move is considered the best move.

If there are no obvious LOSING_POSITIONS, then your algorithm selects "best" move according to the static evaluation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top