-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
353 lines (297 loc) · 9.45 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <unistd.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
{
AnimalStatus status;
AnimalType type;
Location location;
} Animal;
Animal bird, bear, panda;
typedef struct
{
int points;
Location location;
} Hunter;
typedef struct
{
Hunter **hunters;
int nhunters;
Animal **animals;
int nanimals;
SiteType type;
} Site;
/** 2D site grid */
typedef struct
{
int xlength;
int ylength;
Site **sites;
} Grid;
/* initial grid, empty */
Grid grid = {0, 0, NULL};
Grid initgrid(int xlength, int ylength)
{
grid.xlength = xlength;
grid.ylength = ylength;
// Seed the random number generator with the current time
srand(time(NULL));
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;
}
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;
}
void printgrid()
{
for (int i = 0; i < grid.xlength; i++)
{
for (int j = 0; j < grid.ylength; j++)
{
Site *site = &grid.sites[i][j];
int count[3] = {0}; /* do not forget to initialize*/
for (int a = 0; a < site->nanimals; a++)
{
Animal *animal = site->animals[a];
count[animal->type]++;
}
printf("|%d-{%d, %d, %d}{%d}|", site->type, count[0], count[1],
count[2], site->nhunters);
}
printf("\n");
}
}
void printsite(Site *site)
{
int count[3] = {0}; /* do not forget to initialize*/
for (int a = 0; a < site->nanimals; a++)
{
Animal *animal = site->animals[a];
count[animal->type]++;
}
printf("|%d-{%d,%d,%d}{%d}|", site->type, count[0], count[1], count[2],
site->nhunters);
}
void *simulateanimal(void *args)
{
Animal *animal = (Animal *)args;
while (1)
{
// Get the current site location of the animal
int currentX = animal->location.x;
int currentY = animal->location.y;
SiteType currentSiteType = grid.sites[currentX][currentY].type;
// Simulate behavior based on the current site type
if (currentSiteType == NESTING)
{
// Create a new animal of the same type at the current site
Animal *newAnimal = (Animal *)malloc(sizeof(Animal));
newAnimal->status = ALIVE;
newAnimal->type = animal->type;
newAnimal->location.x = currentX;
newAnimal->location.y = currentY;
// Add the new animal to the current site
Site *currentSite = &grid.sites[currentX][currentY];
currentSite->animals = (Animal **)realloc(currentSite->animals, sizeof(Animal *) * (currentSite->nanimals + 1));
currentSite->animals[currentSite->nanimals] = newAnimal;
currentSite->nanimals++;
// Move the current animal to a neighboring random location
int newX, newY;
do
{
newX = currentX + (rand() % 3) - 1;
newY = currentY + (rand() % 3) - 1;
} while (newX < 0 || newX >= grid.xlength || newY < 0 || newY >= grid.ylength || (newX == currentX && newY == currentY));
// Update the location of the current animal
animal->location.x = newX;
animal->location.y = newY;
}
else if (currentSiteType == WINTERING)
{
// Check if the animal dies with 0.5 probability
if ((rand() / (double)RAND_MAX) <= 0.5)
{
animal->status = DEAD;
break;
}
// Move the current animal to a neighboring random location
int newX, newY;
do
{
newX = currentX + (rand() % 3) - 1;
newY = currentY + (rand() % 3) - 1;
} while (newX < 0 || newX >= grid.xlength || newY < 0 || newY >= grid.ylength || (newX == currentX && newY == currentY));
// Update the location of the current animal
animal->location.x = newX;
animal->location.y = newY;
}
else if (currentSiteType == FEEDING)
{
// Check if the animal moves with 0.8 probability
if ((rand() / (double)RAND_MAX) > 0.8)
{
// Move the current animal to a neighboring random location
int newX, newY;
do
{
newX = currentX + (rand() % 3) - 1;
newY = currentY + (rand() % 3) - 1;
} while (newX < 0 || newX >= grid.xlength || newY < 0 || newY >= grid.ylength || (newX == currentX && newY == currentY));
// Update the location of the current animal
animal->location.x = newX;
animal->location.y = newY;
}
}
// Delay before the next simulation time unit
usleep(100000); // Adjust the delay time as needed
}
return NULL;
}
void *simulatehunter(void *args)
{
Hunter *hunter = (Hunter *)args;
while (1)
{
// Get the current site location of the hunter
int currentX = hunter->location.x;
int currentY = hunter->location.y;
// Move the hunter to a neighboring random location
int newX, newY;
do
{
newX = currentX + (rand() % 3) - 1;
newY = currentY + (rand() % 3) - 1;
} while (newX < 0 || newX >= grid.xlength || newY < 0 || newY >= grid.ylength || (newX == currentX && newY == currentY));
// Update the location of the hunter
hunter->location.x = newX;
hunter->location.y = newY;
// Check if there are animals at the new location and kill them
Site *currentSite = &grid.sites[newX][newY];
int numKills = currentSite->nanimals;
currentSite->nanimals = 0;
free(currentSite->animals);
currentSite->animals = NULL;
// Update the hunter's points based on the number of kills
hunter->points += numKills;
// Delay before the next movement
usleep(100000); // Adjust the delay time as needed
}
return NULL;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s <number of hunters>\n", argv[0]);
return 1;
}
int numHunters = atoi(argv[1]);
initgrid(3, 3);
// Create thread for bear
pthread_t bear_thread;
pthread_create(&bear_thread, NULL, simulateanimal, (void *)&bear);
// Create thread for panda
pthread_t panda_thread;
pthread_create(&panda_thread, NULL, simulateanimal, (void *)&panda);
// Create thread for bird
pthread_t bird_thread;
pthread_create(&bird_thread, NULL, simulateanimal, (void *)&bird);
// Create threads for hunters
pthread_t hunter_threads[numHunters];
for (int i = 0; i < numHunters; i++)
{
Hunter *hunter = (Hunter *)malloc(sizeof(Hunter));
hunter->points = 0;
// Set the initial location of the hunter randomly
int initialX, initialY;
do
{
initialX = rand() % grid.xlength;
initialY = rand() % grid.ylength;
} while (grid.sites[initialX][initialY].nhunters > 0 || grid.sites[initialX][initialY].nanimals > 0);
hunter->location.x = initialX;
hunter->location.y = initialY;
// Add the hunter to the site
grid.sites[initialX][initialY].hunters = (Hunter **)realloc(grid.sites[initialX][initialY].hunters, sizeof(Hunter *) * (grid.sites[initialX][initialY].nhunters + 1));
grid.sites[initialX][initialY].hunters[grid.sites[initialX][initialY].nhunters] = hunter;
grid.sites[initialX][initialY].nhunters++;
// Create the hunter thread
pthread_create(&hunter_threads[i], NULL, simulatehunter, (void *)hunter);
}
// Run the simulation for 1 second
usleep(1000000); // Sleep for 1 second
// Cancel animal threads
pthread_cancel(bear_thread);
pthread_cancel(panda_thread);
pthread_cancel(bird_thread);
// Wait for animal threads to finish
void *threadResult;
pthread_join(bear_thread, &threadResult);
pthread_join(panda_thread, &threadResult);
pthread_join(bird_thread, &threadResult);
// Cancel hunter threads
for (int i = 0; i < numHunters; i++)
{
pthread_cancel(hunter_threads[i]);
}
// Wait for hunter threads to finish
for (int i = 0; i < numHunters; i++)
{
pthread_join(hunter_threads[i], &threadResult);
}
printgrid();
deletegrid();
return 0;
}