C++ 连接四个-Negamax AI评估功能问题
我正在尝试为Connect 4实现NegaMax ai。该算法在某些时候运行良好,人工智能可以获胜。然而,有时它完全无法阻挡对手连续三次,或者当它连续三次时,它也无法取得胜利 求值函数在网格中迭代(水平、垂直、对角向上、对角向下),并获取每组四个正方形。然后,它在每个集合内进行检查,并基于此进行计算 我根据此处提供的评估代码创建了该函数: 我的职能如下:C++ 连接四个-Negamax AI评估功能问题,c++,artificial-intelligence,minimax,negamax,C++,Artificial Intelligence,Minimax,Negamax,我正在尝试为Connect 4实现NegaMax ai。该算法在某些时候运行良好,人工智能可以获胜。然而,有时它完全无法阻挡对手连续三次,或者当它连续三次时,它也无法取得胜利 求值函数在网格中迭代(水平、垂直、对角向上、对角向下),并获取每组四个正方形。然后,它在每个集合内进行检查,并基于此进行计算 我根据此处提供的评估代码创建了该函数: 我的职能如下: //All sets of four tiles are evaluated before this //and values
//All sets of four tiles are evaluated before this
//and values for the following variables are set.
if (redFoursInARow != 0)
{
redScore = INT_MAX;
}
else
{
redScore = (redThreesInARow * threeWeight) + (redTwosInARow * twoWeight);
}
int yellowScore = 0;
if (yellowFoursInARow != 0)
{
yellowScore = INT_MAX;
}
else
{
yellowScore = (yellowThreesInARow * threeWeight) + (yellowTwosInARow * twoWeight);
}
int finalScore = yellowScore - redScore;
return turn ? finalScore : -finalScore; //If this is an ai turn, return finalScore. Else return -finalScore.
我的negamax函数如下所示:
inline int NegaMax(char g[6][7], int depth, int &bestMove, int row, int col, bool aiTurn)
{
{
char c = CheckForWinner(g);
if ('E' != c || 0 == depth)
{
return EvaluatePosition(g, aiTurn);
}
}
int bestScore = INT_MIN;
for (int i = 0; i < 7; ++i)
{
if (CanMakeMove(g, i)) //If column i is not full...
{
{
//...then make a move in that column.
//Grid is a 2d char array.
//'E' = empty tile, 'Y' = yellow, 'R' = red.
char newPos[6][7];
memcpy(newPos, g, sizeof(char) * 6 * 7);
int newRow = GetNextEmptyInCol(g, i);
if (aiTurn)
{
UpdateGrid(newPos, i, 'Y');
}
else
{
UpdateGrid(newPos, i, 'R');
}
int newScore = 0; int newMove = 0;
newScore = NegaMax(newPos, depth - 1, newMove, newRow, i, !aiTurn);
newScore = -newScore;
if (newScore > bestScore)
{
bestMove = i;
bestScore = newScore;
}
}
}
}
return bestScore;
}
内联int-NegaMax(字符g[6][7],int-depth,int&bestMove,int-row,int-col,bool-aiTurn)
{
{
char c=CheckForWinner(g);
如果('E'!=c | | 0==深度)
{
返回评估位置(g,aiTurn);
}
}
int bestScore=int_MIN;
对于(int i=0;i<7;++i)
{
if(CanMakeMove(g,i))//如果列i未满。。。
{
{
//…然后在该栏中移动。
//Grid是一个2d字符数组。
//“E”=空瓷砖,“Y”=黄色,“R”=红色。
char newPos[6][7];
memcpy(newPos,g,sizeof(char)*6*7);
int newRow=getnextelyincol(g,i);
if(aiTurn)
{
UpdateGrid(newPos,i,'Y');
}
其他的
{
UpdateGrid(newPos,i,'R');
}
int newScore=0;int newMove=0;
newScore=NegaMax(newPos,depth-1,newMove,newRow,i,!aiTurn);
newScore=-newScore;
如果(新闻核心>最佳分数)
{
bestMove=i;
最佳得分=新闻核心;
}
}
}
}
返回最佳分数;
}
我知道connect four已经解决了,肯定有更好的方法来解决这个问题,但是任何关于修复/改进这个问题的帮助或建议都将不胜感激。谢谢