From bd65725af8c62621723d926b788e13e6d6e17951 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 14:54:10 +0100 Subject: [PATCH] refactoring: check_if_dead --- src/main/c/Snake/snake_start.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 0584deb..a56e34c 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -150,7 +150,7 @@ void draw(Snake *snake, unsigned char fruit){ //returns index of segments which is identical to tile; -1 if not found int part_of_snake(Snake *snake, unsigned char tile){ - for(int i = 0; i < snake->length; i++){ + for(int i = snake->length - 1; i >= 0; i--){ if(snake->segments[i] == tile){ return i; } @@ -160,11 +160,8 @@ int part_of_snake(Snake *snake, unsigned char tile){ bool check_if_dead(Snake *snake){ //Self - for(int i = 0; i < snake->length; i++){ - int part = part_of_snake(snake, snake->segments[i]); - if(part != -1 && part != i){ - return true; - } + if(part_of_snake(snake, snake->segments[0]) > 0){ + return true; } //Wall