我正在制作井字游戏的 C 程序。我现在正试图让 AI 无敌,但我遇到了一个问题。问题是 AI 只是在下一个可用问题中打印符号。为什么?我该如何解决?
这是我的调用函数:
void determinMove(char positions[])
{
int num, i, currentBestScore, score;
currentBestScore = -100;
for(i = 0; i < 9; i++)
{
if(positions[i] == ' ')
{
score = getFutureScoreOfMove(positions, 'C');
if(score > currentBestScore)
{
num = i;
currentBestScore = score;
}
positions[i] = ' ';
}
}
positions[num] = comp;
}
另一个递归函数:
int getFutureScoreOfMove(char positions[], char turn)
{
int i, currentBestScore, score;
if(turn == 'C') currentBestScore = -100;
else currentBestScore = 100;
for(i=0; i<9; i++)
{
if(positions[i] == ' ')
{
if(turn == 'C')
{
positions[i] = 'X';
score = getFutureScoreOfMove(positions, 'U');
positions[i] = ' ';
}
else
{
positions[i] = 'O';
score = getFutureScoreOfMove(positions, 'C');
positions[i] = ' ';
}
if(turn == 'C' && score > currentBestScore)
currentBestScore = score;
if(turn == 'U' && score < currentBestScore)
currentBestScore = score;
}
}
return(currentBestScore);
}
正如我所说,我想知道为什么会发生这种奇怪的行为以及如何解决它。
任何帮助将非常感激 :)