From ac447157554906df8e6161d9acf91c25feb75905 Mon Sep 17 00:00:00 2001 From: vafan Date: Thu, 16 Apr 2026 20:36:48 +0000 Subject: [PATCH] new --- du4/README.md | 1 + du4/snake.c | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 du4/README.md create mode 100644 du4/snake.c diff --git a/du4/README.md b/du4/README.md new file mode 100644 index 0000000..9aa2078 --- /dev/null +++ b/du4/README.md @@ -0,0 +1 @@ +Na túto úlohu som použil copilot a Claude Haiku 4. \ No newline at end of file diff --git a/du4/snake.c b/du4/snake.c new file mode 100644 index 0000000..7669e8d --- /dev/null +++ b/du4/snake.c @@ -0,0 +1,219 @@ +#include +#include +#include +#include + +#define FOOD_COUNT 5 +#define WIDTH 20 +#define HEIGHT 10 + +struct snake { + int x; + int y; + struct snake* next; +}; + +enum endgame { + END_CONTINUE = 0, + END_WALL, + END_SNAKE, + END_FOOD, + END_USER +}; + +struct state { + struct snake* snake; + int foodx[FOOD_COUNT]; + int foody[FOOD_COUNT]; + int sx; + int sy; + int width; + int height; +}; + +struct snake* add_snake(struct snake* snake, int x, int y); +struct snake* remove_snake(struct snake* snake); +int is_snake(struct snake* snake, int x, int y); +void free_snake(struct snake* sn); +int step_state(struct state* state); + +void display_game(struct state* state); +void start_game(struct state* game); + +struct snake* add_snake(struct snake* snake, int x, int y) { + struct snake* new_part = (struct snake*)malloc(sizeof(struct snake)); + new_part->x = x; + new_part->y = y; + new_part->next = snake; + return new_part; +} + +struct snake* remove_snake(struct snake* snake) { + if (snake == NULL) { + return NULL; + } + + if (snake->next == NULL) { + free(snake); + return NULL; + } + + struct snake* current = snake; + while (current->next != NULL && current->next->next != NULL) { + current = current->next; + } + + if (current->next != NULL) { + free(current->next); + current->next = NULL; + } + + return snake; +} + +int is_snake(struct snake* snake, int x, int y) { + struct snake* current = snake; + while (current != NULL) { + if (current->x == x && current->y == y) { + return 1; + } + current = current->next; + } + return 0; +} + +void free_snake(struct snake* sn) { + while (sn != NULL) { + struct snake* temp = sn; + sn = sn->next; + free(temp); + } +} + +int step_state(struct state* state) { + int newX = state->snake->x + state->sx; + int newY = state->snake->y + state->sy; + + if (newX < 0 || newX >= state->width || newY < 0 || newY >= state->height) { + return END_WALL; + } + + if (is_snake(state->snake->next, newX, newY)) { + return END_SNAKE; + } + + int foodIdx = -1; + for (int i = 0; i < FOOD_COUNT; i++) { + if (state->foodx[i] == newX && state->foody[i] == newY) { + foodIdx = i; + break; + } + } + + if (foodIdx != -1) { + state->foodx[foodIdx] = -1; + state->foody[foodIdx] = -1; + + state->snake = add_snake(state->snake, newX, newY); + + int foodLeft = 0; + for (int i = 0; i < FOOD_COUNT; i++) { + if (state->foodx[i] >= 0) { + foodLeft = 1; + break; + } + } + + return foodLeft ? END_CONTINUE : END_FOOD; + } + + state->snake = add_snake(state->snake, newX, newY); + state->snake = remove_snake(state->snake); + + return END_CONTINUE; +} + +void display_game(struct state* game) { + system("clear"); + printf("SNAKE GAME - Simple Version\n"); + printf("================================\n\n"); + + for (int x = 0; x < WIDTH + 2; x++) printf("#"); + printf("\n"); + + for (int y = 0; y < HEIGHT; y++) { + printf("#"); + for (int x = 0; x < WIDTH; x++) { + int head = (game->snake->x == x && game->snake->y == y); + int body = is_snake(game->snake->next, x, y); + int food = 0; + + for (int i = 0; i < FOOD_COUNT; i++) { + if (game->foodx[i] == x && game->foody[i] == y) { + food = 1; + break; + } + } + + if (head) printf("S"); + else if (body) printf("+"); + else if (food) printf("F"); + else printf(" "); + } + printf("#\n"); + } + + for (int x = 0; x < WIDTH + 2; x++) printf("#"); + printf("\n"); +} + +void start_game(struct state* game) { + game->snake = add_snake(NULL, WIDTH / 2, HEIGHT / 2); + game->snake = add_snake(game->snake, WIDTH / 2 - 1, HEIGHT / 2); + + game->sx = 1; + game->sy = 0; + game->width = WIDTH; + game->height = HEIGHT; + + srand(time(NULL)); + for (int i = 0; i < FOOD_COUNT; i++) { + game->foodx[i] = rand() % WIDTH; + game->foody[i] = rand() % HEIGHT; + } +} + +int main(void) { + struct state game; + start_game(&game); + + int playing = 1; + int tick = 0; + + while (playing) { + display_game(&game); + + tick++; + if (tick >= 3) { + tick = 0; + int status = step_state(&game); + + if (status == END_WALL) { + printf("CRASH into wall!\n"); + playing = 0; + } else if (status == END_SNAKE) { + printf("CRASH into yourself!\n"); + playing = 0; + } else if (status == END_FOOD) { + printf("You ate all food. Good job!\n"); + playing = 0; + } + } + + usleep(80000); + } + + free_snake(game.snake); + + return 0; +}