From be7fcb6486e189a739eed8bad040e808cd8917f5 Mon Sep 17 00:00:00 2001 From: Oleksandr Hryshchenko Date: Tue, 27 Apr 2021 17:30:00 +0000 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20'du7/snake.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du7/snake.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/du7/snake.c b/du7/snake.c index 73379aa..69a4c00 100644 --- a/du7/snake.c +++ b/du7/snake.c @@ -7,7 +7,7 @@ struct snake* add_snake(struct snake* snake,int x,int y){ newHead-> = y; newHead->next = snake; - struct snake* temp = snake; + /*struct snake* temp = snake; int myX, myY; while(temp->next != NULL){ @@ -20,8 +20,8 @@ struct snake* add_snake(struct snake* snake,int x,int y){ //temp->next = (struct snake*) calloc(1, sizeof(struct snake)); temp->next->x = temp->x - (myX - temp->x); - temp->next->y = temp->y - (myY - temp->y); - temp->next->next = NULL; + temp->next->y = temp->y - (myX - temp->y); + temp->next->next = NULL;*/ return newHead; } @@ -46,6 +46,8 @@ void free_snake(struct snake* sn){ sn = sn->next; temp->next = NULL; } + + free(sn); } int is_snake(struct snake* snake,int x,int y){ @@ -67,7 +69,7 @@ int step_state(struct state* st){ for(int i = 0; i < FOOD_COUNT; i++){ if(nx == st->foodx[i] && ny == st->foody[i]){ - add_snake(st->snake, st->foodx[i], st->foody[i]); + st->snake = add_snake(st->snake, st->foodx[i], st->foody[i]); st->foodx[i] = -1; st->foody[i] = -1; for(int j = 0; j < FOOD_COUNT; j++){ @@ -78,18 +80,18 @@ int step_state(struct state* st){ return END_FOOD; } } + return END_CONTINUE; } } - for(struct snake* temp = st->snake->next; temp != NULL; temp = temp->next){ - if(nx == temp->x && ny == temp->y){ + for(struct snake* temp = st->snake->next != NULL; temp = temp->next){ + if((nx == temp->x && ny == temp->y) || (nx < 0 || nx >= st->width || ny < 0 || ny >= st->height)){ return END_SNAKE; } } - add_snake(st->snake, nx, ny); + st->snake = add_snake(st->snake, nx, ny); remove_snake(st->snake); return END_CONTINUE; } -