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 df14866..b81fa21 100644 Binary files a/a1/program.exe and b/a1/program.exe differ 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 64bf57d..31c6bf3 100644 Binary files a/du2/program.exe and b/du2/program.exe differ 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 0000000..49ce4f7 Binary files /dev/null and b/du5/snake.o differ