I've been trying to make a tic tac toe game with the miniMax algorithm, but have run into a problem with accessing the array at the miniMax function: if (checkGameOver(board[3][3]) != 0);
and the others.
giving the error:
incompatible integer to pointer conversion passing 'char' to parameter of type 'char *'; take the address with & [-Wint-conversion]
Pretty new to C, but I think I've messed up the referencing of the array somewhere.
int miniMax(char (*board)[3], int player)
{
if (checkGameOver(board[3][3]) != 0)
{
if (checkGameOver(board[3][3]) == 1)
{
if (whichPlayer(-1 * (player)) == 1)
{
return MIN;
}
else if (whichPlayer(-1 * (player)) == -1)
{
return MAX;
}
}
else if (checkGameOver(board[3][3]) == 2)
{
return 0;
}
else{exit(EXIT_FAILURE);}
}
}
void bestMove(char (*board)[3], int player, int *ptrRow, int *ptrCol)
{
int bestMove[2] = {-1};
int minEval = MIN;
// code. Runs through all possible moves, and returns the best move
for (int i = 0; i < 3; i)
{
for (int j = 0; j < 3; j)
{
board[i][j] = whichPlayer(player);
int evalPos = miniMax(board, -1 * player);
if (evalPos >= minEval)
{
minEval = evalPos;
bestMove[0] = i;
bestMove[1] = j;
}
}
}
// pointer to address of array in main func
*ptrRow = bestMove[0];
*ptrCol = bestMove[1];
}
int main()
{
// 2D array
char board[3][3] = {{'_', '_', '_'},
{'_', '_', '_'},
{'_', '_', '_'}};
currentPlayer = 1;
While (1)
{
char copyBoard[3][3] = {{'_'}, {'_'}, {'_'}};
for (int i = 0; i < 3; i)
{
for (int j = 0; j < 3; j)
{
copyBoard[i][j] = board[i][j];
}
}
if (currentPlayer == 1)
{
int move[2] = {-1};
bestMove(copyBoard, currentPlayer, &move[0], &move[1]);
makeMove(board, move, currentPlayer);
currentPlayer = currentPlayer * -1;
}
}
CodePudding user response:
This error message
incompatible integer to pointer conversion passing 'char' to parameter of type 'char *'; take the address with & [-Wint-conversion]
is enough clear.
In statements like this
if (checkGameOver(board[3][3]) != 0)
the argument expression board[3][3]
has the type char
and moreover supplies a non-existent element of the array board
because the valid range of indices is [0,3)
.
But the function expects an argument expression of the type char *
that is a pointer.
So such if statements do not make sense.
CodePudding user response:
To fix the error, pass by reference instead of value, so
if (checkGameOver(board[3][3]) != 0)
becomes
if (checkGameOver(&board[3][3]) != 0)
this will pass a pointer to a char instead of a char. Assuming the function signature takes a char *, they need to match.
Vlad is also right, declaring a board[3][3] means the indices are 0, 1, and 2, so passing board[3][3] is out of bounds.
Also, board[3][3] only passes a single board position, and assuming the checkGameOver function needs to check the full board for a tic-tac-toe, you need to pass a pointer to the full board instead of a single value.
So if you declare
char board[3][3];
and the function declaration is
int checkGameOver(char **board);
you can call it like this
int a = checkGameBoard(&board);