【发布时间】:2021-10-22 22:44:17
【问题描述】:
我目前正在完成一篇大学论文的作业。我正在为跳棋游戏制作 AI。我了解极小极大算法的原理,但是,在实现它时,我很难了解如何以最佳移动方式访问节点,以便实现它。可能是我错误地实现了代码,这让我陷入了这种境地。但是,据我所知,它只返回评估的最佳分数。然后如何获取将其返回给根的节点?
这是包含 minimax 函数的 GameTree 的代码。我希望能够使用在 GetNextMove() 中发送最高分的节点
#include "gametree.h"
//Constructor
GameTree::GameTree()
{
}
/* This function is part of the evalutaion of which move is best by looking for the best
* score in the tree. which is set up by the nodes calling themselves via the Move::PosMove
* function. This function was used because it was taught in class and it enables pruning.
* this function goes down the left side to the bottom of the tree first. Because the
* children of each node are stored in a set the highest score is the first to be seen.
* this will enable maximum pruning, so will speed up the search for the best move.
* Called by Move::AiMove() */
int GameTree::minimax(Node *node, int depth, int alpha, int beta, bool isMaxNode)
{
int Alpha = alpha;
int Beta = beta;
if (depth == 0|| endGame ==1)
{
nScore= node->score;
return nScore;
}
if (isMaxNode)
{
int HighScore = -1000;
for (int i =0; i< (int)node->children.size(); i++)
{
nScore= minimax(node, depth-1, Alpha, Beta, false);
HighScore= max(HighScore,nScore);
Alpha= max(Alpha, HighScore);
if (beta<=alpha)
{
break;
}
}
return Alpha;
}
else
{
int LowScore = 1000;
for (int i =0; i< (int)node->children.size(); i++)
{
nScore= minimax(node,depth-1, Alpha, Beta, true);
LowScore= min(LowScore,nScore);
Beta= min(Beta, LowScore);
if (beta<=alpha)
{
break;
}
}
return Beta;
}
}
/* This function serches for the best move based on teh minimax evaluation by looking at
* the children of the root node.
* Called By Move::AiMove() */
void GameTree::GetNextMove()
{
nodeNext = *nodeRoot->children.begin();
for (Node *n : nodeRoot->children)
{
if (n->score > nodeNext->score)
{
nodeNext = n;
}
}
nodeNext->UpdateDisplay(4);
return;
}
在此先感谢您的帮助,我从该网站获得了很多帮助,而无需提出任何问题。希望你能再次帮助我。谢谢。
【问题讨论】:
-
我在 Qt ide 中编写代码并使用 C++。
-
您只分析叶子的位置,这些节点值将向下渗透到根以选择最佳移动。
-
谢谢,@seccpur,我可以跟得上。在 GetNextMove( ) 函数中,我需要实现什么代码来访问根节点的子节点,这将导致树末端的最佳移动路径?目前看起来它只会搜索根节点子节点以获得最佳分数,而不考虑极小极大结果。
-
将位置信息和分数保存在一个结构中。所以你有最高分和移动键。请参阅 github 上的一些示例,例如 GNU chess 或 chess88。