diff --git a/a3/world.c b/a3/world.c index e69de29..56c6d64 100644 --- a/a3/world.c +++ b/a3/world.c @@ -0,0 +1,34 @@ +#include "world.h" +#include + +struct state* create_world(int width, int height) { + struct state* st = malloc(sizeof(struct state)); + if (st == NULL) { + return NULL; + } + st->width = width; + st->height = height; + st->sx = 1; + st->sy = 0; + st->snake = add_snake(NULL, width / 2, height / 2); + + for (int i = 0; i < FOOD_COUNT; i++) { + st->foodx[i] = rand() % width; + st->foody[i] = rand() % height; + } + return st; +} + +void free_world(struct state* st) { + if (st != NULL) { + free_snake(st->snake); + free(st); + } +} + +void change_direction(struct state* st, int dx, int dy) { + if (st->sx != -dx || st->sy != -dy) { + st->sx = dx; + st->sy = dy; + } +}