From fe913763f631d2816cc0c80e95fbcb11e2fcaf26 Mon Sep 17 00:00:00 2001 From: kr820js Date: Sat, 13 Apr 2024 19:12:56 +0200 Subject: [PATCH] submit1 --- cv9/snake.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 cv9/snake.c diff --git a/cv9/snake.c b/cv9/snake.c new file mode 100644 index 0000000..68e2e35 --- /dev/null +++ b/cv9/snake.c @@ -0,0 +1,60 @@ +#include "snake.h" +#include + +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){ + if(snake==NULL){ + return NULL; + } + struct snake* curent_element = snake; + while(curent_element->next != NULL){ + curent_element = curent_element->next; + } + + free(curent_element); + + if(curent_element==snake){ + return NULL; + } + + return snake; +} + +void free_snake(struct snake* sn){ + struct snake* current=sn; + if(current==NULL){ + return; + } + struct snake* next_element; + while(current->next!=NULL){ + next_element=current->next; + free(current); + current=next_element; + } + free(current); +} + +int is_snake(struct snake* snake,int x,int y){ + struct snake* current =snake; + while(current!=NULL){ + if(x==current->x&&y==current->y){ + return 1; + } + current=current->next; + } + 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; +}