#include "snake.h" #include struct snake* add_snake(struct snake* snake,int x,int y){ struct snake* bod = malloc(sizeof(struct snake)); if (bod == NULL) return snake; bod->x = x; bod->y = y; bod->next = snake; return bod; } struct snake* remove_snake(struct snake* snake){ if (snake == NULL || snake->next == NULL){ free(snake); return NULL; } struct snake* z = snake; while (z->next->next != NULL){ z = z->next; } free(z->next); z->next = NULL; return snake; } void free_snake(struct snake* sn){ while (sn != NULL) { struct snake* tmp = sn; sn = sn->next; free(tmp); } } int is_snake(struct snake* snake,int x,int y){ while ( snake != NULL){ if (snake->x == x && snake->y == y) return 1; snake = snake->next; } return 0; } int step_state(struct state* st){ int nx = (st->snake->x + st->sx); int ny = (st->snake->y + st->sy); if (nx < 0 || nx >= st->width || ny < 0 || ny >= st->height) { return END_WALL; } // vlastna kolizia if (is_snake(st->snake, nx, ny)) { return END_SNAKE; } // skontroluje jedlo for (int i = 0; i < FOOD_COUNT; ++i) { if (st->foodx[i] == nx && st->foody[i] == ny) { // zje st->foodx[i] = -1; st->foody[i] = -1; // hlava st->snake = add_snake(st->snake, nx, ny); // skontroluje ci ostalo jedlo int food_left = 0; for (int j = 0; j < FOOD_COUNT; ++j) { if (st->foodx[j] >= 0 && st->foody[j] >= 0) { food_left = 1; break; } } return food_left ? END_CONTINUE : END_FOOD; } } // Regular move (no food) st->snake = add_snake(st->snake, nx, ny); st->snake = remove_snake(st->snake); return END_CONTINUE; }