From 3ad4d92adcb5e0e4798c92d740b6770e723316f4 Mon Sep 17 00:00:00 2001 From: ov075wu Date: Thu, 10 Apr 2025 22:47:43 +0200 Subject: [PATCH] refresh --- du5/snake.c | 57 ++++++++++++++++++++++++++++------------------------ du5/snake.o | Bin 1715 -> 1763 bytes 2 files changed, 31 insertions(+), 26 deletions(-) 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 7632eef5a40f7db8e81e919506e5ab6e5fb0dca2..b4ddfcc73591f7462a5edb4aef0ccd606246822e 100644 GIT binary patch delta 477 zcmZ9Iu}T9$5Qb-yJ#rqzh?S&H5Q|6w-XUDt&N2b5Cd9VQmh9a*CeF25X)4O z=E@k+Gey7$NO9m5Czs-iv|*jyBnGm@4)f1H^D)y+|D=smqJeyyTFc0;K@wnuncFH< z$*A?btpNq_Ux!zu5jaeDz&WQf9u>*A&3tYKaBsK-TYew$SEc)O3f%Hk(8SXEEVsOzT5#PDJM6sr{MQX{-`orb5_#oMLZyY%CET3l(Wk zjSeNr>fLS4vN;HalsZGCxzbG7n6nUZA+3LLa}2eVi?@ZStjbYIcFTPaRu;}L@ya5P maI@4@_u#$RY!9{jbp5T(e297 z&Cngd0-_xZ7%z0XK54Ff!B8Os7BI1PeNiXaT>FHff*UMgVeR^)o@)mq(72a&KoX)o z^hsxk3P*S73n0bP$)fVY@Bjb*tp`fDb^xtrcqs~0c--|1kje1!2#^423jNaUGJ!<~ zXg+`ITDVyAAr_!82hbA!*6EY~FlpAKn|$-{|NotbfF^Vb9C!TyGV-|V8xZwc2W;#c zkg=@?Dx@GhsDVHMPM~s-vtM&HyS`y;u6@H&cdohi149Y>4zTus9l$_gcVvC NGBTduyp>gt5db)&o?id}