diff --git a/a3/snake.c b/a3/snake.c index c90ec0e..513fccc 100644 --- a/a3/snake.c +++ b/a3/snake.c @@ -1,11 +1,11 @@ #include "snake.h" #include -// Pridá nový diel hada na začiatok (hlavu) +// Adds a new part to the snake at the given (x, y) position, making it the new head struct snake* add_snake(struct snake* snake, int x, int y){ struct snake* new_head = malloc(sizeof(struct snake)); if(!new_head){ - return NULL; + return NULL; } new_head->x = x; new_head->y = y; @@ -13,7 +13,7 @@ struct snake* add_snake(struct snake* snake, int x, int y){ return new_head; } -// Odstráni posledný diel hada (chvost) +// Removes the last part of the snake (tail) struct snake* remove_snake(struct snake* snake){ if(!snake || !snake->next){ free(snake); @@ -21,15 +21,15 @@ struct snake* remove_snake(struct snake* snake){ } struct snake* current = snake; - while (current->next && current->next->next){ - current = current->next; + while (current->next && current->next->next) { + current = current->next; // Move to the second last part } - free(current->next); - current->next = NULL; + free(current->next); // Free the last part (tail) + current->next = NULL; // Set the second last part's next pointer to NULL return snake; } -// Zistí, či sa na daných súradniciach nachádza časť hada +// Checks if there is a part of the snake at the given (x, y) coordinates int is_snake(struct snake* snake, int x, int y){ while(snake){ if(snake->x == x && snake->y == y){ @@ -40,37 +40,61 @@ int is_snake(struct snake* snake, int x, int y){ return 0; } -// Uvoľní pamäť všetkých častí hada +// Frees the memory allocated for all parts of the snake void free_snake(struct snake* sn){ while(sn){ struct snake* next = sn->next; free(sn); - sn = next; + sn = next; } } - // Spraví jeden krok hry – aktualizuje stav podľa pohybu +// Main function that updates the game state based on the snake's movement +// Returns the game status according to the reason to end the game (enum endgame) int step_state(struct state* state){ - int newx = state->snake->x + state->sx; - int newy = state->snake->y + state->sy; + if (!state || !state->snake) return END_USER; - if(newx < 0 || newx >= state->width || newy < 0 || newy >= state->height) + int new_x = state->snake->x + state->sx; + int new_y = state->snake->y + state->sy; + + // Check for collision with wall (end the game if collided) + if(new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height){ return END_WALL; + } - if(is_snake(state->snake, newx, newy)) + // Check for collision with itself (end the game if the snake runs into itself) + if(is_snake(state->snake, new_x, new_y)){ return END_SNAKE; + } - for(int i = 0; i < FOOD_COUNT; i++){ - if(state->foodx[i] == newx && state->foody[i] == newy){ + // Check if the snake ate any food + for(int i = 0; i < FOOD_COUNT; i++) { + if(state->foodx[i] == new_x && state->foody[i] == new_y){ + // Mark the food as eaten state->foodx[i] = -1; state->foody[i] = -1; - state->snake = add_snake(state->snake, newx, newy); - place_food(state); + // Add a new snake part at the food's location (new head) + state->snake = add_snake(state->snake, new_x, new_y); + + int food_placed = 0; + while(!food_placed){ + int food_x = rand() % (state->width - 2) + 1; + int food_y = rand() % (state->height - 2) + 1; + + if(!is_snake(state->snake, food_x, food_y)){ + state->foodx[i] = food_x; + state->foody[i] = food_y; + food_placed = 1; // Successfully placed food + } + } + return END_CONTINUE; } } - state->snake = add_snake(state->snake, newx, newy); + // Regular snake movement: move the snake by adding a new head and removing the tail + state->snake = add_snake(state->snake, new_x, new_y); state->snake = remove_snake(state->snake); - return END_CONTINUE; + + return END_CONTINUE; // Continue the game if no other conditions are met }