clean logs
authorMazet Laurent <mazet@softndesign.org>
Tue, 4 Jun 2024 10:28:42 +0000 (12:28 +0200)
committerMazet Laurent <mazet@softndesign.org>
Tue, 4 Jun 2024 10:28:42 +0000 (12:28 +0200)
battleships.c
board.c
board.h
display.c
display.h
log.c [new file with mode: 0644]
log.h [new file with mode: 0644]

index 00c201959373233a58dd9321978b302f1618843b..60093c06ba713cf7d5e7f362d3344a97e8b3dc5f 100644 (file)
@@ -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 <curses.h>
 #include <stdio.h>
@@ -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 7a922f8160b1f766b7bf51fe1df76c7d4f93cf89..f5663942c5b0fe6585ffd467050612f34f700e74 100644 (file)
--- 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 e2db5da11ce968df65943e7220a306fa8aa6364e..b46f387adc390eb511dd00bf9c00cdf4e34f77c0 100644 (file)
--- 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);
index 3f6c2e841f928c493c29aa040501ce4759d0b2c8..c0302d33fc0f6f1867f27a2c8d4268263360e33e 100644 (file)
--- 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: */
index 85c56967454744e515595ae475c8cdb19342d411..97b4e29c9b17e96fb74ef333f738c6d2b9e1da58 100644 (file)
--- 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 (file)
index 0000000..3f2903c
--- /dev/null
+++ b/log.c
@@ -0,0 +1,75 @@
+#include <curses.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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 (file)
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__ */