I'm developing a little AI for a battleship game and I'm using rand function for first tests but I get random segmentation faults because of the rand() function. I mean, it can occur anytime without any link between cases.
Here is my code :
int get_ai_pos(t_game *game)
{
t_ai_state *ai;
int result;
result = 0;
ai = game->ai;
srand(time(NULL));
result += rand() % 8 + 1;
result += (rand() % 8 + 1) * 10;
if (get_case_state(game, result))
return (get_ai_pos(game));
else
{
ai->last_hit = result;
return (ai->last_hit);
}
}
int get_case_state(t_game *game, int coor)
{
char state;
state = game->cases[coor % 10 - 1][coor / 10 - 1];
if (state == '.')
return (0);
else if (state == 'o')
return (1);
else if (state == 'x')
return (2);
}
In my header, I include and I make a srand.
You maybe should know that the "get_ai_pos" function is called at every turn for the AI.
Aucun commentaire:
Enregistrer un commentaire