【发布时间】:2016-10-16 04:56:19
【问题描述】:
我正在尝试编写一个 AI 来玩 ConnectK(一个需要连接 k 件的连接 4 游戏,并且重力可以打开或关闭)。这是我使用 Minimax 算法获得最佳移动的函数。
struct MoveNode //This struct is defined in header file
{
MoveNode() {};
MoveNode(int Score) : score(Score) {}
Move move;
int score;
};
MoveNode AIShell::getBestMove(int depth, int player) {//Find the best move using MiniMax
if (depth <= 0)
return MoveNode(heuristic());
else if (boardIsFull() && getWinner() == 0)//Tie
return 0;
else if (getWinner() == AI_PIECE)
return 100000;
else if (getWinner() == HUMAN_PIECE)
return -100000;
std::vector<MoveNode> mds;
for (auto i : getMoveList()) {//For each available move
MoveNode md;
md.move = i; //i is Move(col,row)
gameState[i.col][i.row] = player;
if (player == AI_PIECE) {
md.score = getBestMove(depth - 1, HUMAN_PIECE).score;
}
else {
md.score = getBestMove(depth - 1, AI_PIECE).score;
}
mds.push_back(md);
}
//Get the best move after recursion
int best_move_index = 0;
if (player == AI_PIECE) {
int best_score = -1000000;
for (int i = 0; i < mds.size(); i++) {
if (mds[i].score > best_score) {
best_move_index = i;
best_score = mds[i].score;
}
}
} else if (player == HUMAN_PIECE) {
int best_score = 1000000;
for (int i = 0; i < mds.size(); i++) {
if (mds[i].score < best_score) {
best_move_index = i;
best_score = mds[i].score;
}
}
}
return mds[best_move_index];
}
getBestMove() 函数似乎做了一些我不太期待的事情。该函数将在递归之前尝试获得最佳移动,并且AI轮和Human轮没有得到均匀的递归处理。而且我已经花了很长时间调试这个功能,但仍然无法弄清楚。对不起,我的英语不好,但我真的很感激这里的帮助。提前致谢。
【问题讨论】: