i want to code a recursive function to determine the best move in a given tic - tac - toe game
int nextMove(struct Game g, enum Symbol player) {
if (game_over(g) != 0) {
return -1;
}
int i, j;
int score, end;
for(i = 0; i < 3; i += 1) {
for (j = 0; j < 3; j += 1) {
if(g.board.fields[i][j] == 0) {
g.board.fields[i][j] = player;
score = nextMove(g, -player);
}
}
}
end = game_over(g)
}
My game_over function :
enum Symbol game_over(struct Game g)
{
int x, y = x = 0;
int full = 0;
for (y = 0; y < SIZE_Y_AXIS; y += 1)
{
for (x = 0; x < SIZE_X_AXIS; x += 1)
{
if (g.board.fields[x][y] == NONE)
full++;
else if (x < SIZE_X_AXIS - 2 &&
g.board.fields[x][y] == g.board.fields[x+1][y] &&
g.board.fields[x][y] == g.board.fields[x+2][y])
return g.board.fields[x][y];
else if (y < SIZE_Y_AXIS - 2 &&
g.board.fields[x][y] == g.board.fields[x][y+1] &&
g.board.fields[x][y] == g.board.fields[x][y+2])
return g.board.fields[x][y];
else if (x < SIZE_X_AXIS - 2 && y < SIZE_Y_AXIS - 2 &&
g.board.fields[x][y] == g.board.fields[x+1][y+1] &&
g.board.fields[x][y] == g.board.fields[x+2][y+2])
return g.board.fields[x][y];
else if (x >= 2 && y < SIZE_Y_AXIS - 2 &&
g.board.fields[x][y] == g.board.fields[x-1][y+1] &&
g.board.fields[x][y] == g.board.fields[x-2][y+2])
return g.board.fields[x][y];
}
}
if (full == 0)
return FULL;
return NONE;
}
I think the leaf production works fine, but i don't know how to determine which leaf (or which move) is the best one? Any suggestions by now?
Thanks