Aktualizovat a3/main.c

This commit is contained in:
Mykola Syniavskyi 2025-05-02 10:08:22 +00:00
parent 94c853856c
commit 8b629c36db

View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include "snake.h"
#include "world.h"
int main() {
struct state* st=create_world(10, 10);
while (1) {
for (int y = 0; y<st->height; y++) {
for (int x = 0; x<st->width; x++) {
if (is_snake(st->snake, x, y)) {
printf("O");
} else {
int food = 0;
for (int i = 0; i < FOOD_COUNT; i++) {
if (st->foodx[i] == x && st->foody[i] == y) {
food = 1;
break;
}
}
printf(food ? "*" : ".");
}
}
printf("\n");
}
printf("WASD: ");
char c;
scanf(" %c", &c);
if (c == 'w') change_direction(st, 0, -1);
else if (c == 's') change_direction(st, 0, 1);
else if (c == 'a') change_direction(st, -1, 0);
else if (c == 'd') change_direction(st, 1, 0);
else if (c == 'q') break;
int res = step_state(st);
if (res != END_CONTINUE) {
printf("End of game: %d\n", res);
break;
}
}
free_world(st);
return 0;
}