This commit is contained in:
Denis Landa 2025-04-11 10:27:40 +02:00
parent c4a07b8879
commit 7511cf5d88

View File

@ -1,8 +1,10 @@
#include <stdlib.h> #include <stdlib.h>
#include "snake.h" #include "snake.h"
struct snake* add_snake(struct snake* snake, int x, int y){ struct snake* add_snake(struct snake* snake, int x, int y){
struct snake* nova_cast = (struct snake*) calloc(1, sizeof(struct snake)); struct snake* nova_cast = (struct snake*) calloc(1, sizeof(struct snake));
if (nova_cast == NULL){ if (nova_cast == NULL){
return snake; return snake;
} }
@ -38,7 +40,6 @@ struct snake* remove_snake(struct snake* snake){
return snake; return snake;
} }
int is_snake(struct snake* snake, int x, int y){ int is_snake(struct snake* snake, int x, int y){
struct snake* aktualny = snake; struct snake* aktualny = snake;
@ -46,7 +47,6 @@ int is_snake(struct snake* snake, int x, int y){
if (aktualny->x == x && aktualny->y == y){ if (aktualny->x == x && aktualny->y == y){
return 1; return 1;
} }
aktualny = aktualny->next; aktualny = aktualny->next;
} }
@ -64,31 +64,31 @@ void free_snake(struct snake* sn){
} }
int step_state(struct state* state){ int step_state(struct state* state){
int nova_pozicia_x = state->snake->x + state->sx; int nova_x = state->snake->x + state->sx;
int nova_pozicia_y = state->snake->y + state->sy; int nova_y = state->snake->y + state->sy;
if (nova_pozicia_x < 0 || nova_pozicia_y < 0 || nova_pozicia_x >= state->width || nova_pozicia_y >= state->height){ if (nova_x < 0 || nova_y < 0 || nova_x >= state->width || nova_y >= state->height){
return END_WALL; return END_WALL;
} }
if (is_snake(state->snake, nova_pozicia_x, nova_pozicia_y)){ if (is_snake(state->snake, nova_x, nova_y)){
return END_SNAKE; return END_SNAKE;
} }
for (int i = 0; i < FOOD_COUNT; i++){ for (int i = 0; i < FOOD_COUNT; i++){
if (state->foodx[i] == nova_pozicia_x && state->foody[i] == nova_pozicia_y){ if (state->foodx[i] == nova_x && state->foody[i] == nova_y){
state->snake = add_snake(state->snake, nova_pozicia_x, nova_pozicia_y); state->snake = add_snake(state->snake, nova_x, nova_y);
state->foodx[i] = -1; state->foodx[i] = -1;
state->foody[i] = -1; state->foody[i] = -1;
int este_jedlo = 0; int jedlo_ostalo = 0;
for (int j = 0; j < FOOD_COUNT; j++){ for (int j = 0; j < FOOD_COUNT; j++){
if (state->foodx[j] != -1 && state->foody[j] != -1){ if (state->foodx[j] != -1 && state->foody[j] != -1){
este_jedlo = 1; jedlo_ostalo = 1;
} }
} }
if (este_jedlo == 0){ if (jedlo_ostalo == 0){
return END_FOOD; return END_FOOD;
} }
@ -96,7 +96,7 @@ int step_state(struct state* state){
} }
} }
state->snake = add_snake(state->snake, nova_pozicia_x, nova_pozicia_y); state->snake = add_snake(state->snake, nova_x, nova_y);
state->snake = remove_snake(state->snake); state->snake = remove_snake(state->snake);
return END_CONTINUE; return END_CONTINUE;