From bc0b36848f0a79da795508ae7f1c17536d00377d Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Fri, 30 Aug 2024 10:58:37 +0200 Subject: [PATCH] no bonus/malus under the snake --- function.c | 15 ++++++++++----- function.h | 2 +- snake.c | 40 +++++++++++++++++++--------------------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/function.c b/function.c index 0121438..d186739 100644 --- a/function.c +++ b/function.c @@ -332,21 +332,26 @@ int movesnake (board_t *board, snake_t *snake, int dir) return ret; } -void drawbonus (board_t *board, int malus, int bonus) +void drawbonus (board_t *board, snake_t *snake, int malus, int bonus) { + int x, y; int i; memset (board->tab, ' ', board->width * board->height); for (i = 0; i < malus; i++) { - int x = rand () % board->width; - int y = rand () % board->height; + do { + x = rand () % board->width; + y = rand () % board->height; + } while (isown (snake, x, y)); *getcell (board, x, y) = '4'; } for (i = 0; i < bonus; i++) { - int x = rand () % board->width; - int y = rand () % board->height; + do { + x = rand () % board->width; + y = rand () % board->height; + } while (isown (snake, x, y)); *getcell (board, x, y) = '5'; } } diff --git a/function.h b/function.h index 49e7cfa..65da249 100644 --- a/function.h +++ b/function.h @@ -45,7 +45,7 @@ void freesnake (snake_t *snake); int movesnake (board_t *board, snake_t *snake, int dir); -void drawbonus (board_t *board, int malus, int bonus); +void drawbonus (board_t *board, snake_t *snake, int malus, int bonus); #endif /* __FUNCTION_H__ */ diff --git a/snake.c b/snake.c index 63641fa..83d2a97 100644 --- a/snake.c +++ b/snake.c @@ -127,7 +127,8 @@ int main (int argc, char *argv[]) int h0board = LINES - 2 * (yoffset + 1); board_t *board = initboard (w0board, h0board); applyscale (board, scale); - drawbonus (board, malus, bonus); + snake_t *snake = initsnake (board); + drawbonus (board, snake, malus, bonus); /* window positions */ int xboard = board->xoffset = xoffset + 1; @@ -142,9 +143,6 @@ int main (int argc, char *argv[]) VERBOSE (DEBUG, printf ("width: %d\nheight: %d\n", board->width, board->height)); - /* init snake */ - snake_t *snake = initsnake (board); - /* help window */ helpwindow (help, xhelp, yhelp); @@ -157,22 +155,6 @@ int main (int argc, char *argv[]) int nbturns = 0; while (!stop) { - boardwindow (board, 0); - - switch (mode) { - case 0: - setcolor (gray_black); - break; - case 1: - setcolor (yellow_black); - break; - case 2: - setcolor (red_black); - break; - } - scorewindow (xscore, yscore, whelp - 2, score, (duration * speed) / 1000); - setcolor (gray_black); - if (mode == 1) { if (isovertime (&turn)) { setendtime (&turn, speed); @@ -190,12 +172,28 @@ int main (int argc, char *argv[]) } if (nbturns == ratio) { - drawbonus (board, malus, bonus); + drawbonus (board, snake, malus, bonus); nbturns = 0; } } + + boardwindow (board, 0); displaysnake (board, snake); + switch (mode) { + case 0: + setcolor (gray_black); + break; + case 1: + setcolor (yellow_black); + break; + case 2: + setcolor (red_black); + break; + } + scorewindow (xscore, yscore, whelp - 2, score, (duration * speed) / 1000); + setcolor (gray_black); + int ch = getch (); /* main controls */ -- 2.30.2