【发布时间】:2012-11-23 04:20:34
【问题描述】:
我尝试编写 Russel Norvig 关于人工智能的书中给出的井字游戏的极小极大算法。除了将 bestMove 返回给用户的方式之外,它拥有一切。我正在努力返回 bestMove,但无法决定何时选择 bestMove。帮助,有人吗?
moveT MiniMax(stateT state)
{
moveT bestMove;
max_move(state,bestMove);
return bestMove;
}
int max_move(stateT state,int & bestMove)
{
int v = -10000;
if(GameIsOver(state))
{
return EvaluateStaticPosition(state);
}
vector<moveT> moveList;
GenerateMoveList(state, moveList);
int nMoves = moveList.size();
for(int i = 0 ; i < nMoves ; i++)
{
moveT move = moveList[i];
MakeMove(state, move);
int curValue = min_move(state,bestMove);
if(curValue > v)
{
v = curValue;
bestMove = move;
}
RetractMove(state, move);
}
return v;
}
int min_move(stateT state, int &bestMove)
{
int v = 10000;
if(GameIsOver(state))
{
return EvaluateStaticPosition(state);
}
vector<moveT> moveList;
GenerateMoveList(state, moveList);
int nMoves = moveList.size();
for(int i = 0 ; i < nMoves; i++)
{
moveT move = moveList[i];
MakeMove(state, move);
int curValue = max_move(state,depth+1,bestMove);
if(curValue < v)
{
curValue = v;
}
RetractMove(state, move);
}
return v;
}
P.S.:还有其他伪代码可以找到 minmax 值。但是,他们只专注于井字游戏,我正在尝试将其扩展到其他游戏。谢谢。
更新:整个代码可以在这里找到:http://ideone.com/XPswCl
【问题讨论】:
-
您在上面发布的代码是最新的吗?因为它看起来不应该编译。在
min_move中,您使用三个参数调用max_move,但 max_move 只能使用两个参数。 -
@Kevin:哎呀,现在更新了。我试图在某个时候限制深度。
-
感谢更新,但错误的行仍然存在:
int curValue = max_move(state,depth+1,bestMove);这让我担心;这让我怀疑您发布的代码不是您正在编译的代码。这使得潜在的回答者发现问题变得更加困难。我们将在发布的代码中识别出真实代码中不存在的错误,如果它们不在发布的代码中,我们将无法在真实代码中找到错误。 -
请查看更新,给您造成的困扰,抱歉;整个代码在这里:ideone.com/XPswCl
-
感谢您发布整个代码。为了其他回答者的利益,这里是一个计算机播放不完美的示例:选择移动 5,然后选择 7。计算机应该将其第二个棋子放在右上方的插槽中以阻止您的对角线获胜,但它却选择了左上方的插槽.
标签: c++ artificial-intelligence minimax