int get_walkable_cells(struct world **world, int *possibilities, int n_possibilities, int *walkable_cells, int types_to_exclude) { int i; int found = 0; struct world cell; int row, col; for (i = 0; i < n_possibilities; ++i) { get_world_coordinates(possibilities[i], &row, &col); cell = world[row][col]; if(!(cell.type & types_to_exclude)) { walkable_cells[found++] = cell_number(row, col); } } return found; }
/* * get_adjacents: Return the number possible adjacent positions * of a given position (row, col) * adjacents: array with the cell numbers of all * adjacent positions */ int get_adjacents(int row, int col, int *adjacents) { int found = 0; /*Has up adjacent cell?*/ if(row > 0) { adjacents[found++] = cell_number(row - 1, col); } /*Has right adjacent cell?*/ if(col < max_size - 1) { adjacents[found++] = cell_number(row, col + 1); } /*Has down adjacent cell?*/ if(row < max_size - 1) { adjacents[found++] = cell_number(row + 1, col); } /*Has left adjacent cell?*/ if(col > 0) { adjacents[found++] = cell_number(row, col - 1); } return found; }
/* * get_cells_with_squirrels: Return the number of the cells * in possibilities with squirres * squirriles: Array with cell numbers of cells that have squirrels */ int get_cells_with_squirrels(struct world **world, int *possibilities, int n_possibilities, int *squirrels) { int i; int found = 0; struct world cell; int row, col; for (i = 0; i < n_possibilities; ++i) { get_world_coordinates(possibilities[i], &row, &col); cell = world[row][col]; if(cell.type == SQUIRREL) { squirrels[found] = cell_number(row, col); found++; } } return found; }
int main() { int gen = 1; FILE *fp; if ((fp = fopen("cells.txt", "w")) == NULL) { fprintf(stderr, "error: cannot open a file.\n"); return 1; } init_cells(); print_cells(fp); while (1) { printf("gen = %d\n", gen++); cell_number(); update_cells(); print_cells(fp); } fclose(fp); }
/* * choose_position: Apply the rule to choose a position * even when we have more than one to choose */ inline int choose_position(int row, int col, int p) { int c = cell_number(row, col); return c % p; }