Hello I am attempting to create an unbeatable Tic-Tac-Toe game using the MiniMax Algorithm. I keep running it to issues with my code. My MiniMax algorithm appears to work but it does not terminate once it finds the optimal play. In line 7-18 I established parameters for it to terminate but it continues to execute without stopping. My code is too long to submit in one post so I'll piece it together into multiple replies.
//*******************************************
// Definition of Min_Max function. *
//*******************************************
int Min_Max(char Board[][GRID_SIZE], int Depth, bool isMax)
{
int Max_Move = -1000;
int Score = Check_For_Winner(Board); // Set the value of Score
cout << "The value of Score is " << Score << endl;
if (Score == 10) // Return the value of Score if the computer has won
return Score;
if (Score == -10) // Return the value of Score if the human player has won
return Score;
if ((Check_For_Winner(Board) == 0) && (index == GRID_SIZE * GRID_SIZE))
return 0;
if (isMax == false) // Computer Player's turn
{
for (int i = 0; i < GRID_SIZE; i++)
{
for (int j = 0; j < GRID_SIZE; j++)
{
// Check to see if a player has made a move there
while (!((Board[i][j] == 'X') || (Board[i][j] == 'O')))
{
// Store the current value of the element in the array
char Temporary = Board[i][j];
cout << "The contents of temporary for Max is " << Temporary << endl;
// Make the move for the computer
Board[i][j] = AI_Move;
cout << "The value for the element Max is " << Board[i][j] << endl;
Display_Board(Board);
cout << "The value of Max_Move " << Max_Move << endl;
Max_Move = max(Max_Move,
Min_Max(Board, Depth++, !isMax));
// Undo the move made by the computer
Board[i][j] = Temporary;
cout << "The contents of temporary for Max is " << Temporary << endl;
}
}
}
return Max_Move;
}
elseif (isMax == true) // Human Player's turn
{
int Min_Move = 1000;
for (int i = 0; i < GRID_SIZE; i++)
{
for (int j = 0; j < GRID_SIZE; j++)
{
// Check to see if a player has made a move there
while (!((Board[i][j] == 'X') || (Board[i][j] == 'O')))
{
// Store the current value of the element in the array
char Temporary_2 = Board[i][j];
cout << "The contents of temporary for Min is " << Temporary_2 << endl;
// Make the move for the Human player
Board[i][j] = Human_Move;
cout << "The value for the element Min is " << Board[i][j] << endl;
Display_Board(Board);
Min_Move = min(Min_Move,
Min_Max(Board, Depth++, !isMax));
// Undo the move made for the Human player
Board[i][j] = Temporary_2;
cout << "The contents of temporary for Min is " << Temporary_2 << endl;
}
}
}
return Min_Move;
}
}
//***********************************************************
// Definition of structure function Best_Computer_Move. *
//***********************************************************
Computer Best_Computer_Move(char Board[][GRID_SIZE], int Player)
{
cout << "You are in the structure" << endl;
int Best_Value = -1000;
Computer Best_Move; // Create a structure variable
Best_Move.Row = -1;
Best_Move.Column = -1;
for (int i = 0; i < GRID_SIZE; i++)
{
for (int j = 0; j < GRID_SIZE; j++)
{
// Check to see if a player has made a move there
while (!((Board[i][j] == 'X') || (Board[i][j] == 'O')))
{
cout << "i = " << i << " j = " << j << endl;
// Store the current value of the element in the array
char Temporary = Board[i][j];
cout << "The contents of temporary int the structure is " << Temporary << endl;
// Make the move for the computer
Board[i][j] = AI_Move;
cout << Board[i][j] << endl;
// Calculate the Maximizing move
int Value = Min_Max(Board, 0, false);
// Undo the move made by the computer
Board[i][j] = Temporary;
if (Value > Best_Value)
{
Best_Move.Row = i;
Best_Move.Column = j;
Best_Value = Value;
}
}
}
}
return Best_Move;
}