/* 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;
}
这是我的代码实现了一个minimax算法玩一个完美的游戏的tic-tac-toe对计算机代码可以工作,这里没有显示许多其他的helper函数我了解算法的本质,但是我很难完全将头绕在findbestmove()函数末尾的行上:
rating = -minRating;
这就是我的书中所说的:消极的迹象包括在内,因为观点发生了变化:立场是从你对手的角度来评估的,而评级则是从你自己的角度来表达移动的价值。让对手处于不利位置的动作对你有好处,因此有积极的价值。
但当我们最初调用这个函数时,它是从计算机的角度出发的我想当我们评估每个位置时,这个函数是从对手的角度调用的,这就是为什么有没有人能让我更深入地了解递归发生的事情,以及为什么最终评级需要为负。
一如既往地非常感谢你抽出时间。
最佳答案
想象两个位置,A和B,其中A对玩家A更好,B对玩家B更好。当玩家A评估这些位置时,eval(A)>eval(B),但当玩家B评估这些位置时,我们需要eval(A)
关于c++ - Minimax算法:为什么将评级设为负面?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33601177/