From 10cc17764672aba9df6937a4c972e840b3ab55f3 Mon Sep 17 00:00:00 2001 From: mk570rp Date: Sun, 10 May 2026 19:30:35 +0000 Subject: [PATCH] a4: git commit -m a4 git commit -m a4: --- du4/Makefile | 23 ++++++- du4/game.c | 187 ++++++++++++++++++--------------------------------- du4/game.h | 42 ++++++++---- du4/main.c | 96 ++++++++++++++++++++++++++ 4 files changed, 211 insertions(+), 137 deletions(-) create mode 100644 du4/main.c diff --git a/du4/Makefile b/du4/Makefile index e5141b2..0df6ec6 100644 --- a/du4/Makefile +++ b/du4/Makefile @@ -1,5 +1,22 @@ -all: - gcc -std=c11 -Wall -Wextra -Werror game.c -o game +CC = gcc +CFLAGS = -std=c11 -Wall -Wextra -Werror + +TARGET = game + +OBJS = game.o main.o + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CC) $(CFLAGS) $(OBJS) -o $(TARGET) + +game.o: game.c game.h + $(CC) $(CFLAGS) -c game.c + +main.o: main.c game.h + $(CC) $(CFLAGS) -c main.c clean: - rm -f game + rm -f *.o $(TARGET) + +re: clean all diff --git a/du4/game.c b/du4/game.c index ff16ee3..6665266 100644 --- a/du4/game.c +++ b/du4/game.c @@ -1,138 +1,81 @@ -#include #include -#include #include "game.h" -int error = 0; - - -void freeTree(Tree *node) { - if (!node) return; - - freeTree(node->yes); - freeTree(node->no); - free(node); +struct snake* add_snake(struct snake* snake, int x, int y) { + struct snake* h = calloc(1, sizeof(struct snake)); + h->x = x; + h->y = y; + h->next = snake; + return h; } -Tree* readTree() { - char line[SIZE]; +struct snake* remove_snake(struct snake* snake) { + if (!snake) return NULL; - if (fgets(line, SIZE, stdin) == NULL) - return NULL; - - line[strcspn(line, "\r\n")] = 0; - - if (line[0] == '\0') - return NULL; - - Tree *node = malloc(sizeof(Tree)); - node->yes = NULL; - node->no = NULL; - - if (line[0] == '*') { - node->isAnswer = 1; - strcpy(node->text, line + 1); - return node; - } - - node->isAnswer = 0; - strcpy(node->text, line); - - node->yes = readTree(); - if (!node->yes) { - freeTree(node); + if (!snake->next) { + free(snake); return NULL; } - node->no = readTree(); - if (!node->no) { - freeTree(node); - return NULL; - } + struct snake* cur = snake; - return node; + while (cur->next->next) + cur = cur->next; + + free(cur->next); + cur->next = NULL; + + return snake; } - - -int EmptyLine() { - char line[SIZE]; - - if (fgets(line, SIZE, stdin) == NULL) - return 1; - - return strcmp(line, "\n") == 0 || - strcmp(line, "\r\n") == 0; -} - -int Leaves(Tree *node) { - if (!node) return 0; - - if (node->isAnswer) - return 1; - - return Leaves(node->yes) + Leaves(node->no); -} - -void start(Tree *node) { - char input[SIZE]; - - while (node && !node->isAnswer) { - - printf("%s\n", node->text); - - if (fgets(input, SIZE, stdin) == NULL) { - printf("Koniec vstupu\n"); - return; - } - - int i = 0; - - while (input[i] == ' ' || input[i] == '\t') - i++; - - if (input[i] == 'a' || input[i] == 'A') { - node = node->yes; - } - else if (input[i] == 'n' || input[i] == 'N') { - node = node->no; - } - else { - printf("Nerozumiem\n"); - return; - } +int is_snake(struct snake* snake, int x, int y) { + while (snake) { + if (snake->x == x && snake->y == y) + return 1; + snake = snake->next; } - - if (node && node->isAnswer) { - printf("%s\n", node->text); - printf("Koniec\n"); - } -} - -int main() { - - printf("Expert z bufetu to vie.\n"); - - Tree *root = readTree(); - - if (!root || error) { - printf("Chybna databaza\n"); - freeTree(root); - return 0; - } - - if (!EmptyLine()) { - printf("Chybna databaza\n"); - freeTree(root); - return 0; - } - - printf("Pozna %d druhov ovocia a zeleniny.\n", Leaves(root)); - printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); - - start(root); - - freeTree(root); return 0; } +void free_snake(struct snake* sn) { + while (sn) { + struct snake* n = sn->next; + free(sn); + sn = n; + } +} + +int step_state(struct state* s) { + int nx = s->snake->x + s->sx; + int ny = s->snake->y + s->sy; + + if (nx < 0 || ny < 0 || nx >= s->width || ny >= s->height) + return END_WALL; + + if (is_snake(s->snake, nx, ny)) + return END_SNAKE; + + int ate = 0; + + for (int i = 0; i < FOOD_COUNT; i++) { + if (s->foodx[i] == nx && s->foody[i] == ny) { + s->foodx[i] = -1; + s->foody[i] = -1; + ate = 1; + break; + } + } + + s->snake = add_snake(s->snake, nx, ny); + + if (!ate) { + s->snake = remove_snake(s->snake); + } + + for (int i = 0; i < FOOD_COUNT; i++) { + if (s->foodx[i] >= 0) + return END_CONTINUE; + } + + return END_FOOD; +} diff --git a/du4/game.h b/du4/game.h index 3a0cb56..e0b1726 100644 --- a/du4/game.h +++ b/du4/game.h @@ -1,19 +1,37 @@ #ifndef GAME_H #define GAME_H -#define SIZE 256 +#define FOOD_COUNT 3 -typedef struct Tree { - char text[SIZE]; - int isAnswer; - struct Tree *yes; - struct Tree *no; -} Tree; +#define END_CONTINUE 0 +#define END_WALL 1 +#define END_SNAKE 2 +#define END_FOOD 3 -Tree* readTree(); -int EmptyLine(); -int Leaves(Tree *node); -void freeTree(Tree *node); -void start(Tree *node); +struct snake { + int x; + int y; + struct snake* next; +}; + +struct state { + int width; + int height; + + struct snake* snake; + + int sx; + int sy; + + int foodx[FOOD_COUNT]; + int foody[FOOD_COUNT]; +}; + +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); #endif diff --git a/du4/main.c b/du4/main.c new file mode 100644 index 0000000..b18dc63 --- /dev/null +++ b/du4/main.c @@ -0,0 +1,96 @@ +#include +#include +#include "game.h" + +#ifdef _WIN32 +#include +#else +#include +#include +#endif + +#ifndef _WIN32 +int getch() { + struct termios oldt, newt; + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + + newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + + int c = getchar(); + + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + return c; +} +#endif + +void draw(struct state* s) { + system("clear"); + + for (int y = 0; y < s->height; y++) { + for (int x = 0; x < s->width; x++) { + + if (is_snake(s->snake, x, y)) { + printf("O"); + } + else { + int food = 0; + + for (int i = 0; i < FOOD_COUNT; i++) { + if (s->foodx[i] == x && s->foody[i] == y) { + printf("*"); + food = 1; + } + } + + if (!food) printf("."); + } + } + printf("\n"); + } +} + +void set_dir(struct state* s, char c) { + if (c == 'w') { s->sx = 0; s->sy = -1; } + if (c == 's') { s->sx = 0; s->sy = 1; } + if (c == 'a') { s->sx = -1; s->sy = 0; } + if (c == 'd') { s->sx = 1; s->sy = 0; } +} + +int main() { + struct state s; + + s.width = 20; + s.height = 10; + + s.snake = add_snake(NULL, 5, 5); + + s.sx = 1; + s.sy = 0; + + for (int i = 0; i < FOOD_COUNT; i++) { + s.foodx[i] = rand() % s.width; + s.foody[i] = rand() % s.height; + } + + int result = END_CONTINUE; + + while (result == END_CONTINUE) { + draw(&s); + + char c = getch(); + set_dir(&s, c); + + result = step_state(&s); + } + + draw(&s); + + if (result == END_WALL) printf("Game Over: wall\n"); + if (result == END_SNAKE) printf("Game Over: self\n"); + if (result == END_FOOD) printf("You Win!\n"); + + free_snake(s.snake); + return 0; +}