From de56e6f91c3784e665ba08b14c556cd806998a7a Mon Sep 17 00:00:00 2001 From: Vladyslav Korzun Date: Thu, 20 Apr 2023 05:57:12 +0000 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'du7/snake.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du7/snake.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/du7/snake.c b/du7/snake.c index 7038cdb..e8d5350 100644 --- a/du7/snake.c +++ b/du7/snake.c @@ -1,21 +1,47 @@ #include "snake.h" #include -struct snake* add_snake(struct snake* snake,int x,int y){ - return NULL; +struct snake* add_snake(struct snake* snake, int x, int y) { + struct snake* head = calloc(1, sizeof(struct snake)); + head->x = x; + head->y = y; + head->next = snake; + return head; } -struct snake* remove_snake(struct snake* snake){ - return NULL; +struct snake* remove_snake(struct snake* snake) { + struct snake* prev = NULL; + struct snake* curr = snake; + while (curr->next != NULL) { + prev = curr; + curr = curr->next; + } + free(curr); + if (prev != NULL) { + prev->next = NULL; + return snake; + } else { + return NULL; + } } -void free_snake(struct snake* sn){ +void free_snake(struct snake* sn) { + while (sn != NULL) { + struct snake* next = sn->next; + free(sn); + sn = next; + } } -int is_snake(struct snake* snake,int x,int y){ +int is_snake(struct snake* snake, int x, int y) { + while (snake != NULL) { + if (snake->x == x && snake->y == y) { + return 1; + } + snake = snake->next; + } return 0; } - int step_state(struct state* st){ int nx = (st->snake->x + st->sx); int ny = (st->snake->y + st->sy);