# Thread: Minimax algorithm not behaving properly

1. ## Minimax algorithm not behaving properly

I have been learning to program for a couple months now and decided to take a shot at tic-tac-toe and an AI to accompany it. From what I've seen elsewhere, everything in the ai() and minimax() functions should be working properly, however I have noted that the AI tends to miss wins and assume it is a draw in won positions.
Code:
```#include <stdio.h>
#include <stdlib.h>

char board = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
int drawBoard();
int turn();
int turns();
int winner();
int ai(char current);
int minimax(char test, int player);

int main()
{
int players;

printf("How many players? (1 - 2): ");
scanf("%d", &players);

if(players == 1)
{
do
{
system("clear");
drawBoard();
turn();
if(winner(board) != 0 && winner(board) != 1 && winner(board) != -1)
{
ai(board);
}
}
while(winner(board) != 1 && winner(board) != -1 && winner(board) != 0);
}
else if(players == 2)
{
do
{
system("clear");
drawBoard();
turns();
}
while(winner(board) != 1 && winner(board) != -1 && winner(board) != 0);
}

system("clear");
drawBoard();
if(winner(board) != 0)
{
printf("Game Over!\nWinner is player %d\n", (winner(board)+3)/2);
}
else
{
printf("It is a draw!\n");
}
return 0;
}

int drawBoard()
{
for (int i = 1; i < 12; i++)
{
if(i % 4 == 0)
{
printf("---------------------\n");
}
else if(i % 2 == 0)
{
printf("  %c   |   %c   |   %c  \n", board[(i-2)/4], board [(i-2)/4],
board[(i-2)/4]);
}
else
{
printf("      |       |      \n");
}
}
}

int turn()
{
int row, col;

printf("Select a row: ");
scanf("%d", &row);
printf("Select a column: ");
scanf("%d", &col);

if(board[row-1][col-1] != ' ')
{
turn();
return 0;
}

board[row-1][col-1] = 'X';
}

int turns()
{
static int player = 1;
int row, col;

printf("Player %d select a row: ", player);
scanf("%d", &row);
printf("Player %d select a column: ", player);
scanf("%d", &col);

if(board[row-1][col-1] != ' ')
{
turns();
return 0;
}

if(player == 1)
{
board[row-1][col-1] = 'X';
player = 2;
}
else
{
board[row-1][col-1] = 'O';
player = 1;
}
}

signed int winner(char check)
{
int i, j, temp, total = 0;

for(i = 0; i < 3; i++)
{

if(check[i] == check[i] && check[i] == check[i] && check[i] != ' ')
{
temp = (int)check[i];
if(temp == 88)
{
return -1;
}
else
{
return 1;
}
}
if(check[i] == check[i] && check[i] == check[i] && check[i] != ' ')
{
temp = (int)check[i];
if(temp == 88)
{
return -1;
}
else
{
return 1;
}
}
}
if(check == check && check == check && check != ' ')
{
temp = (int)check;
if(temp == 88)
{
return -1;
}
else
{
return 1;
}
}
if(check == check && check == check && check != ' ')
{
temp = (int)check;
if(temp == 88)
{
return -1;
}
else
{
return 1;
}
}

for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
temp = (int)check[i][j];
if(check[i][j] != 32)
{
total += 1;
}}}
if(total == 9)
{
return 0;
}
}

int ai(char current)
{
signed int score, bestScore = -1000;
int bestMove;

for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if((int)current[i][j] == 32)
{
current[i][j] = 'O';
score = minimax(current, 1);
current[i][j] = ' ';
if(score > bestScore)
{
bestScore = score;
bestMove = i;
bestMove = j;
}
}
}
}
board[bestMove][bestMove] = 'O';
}

int minimax(char test, int player)
{
int i, j, score;

if(winner(test) == 0 || winner(test) == 1 || winner(test) == -1)
{
return winner(test);
}
if(player == 2)
{
signed int bestScore = -1000;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if((int)test[i][j] == 32)
{
test[i][j] = 'O';
drawBoard();
score = minimax(test, 1);
test[i][j] = ' ';

if(score > bestScore)
{
bestScore = score;
}
}
}}
return bestScore;
}
else
{
signed int bestScore = 1000;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if((int)test[i][j] == 32)
{
test[i][j] = 'X';
drawBoard();
score = minimax(test, 2);
test[i][j] = ' ';

if(score < bestScore)
{
bestScore = score;
}
}
}}
return bestScore;
}
}```
I have looked through this so many times and can not for the life of me find an error. Thanks in advance! 2. It is worth noting that the algorithm will never lose. When X is placed in the centre of the board, it will always pick a corner (whether or not that is thanks to the minimax is debatable), and it will always pick the centre if any other move is played. It just seems to ignore wins for some reason... 3. Code:
`int winner();`
1. Fix the prototype for winner.
2. Stop using so many magic numbers

Magic number (programming) - Wikipedia

Tim S. 4. Your winner function doesn't have a return value for NOTHING, i.e., not a win for X or O and not a draw. In that case it just "falls off the end" of the function and returns an arbitrary value. Try:
Code:
```int winner(char check)
{
for (int i = 0; i < 3; i++)
{
if (check[i] == check[i] &&
check[i] == check[i] &&
check[i] != ' ')
{
return check[i] == 'X' ? -1 : 1;
}
if (check[i] == check[i] &&
check[i] == check[i] &&
check[i] != ' ')
{
return check[i] == 'X' ? -1 : 1;
}
}

if (check == check &&
check == check &&
check != ' ')
{
return check == 'X' ? -1 : 1;
}

if (check == check &&
check == check &&
check != ' ')
{
return check == 'X' ? -1 : 1;
}

int total = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (check[i][j] != ' ')
++total; Popular pages Recent additions bestscore, int, minimax, return, tic tac toe 