I'm building a chess program in C. I ran valgrind on the program and got:
==6683== Conditional jump or move depends on uninitialised value(s)
==6683== at 0x405F23: getBestMove (MiniMax.c:318)
==6683== by 0x403933: computerTurn (Chess.c:864)
==6683== by 0x402947: runGame (Chess.c:579)
==6683== by 0x403F66: main (Chess.c:981)
move_t* getBestMove(game_t game, int player, unsigned int depth)
{
move_t *cur = NULL, *best = NULL;
double alpha = INT_MIN, score = 0;
firstMove = NULL;
cur = getAllMoves(game, player);
firstMove = 0;
while (cur != NULL)
{
game_t copy;
copyGame(game, ©);
makeMove(©, *cur);
//root->game = copy;
firstMove = NULL;
score = -alphaBetaMax(-INT_MAX, -alpha, depth - 1, copy, !player);
freeGame(copy);
if (board_count > MAX_BOARDS)
break;
if (score > alpha) // line 318
{
alpha = score;
if (best != NULL)
{
best->next = NULL;
free(best);
}
best = copyMove(*cur);
}
cur = cur->next;
}
freeMoves();
if (board_count > MAX_BOARDS)
{
free(best);
return 0;
}
return best;
}
What I don't get is that both of the variables in the if (score, alpha) are initilized to (0,INT_MIN) respectively. So why is valgrind printing this error? Any help would be much appreciated!
Thanks for all the help so far, switching to DBL_MAX didn't affect the problem. Here's the code for alphaBetaMax:
static double alphaBetaMax(double alpha, double beta, int depthleft,
game_t game, bool player)
{
move_t *cur;
double score = 0;
bool did_move = false;
cur = getAllMoves(game, player); // firstMove is a global list of all moves for current player
firstMove = 0;
while (cur != NULL)
{
game_t copy;
if (depthleft <= 0 && !isCapture(game, cur))
{
cur = cur->next;
continue;
}
did_move = true;
copyGame(game, ©);
makeMove(©, *cur);
firstMove = NULL;
score = -alphaBetaMax(-beta, -alpha, depthleft - 1, copy, !player);
if (board_count > MAX_BOARDS)
break;
freeGame(copy);
if (score > alpha) // ln 278
alpha = score;
if (beta <= alpha) // ln 281
break;
cur = cur->next;
}
freeMoves();
if (!did_move)
alpha = evaluate(game) * (player * 2 - 1);
return alpha;
}
Valgrind prints the same message regarding lines 278 and 281, which is hard for me to see why. Thanks for all the help!