#include #include #include #include "world.h" #include "snake.h" static void place_food(struct state* s) { int placed = 0; while (placed < FOOD_COUNT) { int fx = rand() % s->width; int fy = rand() % s->height; if (!is_snake(s->snake, fx, fy)) { s->foodx[placed] = fx; s->foody[placed] = fy; placed++; } } } void setup_world(struct state* s, int w, int h) { if (!s) return; srand((unsigned int)time(NULL)); s->width = w; s->height = h; s->sx = 1; s->sy = 0; s->snake = NULL; int start_x = w / 2; int start_y = h / 2; s->snake = add_snake(s->snake, start_x, start_y); place_food(s); } void draw_world(const struct state* s) { if (!s) return; system("clear"); // или "cls" на Windows for (int row = 0; row < s->height; row++) { for (int col = 0; col < s->width; col++) { int drawn = 0; if (is_snake(s->snake, col, row)) { putchar('O'); drawn = 1; } else { for (int i = 0; i < FOOD_COUNT; ++i) { if (s->foodx[i] == col && s->foody[i] == row) { putchar('*'); drawn = 1; break; } } } if (!drawn) { putchar('.'); } } putchar('\n'); } } char read_input(void) { char key = 0; printf("Zadaj smer (WASD): "); scanf(" %c", &key); return key; } void change_direction(struct state* s, char key) { if (!s) return; switch (key) { case 'w': case 'W': s->sx = 0; s->sy = -1; break; case 's': case 'S': s->sx = 0; s->sy = 1; break; case 'a': case 'A': s->sx = -1; s->sy = 0; break; case 'd': case 'D': s->sx = 1; s->sy = 0; break; default: // ничего не делаем при некорректном вводе break; } }