From 907f8ccee9fad24157e407a753b747eac838119f Mon Sep 17 00:00:00 2001 From: Nazar Sendetskyi Date: Tue, 14 Jun 2022 07:59:12 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20'du7?= =?UTF-8?q?'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du7/snake.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 du7/snake.c diff --git a/du7/snake.c b/du7/snake.c new file mode 100644 index 0000000..25fcdb5 --- /dev/null +++ b/du7/snake.c @@ -0,0 +1,133 @@ +#include "snake.h" +#include +struct snake* add_snake(struct snake* snake,int x,int y){ + struct snake* first_element = calloc(1,sizeof(struct snake)); + first_element->x = x; + first_element->y = y; + + first_element->next = snake; + return first_element; +} +int is_snake(struct snake* snake,int x,int y){ + if(snake == NULL) + return 0; + struct snake* step_snake = snake; + + while (step_snake->next != NULL) + { + if (step_snake->x == x && step_snake->y == y) + { + return 1; + } + step_snake = step_snake->next; + } + + if (step_snake->x == x && step_snake->y == y) + { + return 1; + } + return 0; +} + +int step_state(struct state* st){ + struct snake* main_element = st->snake; + //hlavu + int root_x = st->snake->x; + int nx = (st->snake->x + st->sx); + int root_y = st->snake->y; + int ny = (st->snake->y + st->sy); + // pohybovať hlavou + st->snake->y = ny; + st->snake->x = nx; + + st->snake = st->snake->next; + while (st->snake->next != NULL) + { + //zmeniť na hodnotu ďalšieho prvku + int step_prev_x = st->snake->x; + int step_prev_y = st->snake->y; + + st->snake->x = root_x; + st->snake->y = root_y; + + root_x = step_prev_x; + root_y = step_prev_y; + + st->snake = st->snake->next; + } + //zmeniť hodnotu posledného prvku + st->snake->x = root_x; + st->snake->y = root_y; + + st->snake = main_element; + + root_x = main_element->x; + root_y = main_element->y; + if (main_element->next != NULL){ + st->snake = main_element->next; + } + while (st->snake->next != NULL) + { + if (st->snake->x == root_x && st->snake->y == root_y) + { + free_snake(st->snake); + free(main_element);//voľne alokované mamory (Pamätaj) + return END_SNAKE; + } + st->snake = st->snake->next; + } + if (st->snake->x == root_x && st->snake->y == root_y) + { + free_snake(st->snake); + free(main_element); + + return END_SNAKE; + } + st->snake = main_element; +int i = 0; +while (i < FOOD_COUNT) +{ + if (st->foodx[i] == st->snake->x && st->foody[i] == st->snake->y) + { + st->snake = add_snake(st->snake, st->foodx[i], st->foody[i]); + //koordinovať -1 + st->foody[i] = -1; + st->foodx[i] = -1; + main_element = st->snake; + } + i++; +} +int k =0; +while(k < FOOD_COUNT) +{ + if (st->foodx[k] != -1 && st->foody[k] != -1) + return END_CONTINUE; + k++; +} + + free(main_element); + return END_FOOD; +} + + +void free_snake(struct snake* sn){ + if(sn != NULL){ + free(sn); + } +} +struct snake* remove_snake(struct snake* snake){ + if(snake == NULL|| snake->next == NULL) + return NULL; + + struct snake* main_element = snake; + struct snake* step_snake = snake; + //cheak + while (step_snake->next->next != NULL) + { + step_snake = step_snake->next; + } + step_snake->next = NULL; + step_snake = main_element; +return step_snake; +} +