我有一个类似于大学项目的东西,一个基本的“游戏”人工智能:
struct ai_config{
float followup_mul;
float aggro_ratio;
int wisdom;
};
extern struct ai_config global_AI;
struct ai_move {
int pngn;
int coords[2];
float aggro;
float safety;
int order;
struct ai_move* followup;
int followup_size;
};
还有这个功能:
int assess_moves(struct Gmdt* gmdt, struct ai_move** ai_arr, int count, int xpos, int ypos, int order, int pg){
if (order >= global_AI.wisdom) return 0;
int dir, dist;
struct ai_move* temp;
for (dir = 1; dir <= 6; dir++)
for (dist = 1; dist < estimate_mvmnt_adv(gmdt, xpos, ypos, dir); dist++)
if(check_move_ai(gmdt, xpos, ypos, dist, dir)){
count++;
temp = (struct ai_move*)realloc(*ai_arr, count * sizeof(struct ai_move));
if (temp == NULL){
printf("[AI]\tMemory reallocation error! (Order #%d)", order);
return -1;
}
else {
(*ai_arr) = temp;
(*ai_arr)[count-1].pngn = pg;
(*ai_arr)[count-1].coords[0] = polar_to_x(xpos, ypos, dir, dist);
(*ai_arr)[count-1].coords[1] = polar_to_y(xpos, dir, dist);
(*ai_arr)[count-1].order = order;
(*ai_arr)[count-1].aggro = assess_aggro(gmdt, &((*ai_arr)[count-1]));
(*ai_arr)[count-1].safety = assess_safety(gmdt, &((*ai_arr)[count-1]));
(*ai_arr)[count-1].followup_size = assess_moves(gmdt, &((*ai_arr)[count-1].followup), 0, (*ai_arr)[count-1].coords[0], (*ai_arr)[count-1].coords[1], order+1, pg);
}
}
return count;
}
在main.c中循环调用:
struct ai_move* moves;
if(gmdt.phase == 1)
{
int x, y, n = 0, c;
for (y = 0; y < gmdt.rows; y++)
for (x = 0; x < gmdt.columns; x++)
if (gmdt.map[x][y] == '1'){
c = n;
n = assess_moves(&gmdt, &moves, n, x, y, 0, -1);
printf("\n[AI]\t%d moves would be possible from the (%d, %d) position.\n", n-c, x, y);
}
}
当global_AI.wisdom设置为1时,它工作正常。但是当我将其设置为2时,realloc返回NULL,顺序等于1,即使在global_AI的同一点上不会出现相同的错误。智慧设定为1.为什么可能这样?我错过了什么吗?