最小最大值算法:为什么要使评级为负
Minimax Algorithm: Why make rating negative?
/* 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(
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用指针从C++中的数组中获取最大值
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 整数溢出,最大值为 pow(10,19)
- 以C++递归方式查找向量中的最大值
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 解释为什么 C++ 和 C# 对双精度类型具有不同的最大值?
- 为什么从矩阵中获取最大值的函数建议 ->无法将 'int (*)[C]' 转换为 'int (*)[0]
- 为什么我们不能从浮点最大值中减去?
- 为什么长长和长长的最大值产生相同的输出
- 为什么浮点数和 int 具有如此不同的最大值,即使它们是相同的位数?
- 为什么DWORDLONG mem = 2048 * 1024 * 1024;获取最大值
- 为什么我的c++算法总是为一个数字的最小值和最大值返回相同的值
- 最小最大值算法:为什么要使评级为负
- 为什么QT进度条的最大值限制在99%
- C/C++ 中整数类型的最大值。为什么在这个例子中我不能正确计算它?