This commit is contained in:
Oleksandr Vyshniakov 2025-04-09 22:30:50 +02:00
parent c4b92debdf
commit f98b699006
3 changed files with 48 additions and 0 deletions

View File

@ -42,6 +42,16 @@ struct snake* remove_snake(struct snake* snake) {
return snake; return snake;
} }
void free_snake(struct snake* sn) {
struct snake* walker = sn;
struct snake* next_part = NULL;
while (walker !=NULL) {
next_part = walker->next;
free(walker);
walker = next_part;
}
}
int is_snake(struct snake* snake, int x, int y) { int is_snake(struct snake* snake, int x, int y) {
struct snake* current = snake; struct snake* current = snake;
@ -54,4 +64,42 @@ int is_snake(struct snake* snake, int x, int y) {
return 0; return 0;
} }
int step_state(struct state* state) {
int new_x = state->snake->x + state->sx;
int new_y = state->snake->y + state->sy;
if (new_x < 0 || new_x >= state-> width || new_y < 0 || new_y >= state->height) {
return END_WALL;
}
if (is_snake(state->snake, new_x, new_y)) {
return END_SNAKE;
}
for (int i = 0; i < FOOD_COUNT; i++) {
if (state->foodx[i] == new_x && state->foody[i] == new_y) {
state->foodx[i] = -1;
state->foody[i] = -1;
state->snake = add_snake(state->snake, new_x, new_y);
int any_food_left = 0;
for (int j = 0; j < FOOD_COUNT; j++) {
if (state->foodx[j] >= 0 && state->foody[j] >= 0) {
any_food_left = 1;
break;
}
}
if (any_food_left) {
return END_CONTINUE;
} else {
return END_FOOD;
}
}
}
state->snake = add_snake(state->snake, new_x, new_y);
state->snake = remove_snake(state->snake);
return END_CONTINUE;
}

BIN
du5/snake.exe Normal file

Binary file not shown.

Binary file not shown.