This commit is contained in:
Oleksandr Vyshniakov 2025-04-10 22:47:43 +02:00
parent 9391f1ab50
commit 3ad4d92adc
2 changed files with 31 additions and 26 deletions

View File

@ -65,40 +65,45 @@ int is_snake(struct snake* snake, int x, int y) {
}
int step_state(struct state* state) {
int new_x = state->snake->x + state->sx;
int new_y = state->snake->y + state->sy;
int x = state->snake->x + state->sx;
int 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 (x < 0) return END_WALL;
if (x >= state->width) return END_WALL;
if (y < 0) return END_WALL;
if (y >= state->height) return END_WALL;
if (is_snake(state->snake, new_x, new_y)) {
if (is_snake(state->snake, x, 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;
}
int food_found = -1;
for (int idx = 0; idx < FOOD_COUNT; idx++) {
if (state->foodx[idx] == x && state->foody[idx] == y) {
food_found = idx;
break;
}
}
state->snake = add_snake(state->snake, new_x, new_y);
if (food_found != -1) {
state->foodx[food_found] = -1;
state->foody[food_found] = -1;
state->snake = add_snake(state->snake, x, y);
int remaining = 0;
for (int k = 0; k < FOOD_COUNT; ++k) {
remaining += (state->foodx[k] >= 0 && state->foody[k] >= 0);
}
if (remaining == 0) {
return END_FOOD;
}
return END_CONTINUE;
}
state->snake = add_snake(state->snake, x, y);
state->snake = remove_snake(state->snake);
return END_CONTINUE;
}

Binary file not shown.