I am making a C program of Tic-Tac-Toe. I am trying to make the AI invincible at the moment but I have encountered a problem. The problem is that the AI just prints the symbol in the next available question. Why? And how can I fix it?
Here's my calling function:
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;
}
And the other recursive function:
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);
}
As I stated I'd like to know why this weird behavior occurs and how to fix it.
Any help would be much appreciated :)