git commit -m a4
git commit -m a4:
This commit is contained in:
Maksym Kovalchuk 2026-05-10 19:30:35 +00:00
parent 1f4d693fa2
commit 10cc177646
4 changed files with 211 additions and 137 deletions

View File

@ -1,5 +1,22 @@
all: CC = gcc
gcc -std=c11 -Wall -Wextra -Werror game.c -o game 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: clean:
rm -f game rm -f *.o $(TARGET)
re: clean all

View File

@ -1,138 +1,81 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "game.h" #include "game.h"
int error = 0; struct snake* add_snake(struct snake* snake, int x, int y) {
struct snake* h = calloc(1, sizeof(struct snake));
h->x = x;
void freeTree(Tree *node) { h->y = y;
if (!node) return; h->next = snake;
return h;
freeTree(node->yes);
freeTree(node->no);
free(node);
} }
Tree* readTree() { struct snake* remove_snake(struct snake* snake) {
char line[SIZE]; if (!snake) return NULL;
if (fgets(line, SIZE, stdin) == NULL) if (!snake->next) {
return NULL; free(snake);
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);
return NULL; return NULL;
} }
node->no = readTree(); struct snake* cur = snake;
if (!node->no) {
freeTree(node); while (cur->next->next)
return NULL; cur = cur->next;
free(cur->next);
cur->next = NULL;
return snake;
} }
return node; int is_snake(struct snake* snake, int x, int y) {
} while (snake) {
if (snake->x == x && snake->y == y)
int EmptyLine() {
char line[SIZE];
if (fgets(line, SIZE, stdin) == NULL)
return 1; return 1;
snake = snake->next;
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;
}
}
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; return 0;
} }
if (!EmptyLine()) { void free_snake(struct snake* sn) {
printf("Chybna databaza\n"); while (sn) {
freeTree(root); struct snake* n = sn->next;
return 0; free(sn);
sn = n;
}
} }
printf("Pozna %d druhov ovocia a zeleniny.\n", Leaves(root)); int step_state(struct state* s) {
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); int nx = s->snake->x + s->sx;
int ny = s->snake->y + s->sy;
start(root); if (nx < 0 || ny < 0 || nx >= s->width || ny >= s->height)
return END_WALL;
freeTree(root); if (is_snake(s->snake, nx, ny))
return 0; 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;
}

View File

@ -1,19 +1,37 @@
#ifndef GAME_H #ifndef GAME_H
#define GAME_H #define GAME_H
#define SIZE 256 #define FOOD_COUNT 3
typedef struct Tree { #define END_CONTINUE 0
char text[SIZE]; #define END_WALL 1
int isAnswer; #define END_SNAKE 2
struct Tree *yes; #define END_FOOD 3
struct Tree *no;
} Tree;
Tree* readTree(); struct snake {
int EmptyLine(); int x;
int Leaves(Tree *node); int y;
void freeTree(Tree *node); struct snake* next;
void start(Tree *node); };
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 #endif

96
du4/main.c Normal file
View File

@ -0,0 +1,96 @@
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
#ifdef _WIN32
#include <conio.h>
#else
#include <unistd.h>
#include <termios.h>
#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;
}