From ad74267726b37dcca2e50914e347c6411fd70cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Vasi=C4=BE?= Date: Wed, 27 Apr 2022 20:33:02 +0200 Subject: [PATCH] domaca_uloha_7 --- du7/snake.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/du7/snake.c b/du7/snake.c index cbd321c..451ecd4 100644 --- a/du7/snake.c +++ b/du7/snake.c @@ -62,5 +62,41 @@ int is_snake(struct snake* snake,int x,int y){ int step_state(struct state* st){ int nx = (st->snake->x + st->sx); int ny = (st->snake->y + st->sy); + int i, pocetJedal = FOOD_COUNT; + + struct snake* had = calloc(1, sizeof(struct snake)); + struct snake* hlava = calloc(1, sizeof(struct snake)); + + had = st->snake; + hlava = st->snake; + + while (had != NULL) { + if (snake->x == nx || snake->y == ny) return END_SNAKE; + + for (i = 0; i < FOOD_COUNT; i++) { + if (foodx[i] == nx) { + if (foody[i] == ny) { + had = add_snake(had, foodx[i], foody[i]); + + foodx[i] = -1; + foody[i] = -1; + pocetJedal--; + } + } + } + + if (pocetJedal == 0) return END_FOOD; + } + + had = hlava; + free(hlava); + + if (nx > 0 && nx < width) { + if (ny > 0 && ny < height) { + had = add_snake(had, nx, ny); + had = remove_snake(had); + } + } + return END_CONTINUE; }