0

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;
}
motiur
  • 1,640
  • 9
  • 33
  • 61

1 Answers1

1

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.

lenik
  • 23,228
  • 4
  • 34
  • 43
  • Just to reiterate and to make something clear , the minimum value obtained in the snippet int curRating = EvaluatePosition(state, depth + 1); benefits whom , current player , or the opponent . I can assume it is from the context of the current player , else he would not want his opponent to lose . Only when the evaluation function gives the value of LOSING_POSITION is when the loop stops . Am I right , it would be helpful if you want to add some . – motiur Nov 26 '12 at 10:20
  • the loop stops when out of possible moves. or when it's obvious, the opponent has no chance to win whatever move he chooses. `curRating` is the lower the better. same for `minRating`. however, just before `Evaluate()` returns, the `rating` has changed the sign, because the opponent should move his rating in the opposite direction, hence "NegaMax". – lenik Nov 26 '12 at 11:00