From 2efc9ad9605df1e4543a66d8f28616c5ddb73c66 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Thu, 19 Sep 2024 20:03:35 +0200 Subject: [PATCH] find possible positions --- display.c | 3 +++ function.c | 31 +++++++++++++++++++++++++++++++ function.h | 4 ++++ reversi.c | 6 +++++- 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/display.c b/display.c index 4088816..98021de 100644 --- a/display.c +++ b/display.c @@ -84,6 +84,9 @@ void _put_color_block (int y, int x, char symb) case '3': mvaddcb (y, x, lighterred); break; + case '4': + mvaddcb (y, x, lightergreen); + break; } } diff --git a/function.c b/function.c index ca43ba2..d4a7b74 100644 --- a/function.c +++ b/function.c @@ -37,6 +37,17 @@ board_t *initboard (int width, int height) return board; } +board_t *copyboard (board_t *board) +{ + board_t *newboard = (board_t *) malloc (sizeof (board_t)); + CHECKALLOC (board); + memcpy (newboard, board, sizeof (board_t)); + newboard->tab = (char *) calloc (1, board->width * board->height + 1); + CHECKALLOC (newboard->tab); + memcpy (newboard->tab, board->tab, board->width * board->height + 1); + return newboard; +} + board_t *setscale (board_t *board, int scale) { board->scale = scale; @@ -289,4 +300,24 @@ int putdisk (board_t *board, int x, int y, int id) return ret; } +int find (board_t *board, int id) +{ + int ret = 0; + int x, y; + for (x = 0; x < board->width; x++) { + for (y = 0; y < board->height; y++) { + if (*getcell (board, x, y) == ' ') { + int dir; + for (dir = 0; dir < 8; dir++) { + if (_checkline (board, x, y, id, dir)) { + *getcell (board, x, y) = '4'; + ret = 1; + } + } + } + } + } + return ret; +} + /* vim: set ts=4 sw=4 et: */ diff --git a/function.h b/function.h index 7a94cf7..af8eed9 100644 --- a/function.h +++ b/function.h @@ -23,6 +23,8 @@ int strmaxlen (char *str, char ch); board_t *initboard (int width, int height); +board_t *copyboard (board_t *board); + board_t *setscale (board_t *board, int scale); void freeboard (board_t *board); @@ -41,6 +43,8 @@ board_t *loadboard (char *str); int putdisk (board_t *board, int x, int y, int id); +int find (board_t *board, int id); + #endif /* __FUNCTION_H__ */ /* vim: set ts=4 sw=4 et: */ diff --git a/reversi.c b/reversi.c index a41a579..67ffb36 100644 --- a/reversi.c +++ b/reversi.c @@ -175,7 +175,11 @@ int main (int argc, char *argv[]) int mode = 0; while (!stop) { - boardwindow (board, 0); + board_t *boardturn = copyboard (board); + int ret = find (boardturn, id); + boardwindow (boardturn, 0); + freeboard (boardturn); + displaycursor (board, id, xcursor, ycursor); int ch = getch (); -- 2.30.2