From 7bf81d50fee61187a0b2e91010c8f6c196f9d10d Mon Sep 17 00:00:00 2001 From: Bohdana Marchenko Date: Thu, 1 May 2025 11:47:00 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?a3=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- a3/snake.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 a3/snake.c diff --git a/a3/snake.c b/a3/snake.c new file mode 100644 index 0000000..810947a --- /dev/null +++ b/a3/snake.c @@ -0,0 +1,57 @@ +#include "snake.h" +#include + +struct snake* add_snake(struct snake* snake, int x, int y) { + struct snake* head = calloc(1, sizeof(struct snake)); + if (head == NULL) { + return NULL; // Handle memory allocation failure + } + head->x = x; + head->y = y; + head->next = snake; + return head; +} + +struct snake* remove_snake(struct snake* snake) { + if (snake == NULL) { + return NULL; + } + if (snake->next == NULL) { + free(snake); + return NULL; + } + + struct snake* this = snake; + struct snake* prev = NULL; + while (this->next != NULL) { + prev = this; + this = this->next; + } + + free(this); + prev->next = NULL; + return snake; +} + +void free_snake(struct snake* sn) { + if (sn == NULL) { + return; + } + free_snake(sn->next); + free(sn); +} + +int is_snake(struct snake* snake, int x, int y) { + for (struct snake* this = snake; this != NULL; this = this->next) { + if (this->x == x && this->y == y) { + return 1; + } + } + return 0; +} + +int step_state(struct state* st) { + int nx = (st->snake->x + st->sx); + int ny = (st->snake->y + st->sy); + return END_CONTINUE; +}