From 6e04d4bb23075ccb35649efd886ba4a3a425f9c7 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sun, 22 Mar 2015 15:39:33 +0000 Subject: snake.c: removed prototypes and git add snake.c --- snake.c | 97 ++++++++++++++++++++++++++++------------------------------------- 1 file changed, 42 insertions(+), 55 deletions(-) diff --git a/snake.c b/snake.c index 019228a..bda1c1d 100644 --- a/snake.c +++ b/snake.c @@ -47,19 +47,6 @@ struct { enum state state; } game; -void init_game(void); -void init_ncurses(void); -void main_loop(void); - -int main(void) -{ - init_ncurses(); - init_game(); - main_loop(); - - return EXIT_SUCCESS; -} - void init_ncurses(void) { initscr(); @@ -103,25 +90,6 @@ void init_game(void) game.apple.y = game.height / 2 + 1; } -void print_board(void); -void poll_input(void); -void simulate(void); - -void main_loop(void) -{ - while (true) { - poll_input(); - if (!game.running) - break; - simulate(); - if (!game.running) - break; - print_board(); - usleep(100000); - } -} - - void print_board(void) { erase(); @@ -129,7 +97,7 @@ void print_board(void) for (uint16_t i = game.snake.tail; i <= game.snake.head; i++) { chtype ch; - if (!!(i % 2)) + if (i % 2) ch = ACS_BLOCK | COLOR_PAIR(C_SEGMENT0); else ch = ACS_BLOCK | COLOR_PAIR(C_SEGMENT1); @@ -142,22 +110,6 @@ void print_board(void) refresh(); } -void redirect(enum direction desired_dir); - -void poll_input(void) -{ - int c; - - while ((c = getch()) != ERR) - switch (c) { - case 'h': redirect(D_LEFT); break; - case 'j': redirect(D_DOWN); break; - case 'k': redirect(D_UP); break; - case 'l': redirect(D_RIGHT); break; - case 'q': game.running = false; break; - } -} - void redirect(enum direction desired_dir) { struct segment *current = &game.snake.segments[game.snake.head]; @@ -186,6 +138,20 @@ void redirect(enum direction desired_dir) game.snake.direction = desired_dir; } +void poll_input(void) +{ + int c; + + while ((c = getch()) != ERR) + switch (c) { + case 'h': redirect(D_LEFT); break; + case 'j': redirect(D_DOWN); break; + case 'k': redirect(D_UP); break; + case 'l': redirect(D_RIGHT); break; + case 'q': game.running = false; break; + } +} + inline uint16_t rand_to_max(uint16_t max) { double random; @@ -194,7 +160,14 @@ inline uint16_t rand_to_max(uint16_t max) return random / (double)RAND_MAX * (double)max; } -bool part_of_snake(uint16_t x, uint16_t y); +bool part_of_snake(uint16_t x, uint16_t y) +{ + for (uint16_t i = game.snake.tail; i != game.snake.head; i++) + if (game.snake.segments[i].x == x && game.snake.segments[i].y == y) + return true; + + return false; +} void replace_apple(void) { @@ -256,11 +229,25 @@ void simulate(void) game.snake.tail++; } -bool part_of_snake(uint16_t x, uint16_t y) +void main_loop(void) { - for (uint16_t i = game.snake.tail; i != game.snake.head; i++) - if (game.snake.segments[i].x == x && game.snake.segments[i].y == y) - return true; + while (true) { + poll_input(); + if (!game.running) + break; + simulate(); + if (!game.running) + break; + print_board(); + usleep(100000); + } +} - return false; +int main(void) +{ + init_ncurses(); + init_game(); + main_loop(); + + return EXIT_SUCCESS; } -- cgit v1.2.3-54-g00ecf