#include <stdio.h> #include <stdlib.h> typedef enum { BEAR, BIRD, PANDA} AnimalType;…
- User
#include <stdio.h> #include <stdlib.h>
typedef enum { BEAR, BIRD, PANDA} AnimalType;
typedef enum { ALIVE, DEAD } AnimalStatus;
typedef struct { int x; int y; } Location;
typedef enum { FEEDING, NESTING, WINTERING } SiteType;
typedef struct { /** animal can be DEAD or ALIVE*/ AnimalStatus status; /** animal type, bear, bird, panda*/ AnimalType type; /** its location in 2D site grid*/ Location location; } Animal;
/example usage/ Animal bird, bear, panda;
/** type of Hunter*/ typedef struct { /** points indicate the number of animals, a hunter killed*/ int points; /** its location in the site grid*/ Location location; } Hunter;
/** type of a site (a cell in the grid)/ typedef struct { /** array of pointers to the hunters located at this site/ Hunter hunters; / the number of hunters at this site*/ int nhunters; /** array of pointers to the animals located at this site*/ Animal animals; / the number of animals at this site*/ int nanimals; /** the type of site*/ SiteType type; } Site;
/** 2D site grid*/ typedef struct { /** number of rows, length at the x-coordinate*/ int xlength; /** number of columns, length at the y-coordinate*/ int ylength; /** the 2d site array*/ Site **sites; } Grid;
/* initial grid, empty*/ Grid grid = {0, 0, NULL};
/**
-
@brief initialize grid with random site types
-
@param xlength
-
@param ylength
-
@return Grid */ Grid initgrid(int xlength, int ylength) { grid.xlength = xlength; grid.ylength = ylength;
grid.sites = (Site **)malloc(sizeof(Site *) * xlength); for (int i = 0; i < xlength; i++) { grid.sites[i] = (Site *)malloc(sizeof(Site) * ylength); for (int j = 0; j < ylength; j++) { grid.sites[i][j].animals = NULL; grid.sites[i][j].hunters = NULL; grid.sites[i][j].nhunters = 0; grid.sites[i][j].nanimals = 0; double r = rand() / (double)RAND_MAX; SiteType st; if (r < 0.33) st = WINTERING; else if (r < 0.66) st = FEEDING; else st = NESTING; grid.sites[i][j].type = st; } }
return grid; }
/**
- @brief
*/ void deletegrid() { for (int i = 0; i < grid.xlength; i++) { free(grid.sites[i]); }
free(grid.sites); grid.sites = NULL; grid.xlength = -1; grid.ylength = -1;}
/**
-
@brief prints the number animals and hunters in each site
-
of a given grid
-
@param grid / void printgrid() { for (int i = 0; i < grid.xlength; i++) { for (int j = 0; j < grid.ylength; j++) { Site do not forget to initialize/ for (int a = 0; a < site->nanimals; a++) { Animal *animal = site->animals[a]; count[animal->type]++; }
-
- Assistant
- User
you should write the full code
- Assistant
- User
how do you write the move function for the animals also controlling edge cases
- Assistant
You can create a helper function called
move_animalthat handles the moving of the animal from one location to another while considering edge cases. To do this, get x-length and y-length of the grid, and use them to determine whether the new position is valid before moving the animal.Here's an example of the
move_animalfunction:void move_animal(Animal *animal, int dx, int dy) { int new_x = animal->location.x + dx; int new_y = animal->location.y + dy; if (new_x >= 0 && new_x < grid.xlength && new_y >= 0 && new_y < grid.ylength) { animal->location.x = new_x; animal->location.y = new_y; } }