diff --git a/du5/snake.c b/du5/snake.c index a725652..9966ef6 100644 --- a/du5/snake.c +++ b/du5/snake.c @@ -1,8 +1,10 @@ #include #include "snake.h" + struct snake* add_snake(struct snake* snake, int x, int y){ struct snake* nova_cast = (struct snake*) calloc(1, sizeof(struct snake)); + if (nova_cast == NULL){ return snake; } @@ -38,7 +40,6 @@ struct snake* remove_snake(struct snake* snake){ return snake; } - int is_snake(struct snake* snake, int x, int y){ struct snake* aktualny = snake; @@ -46,7 +47,6 @@ int is_snake(struct snake* snake, int x, int y){ if (aktualny->x == x && aktualny->y == y){ return 1; } - aktualny = aktualny->next; } @@ -64,31 +64,31 @@ void free_snake(struct snake* sn){ } int step_state(struct state* state){ - int nova_pozicia_x = state->snake->x + state->sx; - int nova_pozicia_y = state->snake->y + state->sy; + int nova_x = state->snake->x + state->sx; + int nova_y = state->snake->y + state->sy; - if (nova_pozicia_x < 0 || nova_pozicia_y < 0 || nova_pozicia_x >= state->width || nova_pozicia_y >= state->height){ + if (nova_x < 0 || nova_y < 0 || nova_x >= state->width || nova_y >= state->height){ return END_WALL; } - if (is_snake(state->snake, nova_pozicia_x, nova_pozicia_y)){ + if (is_snake(state->snake, nova_x, nova_y)){ return END_SNAKE; } for (int i = 0; i < FOOD_COUNT; i++){ - if (state->foodx[i] == nova_pozicia_x && state->foody[i] == nova_pozicia_y){ - state->snake = add_snake(state->snake, nova_pozicia_x, nova_pozicia_y); + if (state->foodx[i] == nova_x && state->foody[i] == nova_y){ + state->snake = add_snake(state->snake, nova_x, nova_y); state->foodx[i] = -1; state->foody[i] = -1; - int este_jedlo = 0; + int jedlo_ostalo = 0; for (int j = 0; j < FOOD_COUNT; j++){ if (state->foodx[j] != -1 && state->foody[j] != -1){ - este_jedlo = 1; + jedlo_ostalo = 1; } } - if (este_jedlo == 0){ + if (jedlo_ostalo == 0){ return END_FOOD; } @@ -96,7 +96,7 @@ int step_state(struct state* state){ } } - state->snake = add_snake(state->snake, nova_pozicia_x, nova_pozicia_y); + state->snake = add_snake(state->snake, nova_x, nova_y); state->snake = remove_snake(state->snake); return END_CONTINUE;