From 8e5e2a2dae356b38034d68d17c342e4d31eb3cbc Mon Sep 17 00:00:00 2001 From: ov075wu Date: Tue, 8 Apr 2025 19:19:05 +0200 Subject: [PATCH] refresh --- Makefile | 2 +- a1/program.c | 4 +- a1/program.exe | Bin 89932 -> 89932 bytes du1/program.c | 6 +-- du2/program.c | 14 +++--- du2/program.exe | Bin 90141 -> 90141 bytes du5/snake.c | 43 +++++++++++++++++++ du5/snake.h | 111 ++++++++++++++++++++++++++++++++++++++++++++++++ du5/snake.o | Bin 0 -> 747 bytes 9 files changed, 167 insertions(+), 13 deletions(-) create mode 100644 du5/snake.c create mode 100644 du5/snake.h create mode 100644 du5/snake.o diff --git a/Makefile b/Makefile index b0c271a..8e61469 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc CFLAGS = -Wall -Wextra -std=c99 -dIRS = a1 du1 du2 du3 du4 +dIRS = a1 du1 du2 du3 du4 du5 all: @for %%d in ($(dIRS)) do \ $(CC) $(CFLAGS) %%d\program.c -o %%d\program.exe diff --git a/a1/program.c b/a1/program.c index ac1d84d..7ef0d5f 100644 --- a/a1/program.c +++ b/a1/program.c @@ -13,8 +13,8 @@ void funkcia (int c, int *line_count) { } int main() { - int c; //текущий считанный символ - int c2 = 0; //счётчик строк + int c; + int c2 = 0; while ((c = getchar()) !=EOF) { funkcia(c, &c2); } diff --git a/a1/program.exe b/a1/program.exe index df14866d852502b1db5078f79846cbe8ef3039d2..b81fa214d0915f6c3a5ed964f50dd3320366cce8 100644 GIT binary patch delta 25 hcmX@JkM+zx)(IWVA!>IfcKb42Jl1$?>n+BjH~^nz3{3z4 delta 25 hcmX@JkM+zx)(IWV_aEP$*zL;{)6#fr>n+BjH~_5e4BY?# diff --git a/du1/program.c b/du1/program.c index 5661b32..627aeb5 100644 --- a/du1/program.c +++ b/du1/program.c @@ -11,15 +11,15 @@ int main() { break; } results[count] = num; - count++; //нумерация чисел + count++; } - if (count == 0) { //если не было введено никакое число + if (count == 0) { printf("Chyba: Malo platnych hodnot.\n"); return 0; } - for (int i = 0; i < count; i++) { //выписывание всех чисел + for (int i = 0; i < count; i++) { printf("Sutaziaci c. %d vypil %d poharov.\n",i+1,results[i]); } diff --git a/du2/program.c b/du2/program.c index 06952af..d4faeac 100644 --- a/du2/program.c +++ b/du2/program.c @@ -3,17 +3,17 @@ int read_number (double *num) { char buffer[50]; - if (!fgets(buffer, sizeof(buffer), stdin)) { //если fgets не смог считать строку + if (!fgets(buffer, sizeof(buffer), stdin)) { return 0; } - if (sscanf(buffer, "%lf", num) != 1) { //переделываем в double. Если всё успешно + if (sscanf(buffer, "%lf", num) != 1) { return 0; } return 1; } -double hornerovaschema (double x, double cofecients[], int count) { //вычесляет значение многочлена в точке x +double hornerovaschema (double x, double cofecients[], int count) { double result = 0.0; for (int i = 0; i < count; i++) { result = result * x + cofecients[i]; @@ -22,19 +22,19 @@ double hornerovaschema (double x, double cofecients[], int count) { //вычес } int main() { - double x; //хранит точку, в которой вычисляется многочлен. + double x; double cof[maxcoe]; int count = 0; - if (!read_number(&x)) { //если не может считать + if (!read_number(&x)) { printf("Nepodarilo sa nacitat zaklad x\n"); return 0; } while (count < maxcoe) { - double coef; //переменна для хранения считанного коэффициента - char buffer[50];//строковый буфер для fgetc + double coef; + char buffer[50]; if (!fgets(buffer, sizeof(buffer), stdin) || buffer[0] == '\n') { break; diff --git a/du2/program.exe b/du2/program.exe index 64bf57d0cecdb3f7a168f01c06828e306a46c5a2..31c6bf3d61a67a59e1d5140179e3e4ea553d1b14 100644 GIT binary patch delta 25 hcmbPxfOYNx)(IWVTQ%=Y?Dl24Uf+0Y>n%p-cmSIG3+Mm< delta 25 hcmbPxfOYNx)(IWVE^W6acKb4=+-bbE^%kRZJOG#=3%mdT diff --git a/du5/snake.c b/du5/snake.c new file mode 100644 index 0000000..b5f4018 --- /dev/null +++ b/du5/snake.c @@ -0,0 +1,43 @@ +#include +#include "snake.h" +#include + +struct snake* add_snake (struct snake* snake, int x, int y) { + struct snake* head = NULL; + + head = malloc(sizeof(struct snake)); + + if (!head) { + return snake; + } + head->x = x; + head->y = y; + + head->next = NULL; + + if (snake !=NULL) { + head->next = snake; + } + return head; +} + +struct snake* remove_snake(struct snake* snake) { + if(snake == NULL || snake-> next == NULL) { + free(snake); + return NULL; + } + + struct snake* previous = NULL; + struct snake* current = NULL; + while (current->next !=NULL) { + previous = current; + current = current->next; + } + + if (previous !=NULL) { + free(previous->next); + previous->next = NULL; + } +} + + diff --git a/du5/snake.h b/du5/snake.h new file mode 100644 index 0000000..df3a11b --- /dev/null +++ b/du5/snake.h @@ -0,0 +1,111 @@ +#ifndef snake_h_INCLUDED +#define snake_h_INCLUDED + +// Number of food items on the plane +#define FOOD_COUNT 5 + +/** + * One part of the snake; + * + * The snake is a linked list; + */ +struct snake { + // x position of the snake part + int x; + // y position of the snake part + int y; + // Pointer to the next snake part. + // The last part of the snake has NULL pointer to the next part. + struct snake* next; +}; + +// End game reason constants, return value of step_state +enum endgame { + // Continue the game + END_CONTINUE = 0, + // Snake hit a wall + END_WALL, + // Snake hit itself + END_SNAKE, + // No food left + END_FOOD, + // Other reason to end + END_USER +}; + +/** + * State of the game. + * + * The state consists of the snake, its speed and food on the plane. + * + * The snake is a linked list of snake parts. + * + * Speed vector is a vector added to the last head position to create a new head. + * + * Food are points on the plane. Food with negative coordinates means food is already eaten. + */ +struct state { + // Snake as a linked list + struct snake* snake; + // X of the food positions + int foodx[FOOD_COUNT]; + // Y of the food positions + int foody[FOOD_COUNT]; + int sx; + int sy; + int width; + int height; +}; + +/** + * Add a new snake part with given position. The new snake part becomes the new head. + * + * @param head of the snake. + * @param x coordinate of the new head; + * @param y coordinate of the new head. + * @return new head of the snake. + */ +struct snake* add_snake(struct snake* snake, int x, int y); + +/** + * Remove the last snake part. + * The last snake part should always have NULL next pointer. + * + * @param head of the snake. + * @return new head of the snake. + */ +struct snake* remove_snake(struct snake* snake); + +/** + * Finds out if given coordinates are part of the snake. + * @param snake + * @param x coordinate to search in snake + * @param y coordinate to search in snake + * @return True, if there is a snake part with coordinates x,y. False otherwise + */ +int is_snake(struct snake* snake, int x, int y); + +/** + * Remove and free each snake part; + * @param head of the snake. + */ +void free_snake(struct snake* sn); + +/** + * Change game state. + * + * The function should calculate new position of the snake head + * from the current position and speed vector. + * Then it should modify snake parts or food coordinates according to the rules: + * + * - If the new position is on the snake, end the game, return END_SNAKE. + * - If the new position is on the food, mark food as eaten + * (set its coordinates to -1) and add new snake part on the position of the food. If there is no food left, return END_FOOD. Else return END_CONTINUE. + * - If the new position is on the plane, add new snake part on the new position and remove the last part of the snake, return END_CONTINUE. + * + * @param current state of the game + * @return reason to end the game according to enum endgame. + */ +int step_state(struct state* state); + +#endif // snake_h_INCLUDED \ No newline at end of file diff --git a/du5/snake.o b/du5/snake.o new file mode 100644 index 0000000000000000000000000000000000000000..49ce4f704a978e7e44dfb391e2781c0445b03aca GIT binary patch literal 747 zcmZ{i&r1SP5Xa~3uCRfb2RlR^7V=;RpGp?h$&$rOfe4njZOhdpe}HZfOF|$)ix3`@N1a;hVHS=6UB?o5a`({3G_r0AxWC0+1WB^M-DU93JwJr20lrX)a50D|F zq(SnOmrdIgmH8n9$tQl+>C7iaom=D*>1is8%e4Q_aR;B5Nu;*MOObalmXLe!SxUqe z-S^&CY^Zxg4V7pR(+6o+_j+9qt^$2s{fzf@uU`zhVUfdbUO_R;s08-9D%cC6p}Kh$ z#q0B6kjrNGwAfLtnLFXFc2&c5e4B6bWIVN@2>}qH?P$y?0J~AyYEV(`@nCCMBUjLm z(P2NN-bn|G0jcnU2rOeMgdPD6BT*6Hm0I0GMfUvzP-UH_d1>+UQRy$P_=@3Pi9W)N zP0p3tGNThrgOf9r1T*sxTu#nYp2N^Cnjab6LT2ki&xV9wpubRNG|B1n7!9*tZ;fw? g_kvwyc$