0
私はConnectKをプレイするためにAIを書き込もうとしています(k個のゲームを接続し、重力をオンまたはオフにする必要があります)。 Minimaxアルゴリズムを使って最良の動きを得るための関数です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ターンとヒューマンターンは再帰的に均等に処理されません。そして、私はこの機能をデバッグするのにかなりの時間を費やしましたが、まだそれを理解することはできません。私の悪い英語を申し訳ありませんが、本当に助けてくれてありがとうございます。前もって感謝します。