C
C#8mo ago
Freez

❔ TicTacToe Minimax isnt working properly

Hello ive been trying to make a tictactoe game where the player plays against an AI that uses the minimax algorithm but the bot is making suboptimal moves and even after surfing the web i am confused as to why.. the methods i use to determine a move for the bot are
GetMove(); Minimax(); Eval()
GetMove(); Minimax(); Eval()
im attaching the whole code as a file
16 Replies
JakenVeina
JakenVeina8mo ago
define "suboptimal moves"
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Freez
Freez8mo ago
the algorithm isnt taking the best moves in a given situation hence missing easy wins or losing when it could be easily avoided
JakenVeina
JakenVeina8mo ago
in a given situation
like what?
Freez
Freez8mo ago
No description
Freez
Freez8mo ago
in this example where the bot is X and its his and everything in white is already in the game he would play the red X therefore missing the easy win with the green one
JakenVeina
JakenVeina8mo ago
excellent, let's run the algorithm for that state this is our move logic, yeah?
int maxEval = int.MinValue;

for (int y = 0; y < currentGame._size; y++)
{
for (int x = 0; x < currentGame._size; x++)
{
if (currentGame.IsSafe(y, x))
{
currentGame._grid[y, x] = 'X';
int eval = MiniMax(currentGame, 'O');
currentGame._grid[y, x] = ' ';
if (eval > maxEval)
{
maxEval = eval;
_move = new int[] { y, x };
}
}
}
}
return maxEval;
int maxEval = int.MinValue;

for (int y = 0; y < currentGame._size; y++)
{
for (int x = 0; x < currentGame._size; x++)
{
if (currentGame.IsSafe(y, x))
{
currentGame._grid[y, x] = 'X';
int eval = MiniMax(currentGame, 'O');
currentGame._grid[y, x] = ' ';
if (eval > maxEval)
{
maxEval = eval;
_move = new int[] { y, x };
}
}
}
}
return maxEval;
what happens for a game state of
x 0 1 2
y
0 X O O
1 X
2
x 0 1 2
y
0 X O O
1 X
2
Freez
Freez8mo ago
we get a maxEval of 3 with a move of y=1, x=1 going from this
Freez
Freez8mo ago
No description
Freez
Freez8mo ago
to this
Freez
Freez8mo ago
No description
Freez
Freez8mo ago
(dont mind the cyan square its the player's indicator)
JakenVeina
JakenVeina8mo ago
why's that? it ought to be a 4 er rather the optimal move is a 4 for y=2, x=0 is that one not returning a 4, or is the 4 not getting captured properly?
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Freez
Freez8mo ago
i think it returned a 3 for it too when debugging but since its the same it didnt overwrite it as a better eval im not able to look into it today nor the first half of tomorrow but after that id be free if you're still eager to help resolve this issue in the meantime you could of course look into it on yoyr own if you'd like
JakenVeina
JakenVeina8mo ago
I have if the evaluation for y=2,x=0 is returning 3, then that's your problem. It should be 4.