最小最大值算法:为什么要使评级为负

Minimax Algorithm: Why make rating negative?

本文关键字:为什么 最大值 算法      更新时间:2023-10-16
/* finds the best move for the current player given the state of the game.
 * depth parameter and MAX_DEPTH are used to limit the depth of the search for games
 * that are too difficult to analyze in full detail (like chess)
 * returns best move by storing an int in variable that rating points to.
 * we want to make the move that will result in the lowest best move for the position after us(our opponent)
 */
moveT findBestMove(stateT state, int depth, int &rating) {
    Vector<moveT> moveList;
    generateMoveList(state, moveList);
    int nMoves = moveList.size();
    if (nMoves == 0) cout << "no move??" << endl;
    moveT bestMove;
    int minRating = WINNING_POSITION + 1; //guarantees that this will be updated in for loop
    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;
}
/* evaluates the position by finding the rating of the best move in that position, limited by MAX_DEPTH */
int evaluatePosition(stateT state, int depth) {
    int rating;
    if (gameIsOver(state) || depth >= MAX_DEPTH) {
        return evaluateStaticPosition(state);
    }
    findBestMove(state, depth, rating);
    return rating;
}

这是我实现最小最大值算法的代码,以与计算机玩完美的井字游戏。代码有效,此处未显示许多其他帮助程序函数。我了解算法的本质,但是我很难完全理解findBestMove((函数末尾的行:

rating = -minRating;

这就是我的书中所说的:包括负号是因为视角发生了变化:位置是从对手的角度评估的,而评级是从你自己的角度表达移动的价值。让对手处于负面位置的举动对您有好处,因此具有正值。但是当我们最初调用该函数时,它是从计算机的角度。我想当我们评估每个位置时,这个函数是从对手的角度调用的,这就是为什么?有人可以让我更深入地了解正在递归发生的事情以及为什么最后的评级需要为负数。一如既往,非常感谢您的时间。

假设两个位置,A 和 B,其中 A 对玩家 A 更好,B 对玩家 B 更好。 当玩家 A 评估这些位置时,eval(A(> eval(B(,但是当玩家 b 评估时,我们希望 eval(A(