Hi,
Sorry if it doesn't belong here (it's not really c++, not entirely c and I think it's not general enough for the general AI programming category). Appreciate moving it if needed.
So I bumped into an attempt to implement the simple minimax algorithm in a Tic Tac Toe game:
Code:
bool CheckWin(char board[3][3], int i, int j, char player)
{
board[i][j] = player;
if (CheckWin(board, i, j))
{
board[i][j] = 0;
return true;
}
board[i][j] = 0;
return false;
}
int CountWinAndLose(char board[3][3], char player)
{
int count = 0;
for (int i=0;i<3;i++)
for (int j=0;j<3;j++)
if (board[i][j] == 0)
{
if (CheckWin(board, i, j, player))
count++;
if (CheckWin(board, i, j, player == 'X' ? 'O' : 'X'))
count--;
}
return count;
}
int MiniMax(char board[3][3], char player, int depth, int max, int* maxij, int torns)
{
int count;
if (depth == 0)
return max;
if (torns == 10)
return 0;
for (int k=0;k<3;k++)
for (int l=0;l<3;l++)
if (board[k][l] == 0)
{
count = 0;
board[k][l] = player;
count = CountWinAndLose(board, player)
+ MiniMax(board, player == 'X' ? 'O' : 'X', depth - 1, max, maxij, torns + 1);
if (count > max)
{
max = count;
*maxij = k * 3 + l;
}
board[k][l] = 0;
}
return max;
}
Yep it doesn't work.. any ideas?
Thanks (: