diff --git a/du5/snake.c b/du5/snake.c index 802286d..8213b2b 100644 --- a/du5/snake.c +++ b/du5/snake.c @@ -42,6 +42,16 @@ struct snake* remove_snake(struct snake* 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) { struct snake* current = snake; @@ -54,4 +64,42 @@ int is_snake(struct snake* snake, int x, int y) { 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; +} + diff --git a/du5/snake.exe b/du5/snake.exe new file mode 100644 index 0000000..d9ad95c Binary files /dev/null and b/du5/snake.exe differ diff --git a/du5/snake.o b/du5/snake.o index cf3be55..c417a44 100644 Binary files a/du5/snake.o and b/du5/snake.o differ