diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 57d332f..efcd08b 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -17,6 +17,7 @@ void main_menu(); void game(); Snake initialize_game(); void draw(Snake *snake); +int part_of_snake(Snake *snake, unsigned char tile); void snake_start(){ @@ -83,24 +84,24 @@ void draw(Snake *snake){ for(int i = 1; i <= HEIGHT; i++){ printf("|"); for(int j = 1; j <= WIDTH; j++){ - //printf("%d:", i * 16 + j); - for(int pos = 0; pos < snake->length; pos++){ - //printf(" %d", snake->segments[pos]); - if(i * 16 + j == snake->segments[pos]){ - if(pos == 0){ - printf("0"); - }else{ - printf("O"); - } - break; - }else if(pos == snake->length - 1){ - printf(" "); - } - } + int index = part_of_snake(snake, i * 16 + j); + if(index == 0){printf("0");} + else if(index > 0){printf("O");} + else{printf(" ");} } printf("|\n"); } printf("+"); for(int i = 0; i < WIDTH; i++){printf("-");} printf("+\n"); +} + +//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++){ + if(snake->segments[i] == tile){ + return i; + } + } + return -1; } \ No newline at end of file diff --git a/src/main/c/Snake/snake_start.h b/src/main/c/Snake/snake_start.h index e69de29..423f324 100644 --- a/src/main/c/Snake/snake_start.h +++ b/src/main/c/Snake/snake_start.h @@ -0,0 +1,6 @@ +#ifndef SNAKE_START_H +#define SNAKE_START_H + +void snake_start(); + +#endif // SNAKE_START_H \ No newline at end of file