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; } -