From 9780ae9fe622bfeb62b8d8f61b477ebd718cc4d6 Mon Sep 17 00:00:00 2001 From: Mazet Laurent Date: Tue, 4 Jun 2024 12:28:42 +0200 Subject: [PATCH] clean logs --- battleships.c | 24 ++++++++++------- board.c | 9 +++++++ board.h | 2 ++ display.c | 47 +------------------------------- display.h | 4 +-- log.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ log.h | 18 +++++++++++++ 7 files changed, 122 insertions(+), 57 deletions(-) create mode 100644 log.c create mode 100644 log.h diff --git a/battleships.c b/battleships.c index 00c2019..60093c0 100644 --- a/battleships.c +++ b/battleships.c @@ -1,7 +1,7 @@ /* depend: */ /* cflags: */ -/* linker: board.o debug.o display.o -lcurses */ -/* winlnk: board.o debug.o display.o -lpdcurses */ +/* linker: board.o debug.o display.o log.o -lcurses */ +/* winlnk: board.o debug.o display.o log.o -lpdcurses */ #include #include @@ -12,6 +12,7 @@ #include "board.h" #include "debug.h" #include "display.h" +#include "log.h" /* static variables */ char *progname = NULL; @@ -143,8 +144,9 @@ int main (int argc, char *argv[]) displayhelp (help, xhelp, yhelp, whelp); - displaylogs ("Welcome to Battle Ships", wlogs, hlogs, xlogs, ylogs); - displaylogs ("Put your ships on board", wlogs, hlogs, xlogs, ylogs); + log_t *logs = initlog (wlogs, hlogs); + displaylog (logs, "Welcome to Battle Ships", xlogs, ylogs); + displaylog (logs, "Put your ships on board", xlogs, ylogs); int stop = 0; while (!stop) { @@ -256,7 +258,7 @@ int main (int argc, char *argv[]) VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1)); stop = 1; } - displaylogs ("Send bomb on computer board", wlogs, hlogs, xlogs, ylogs); + displaylog (logs, "Send bomb on computer board", xlogs, ylogs); } else { if (!findlocation (boardhuman, length, &x, &y, &orient, " ")) { VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", length)); @@ -265,7 +267,7 @@ int main (int argc, char *argv[]) } } else { if (drawbomb (boardcomputer, x, y)) { - displaylogs ("Computer ship hitted", wlogs, hlogs, xlogs, ylogs); + displaylog (logs, "Computer ship hitted", xlogs, ylogs); } if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) { VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1)); @@ -274,19 +276,19 @@ int main (int argc, char *argv[]) if (testsunk (boardcomputer)) { displayboard (boardhuman, xhuman, yhuman, mode, 1); displayboard (boardcomputer, xcomputer, ycomputer, mode, 1); - displaylogs ("All computer ships sunk", wlogs, hlogs, xlogs, ylogs); + displaylog (logs, "All computer ships sunk", xlogs, ylogs); stop = stoploop (); } /* computer turn */ if (drawbomb (boardhuman, -1, -1)) { - displaylogs ("Humain ship hitted", wlogs, hlogs, xlogs, ylogs); + displaylog (logs, "Humain ship hitted", xlogs, ylogs); } if (testsunk (boardhuman)) { displayboard (boardhuman, xhuman, yhuman, mode, 1); displayboard (boardcomputer, xcomputer, ycomputer, mode, 1); - displaylogs ("All human ships sunk", wlogs, hlogs, xlogs, ylogs); + displaylog (logs, "All human ships sunk", xlogs, ylogs); stop = stoploop (); } } @@ -308,6 +310,10 @@ int main (int argc, char *argv[]) endwin (); + freelog (logs); + freeboard (boardhuman); + freeboard (boardcomputer); + return 0; } diff --git a/board.c b/board.c index 7a922f8..f566394 100644 --- a/board.c +++ b/board.c @@ -14,6 +14,15 @@ board_t *initboard (char *title, int xsize, int ysize) return board; } +void freeboard (board_t *board) +{ + if (board) { + free (board->tab); + free (board->title); + } + free (board); +} + int isoneof (char t, char *symbs) { int ret = 0; diff --git a/board.h b/board.h index e2db5da..b46f387 100644 --- a/board.h +++ b/board.h @@ -10,6 +10,8 @@ typedef struct { board_t *initboard (char *title, int xsize, int ysize); +void freeboard (board_t *board); + int testlocation (board_t *board, int length, int x, int y, int orient, char *symbs); int findlocation (board_t *board, int length, int *x, int *y, int *orient, char *symbs); diff --git a/display.c b/display.c index 3f6c2e8..c0302d3 100644 --- a/display.c +++ b/display.c @@ -129,49 +129,4 @@ void displayhelp (char *msg, int xoffset, int yoffset, int length) } } -void displaylogs (char *msg, int width, int height, int xoffset, int yoffset) -{ - int i, j; - - static char **buffer = NULL; - if (buffer == NULL) { - buffer = (char **) malloc (height); - for (i = 0; i < height; i++) { - buffer[i] = (char *) calloc (1, width + 1); - } - } - for (i = 0; i < height; i++) { - if (*buffer[i] == '\0') { - strncpy (buffer[i], msg, width); - break; - } - } - if (i == height) { - for (i = 1; i < height; i++) { - strcpy (buffer[i - 1], buffer[i]); - } - strncpy (buffer[height - 1], msg, width); - } - - mvaddch (yoffset - 1, xoffset - 1, ACS_ULCORNER); - mvaddch (yoffset + height, xoffset - 1, ACS_LLCORNER); - mvaddch (yoffset + height, xoffset + width, ACS_LRCORNER); - mvaddch (yoffset - 1, xoffset + width, ACS_URCORNER); - - for (i = 0; i < width; i++) { - mvaddch (yoffset - 1, xoffset + i, ACS_HLINE); - mvaddch (yoffset + height, xoffset + i, ACS_HLINE); - for (j = 0; j < height; j++) { - mvaddch (yoffset + j, xoffset + i, ' '); - } - } - - for (i = 0; i < height; i++) { - mvaddch (yoffset + i, xoffset - 1, ACS_VLINE); - mvaddch (yoffset + i, xoffset + width, ACS_VLINE); - if (*buffer[i] != 0) { - mvaddstr (yoffset + i, xoffset, buffer[i]); - } - } -} - +/* vim: set ts=4 sw=4 et: */ diff --git a/display.h b/display.h index 85c5696..97b4e29 100644 --- a/display.h +++ b/display.h @@ -11,6 +11,6 @@ void displayelement (int length, int x, int y, int orient, char symb, int show); void displayhelp (char *msg, int xoffset, int yoffset, int length); -void displaylogs (char *messages, int width, int height, int xoffset, int yoffset); - #endif /* __DISPLAY_H__ */ + +/* vim: set ts=4 sw=4 et: */ diff --git a/log.c b/log.c new file mode 100644 index 0000000..3f2903c --- /dev/null +++ b/log.c @@ -0,0 +1,75 @@ +#include +#include +#include + +#include "log.h" + +log_t *initlog (int width, int height) +{ + int i; + + log_t *logs = (log_t *) malloc (sizeof (log_t)); + logs->buffer = (char **) malloc (height * sizeof (char *)); + for (i = 0; i < height; i++) { + logs->buffer[i] = (char *) calloc (1, width + 1); + } + logs->width = width; + logs->height = height; + + return logs; +} + +void freelog (log_t *logs) +{ + int i; + if (logs) { + if (logs->buffer) { + for (i = 0; i < logs->height; i++) { + free (logs->buffer[i]); + } + free (logs->buffer); + } + } + free (logs); +} + +void displaylog (log_t *logs, char *msg, int xoffset, int yoffset) +{ + int i, j; + + for (i = 0; i < logs->height; i++) { + if (*logs->buffer[i] == '\0') { + strncpy (logs->buffer[i], msg, logs->width); + break; + } + } + if (i == logs->height) { + for (i = 1; i < logs->height; i++) { + strcpy (logs->buffer[i - 1], logs->buffer[i]); + } + strncpy (logs->buffer[logs->height - 1], msg, logs->width); + } + + mvaddch (yoffset - 1, xoffset - 1, ACS_ULCORNER); + mvaddch (yoffset + logs->height, xoffset - 1, ACS_LLCORNER); + mvaddch (yoffset + logs->height, xoffset + logs->width, ACS_LRCORNER); + mvaddch (yoffset - 1, xoffset + logs->width, ACS_URCORNER); + + for (i = 0; i < logs->width; i++) { + mvaddch (yoffset - 1, xoffset + i, ACS_HLINE); + mvaddch (yoffset + logs->height, xoffset + i, ACS_HLINE); + for (j = 0; j < logs->height; j++) { + mvaddch (yoffset + j, xoffset + i, ' '); + } + } + + for (i = 0; i < logs->height; i++) { + mvaddch (yoffset + i, xoffset - 1, ACS_VLINE); + mvaddch (yoffset + i, xoffset + logs->width, ACS_VLINE); + if (*logs->buffer[i] != 0) { + mvaddstr (yoffset + i, xoffset, logs->buffer[i]); + } + } +} + +/* vim: set ts=4 sw=4 et: */ diff --git a/log.h b/log.h new file mode 100644 index 0000000..6fac2c4 --- /dev/null +++ b/log.h @@ -0,0 +1,18 @@ +#ifndef __LOG_H__ +#define __LOG_H__ + +typedef struct { + char **buffer; + int width; + int height; + int xoffset; + int yoffset; +} log_t; + +log_t *initlog (int width, int height); + +void freelog (log_t *logs); + +void displaylog (log_t *logs, char *msg, int xoffset, int yoffset); + +#endif /* __LOG_H__ */ -- 2.30.2