diff --git a/du5/snake.c b/du5/snake.c index ead5e0f..8ab3ecb 100644 --- a/du5/snake.c +++ b/du5/snake.c @@ -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; } diff --git a/du5/snake.o b/du5/snake.o index 7632eef..b4ddfcc 100644 Binary files a/du5/snake.o and b/du5/snake.o differ