c - 我怎样才能得到我的代理人接下来应该调动的职位?
问题描述
我是一个在 C 语言中研究猎物 - 捕食者模拟的初学者,我需要根据一个标准来决定我的每个代理下一步将移动到的位置。只允许上、右、下和左(没有对角线)。候选/可能的单元格应从 0 开始按顺时针顺序编号(因此应为上->右->下->左)。如果这些单元格为空,则每个单元格都是可能的候选者。
typedef enum Neighbor {
up, right, down, left
} Neighbor;
这是我的单元结构和初始化:
typedef struct Cell {
int x, y;
int empty;
Neighbor *neighbor;
Agent *agent;
} Cell;
Cell *initCell(int x, int y) {
Cell *cell = calloc(1, sizeof(Cell));
cell->x = x;
cell->y = y;
cell->empty = 0;
cell->agent = NULL;
return cell;
}
我有一个辅助函数,它获取所有可能的单元格并根据以下标准选择代理将移动到的单元格:(ngen+x+y)%count。该函数返回所选单元格。
Cell *getAdjacentEmpty(Field *field, int ngen, int x, int y) {
int dx, dy;
int count = 0;
Cell *candidates[4];
for (Neighbor n = up; n <= left; n++) {
switch (n)
{
case up:
dx = 0; dy = -1;
break;
case right:
dx = 1; dy = 0;
break;
case down:
dx = 0; dy = 1;
break;
case left:
dx = -1; dy = 0;
break;
}
Cell *candidate = position(field, x+dx, y+dy);
if (candidate->agent == NULL && candidate->agent->species != ROCK) {
if (x > 0 && y > 0 && x <= field->rows-1 && y <= field->cols-1) {
candidates[count] = candidate;
count++;
}
}
}
if (count == 0)
return NULL;
return candidates[(ngen+x+y)%count];
我从另一个函数调用此函数,以在每次迭代时更新我的字段。
void updateField(Field *field, int ngen, int rproc, int fproc, int fhunger, int move) {
int i,j;
int rows = field->rows;
int cols = field->cols;
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
Cell *cell = position(field, i, j);
Agent *agent = cell->agent;
Cell *nextcell = getAdjacentEmpty(field, ngen, agent->x, agent->y);
nextcell->agent = agent;
agent->x = nextcell->x;
agent->y = nextcell->y;
(...)
}
}
}
当我尝试运行它时,我收到此错误:“错误:预期表达式 Cell *nextcell = getAdjacentEmpty(field, ngen, agent...”
基本上,当我从 updateField 调用 getAdjacentEmpty 函数时,我无法获得要移动的坐标,我完全被卡住了。
如何从 updateField 函数执行此操作?我很感激任何帮助。如果我在做其他没有意义的事情,请随时指出。
非常感谢
解决方案
推荐阅读
- javascript - Apollo - 将客户端链接到多个 graphql 模式注册表
- optimization - 自动优化和谷歌洞察
- sql - 查找同时注册了两门课程的学生
- wordpress - 如何将邮件从 mail.domain.com 发送到 mail2.domain.com
- ios - Mapkit 标注附件按钮激活
- soap - Amazon Java 客户端库
- laravel - 在 laravel 中调用 null 上的成员函数 move()
- here-api - 根据交通状况访问和着色特定车道
- scrapy - Scrapy - 蜘蛛调度
- asp.net-mvc - d3 sunburst zoom 在 html 中工作,但在 mvc 视图中不工作