split code
authorLaurent Mazet <mazet@softndesign.org>
Sat, 25 May 2024 20:51:18 +0000 (22:51 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 25 May 2024 20:51:18 +0000 (22:51 +0200)
battleships.c
board.c
board.h
display.c [new file with mode: 0644]
display.h [new file with mode: 0644]

index 6ac20dbb5a0437da3e9e58146fbd053b3ea0b243..0f4b2cf2aeebc3c393ebe3d796ab6959a0986837 100644 (file)
@@ -1,7 +1,7 @@
 /* depend: */
 /* cflags: */
-/* linker: board.o debug.o -lcurses */
-/* winlnk: board.o debug.o -lpdcurses */
+/* linker: board.o debug.o display.o -lcurses */
+/* winlnk: board.o debug.o display.o -lpdcurses */
 
 #include <curses.h>
 #include <stdio.h>
@@ -9,8 +9,7 @@
 
 #include "board.h"
 #include "debug.h"
-
-#define KEY_ESC 0x1b
+#include "display.h"
 
 /* static variables */
 char *progname = NULL;
@@ -142,8 +141,8 @@ int main (int argc, char *argv[])
             putlocation (board, boatlength, x, y, orient, 'S');
             boatlength = (random () % 4) + 1;
             if (!findlocation (board, boatlength, &x, &y, &orient)) {
-                VERBOSE (ERROR, fprintf (stderr, "can't position for boat %d\n", boatlength));
-                exit (1);
+                VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", boatlength));
+                stop = 1;
             }
             break;
         case 'q':
diff --git a/board.c b/board.c
index aef8af591b32f0f8c805129a8cb73ea82c3c0226..0c9c0d30412629279982ac5c88b97428a894e7bb 100644 (file)
--- a/board.c
+++ b/board.c
@@ -1,4 +1,3 @@
-#include <curses.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -14,80 +13,6 @@ board_t *initboard (int xsize, int ysize)
     return board;
 }
 
-typedef enum {
-    white = 1,
-    red, green, blue,
-    cyan, magenta, yellow,
-    black
-} color_t;
-
-void setcolor (color_t color)
-{
-    static int init = 1;
-    if (init) {
-        init_pair (white, COLOR_WHITE, COLOR_BLACK);
-        init_pair (red, COLOR_BLACK, COLOR_RED);
-        init_pair (green, COLOR_BLACK, COLOR_GREEN);
-        init_pair (blue, COLOR_BLACK, COLOR_BLUE);
-        init_pair (cyan, COLOR_BLACK, COLOR_CYAN);
-        init_pair (magenta, COLOR_BLACK, COLOR_MAGENTA);
-        init_pair (yellow, COLOR_BLACK, COLOR_YELLOW);
-        init_pair (black, COLOR_BLACK, COLOR_WHITE);
-        init = 0;
-    }
-
-    attrset (COLOR_PAIR (color));
-}
-
-void displayboard (board_t *board, int xoffset, int yoffset, int mode)
-{
-    int x, y;
-
-    for (x = -1; x <= board->xsize; x++) {
-        for (y = -1; y <= board->ysize; y++) {
-            int c = ' ';
-            if ((x == -1) && (y == -1)) {
-                c = ACS_ULCORNER;
-                setcolor (black);
-            } else if ((x == -1) && (y == board->ysize)) {
-                c = ACS_LLCORNER;
-                setcolor (black);
-            } else if ((x == board->xsize) && (y == board->ysize)) {
-                c = ACS_LRCORNER;
-                setcolor (black);
-            } else if ((x == board->xsize) && (y == -1)) {
-                c = ACS_URCORNER;
-                setcolor (black);
-            } else if ((x == -1) || (x == board->xsize)) {
-                c = ACS_VLINE;
-                setcolor (black);
-            } else if ((y == -1) || (y == board->ysize)) {
-                c = ACS_HLINE;
-                setcolor (black);
-            } else {
-                c = board->tab[x + y * board->xsize];
-                switch (c) {
-                case 'O':
-                    setcolor (blue);
-                    break;
-                case 'X':
-                    setcolor (red);
-                    break;
-                case 'S':
-                    setcolor (yellow);
-                    break;
-                }
-                if (!mode) {
-                    c = ' ';
-                }
-            }
-
-            mvaddch (yoffset + y, xoffset + x, c);
-            setcolor (white);
-        }
-    }
-}
-
 int testlocation (board_t *board, int length, int x, int y, int orient)
 {
     if ((x < 0) || (x + orient * (length - 1) == board->xsize) ||
diff --git a/board.h b/board.h
index 5ba3ebc3917ca10ecccb1f2a214b1a739fafd099..40c29d6cc395c94e509b37ea74547abdd461a0d9 100644 (file)
--- a/board.h
+++ b/board.h
@@ -9,8 +9,6 @@ typedef struct {
 
 board_t *initboard (int xsize, int ysize);
 
-void displayboard (board_t *board, int xoffset, int yoffset, int mode);
-
 int testlocation (board_t *board, int length, int x, int y, int orient);
 
 int findlocation (board_t *board, int length, int *x, int *y, int *orient);
diff --git a/display.c b/display.c
new file mode 100644 (file)
index 0000000..b4f48f5
--- /dev/null
+++ b/display.c
@@ -0,0 +1,78 @@
+#include <curses.h>
+#include "board.h"
+
+#include "display.h"
+
+typedef enum {
+    white = 1,
+    red, green, blue,
+    cyan, magenta, yellow,
+    black
+} color_t;
+
+void setcolor (color_t color)
+{
+    static int init = 1;
+    if (init) {
+        init_pair (white, COLOR_WHITE, COLOR_BLACK);
+        init_pair (red, COLOR_BLACK, COLOR_RED);
+        init_pair (green, COLOR_BLACK, COLOR_GREEN);
+        init_pair (blue, COLOR_BLACK, COLOR_BLUE);
+        init_pair (cyan, COLOR_BLACK, COLOR_CYAN);
+        init_pair (magenta, COLOR_BLACK, COLOR_MAGENTA);
+        init_pair (yellow, COLOR_BLACK, COLOR_YELLOW);
+        init_pair (black, COLOR_BLACK, COLOR_WHITE);
+        init = 0;
+    }
+
+    attrset (COLOR_PAIR (color));
+}
+
+void displayboard (board_t *board, int xoffset, int yoffset, int mode)
+{
+    int x, y;
+
+    for (x = -1; x <= board->xsize; x++) {
+        for (y = -1; y <= board->ysize; y++) {
+            int c = ' ';
+            if ((x == -1) && (y == -1)) {
+                c = ACS_ULCORNER;
+                setcolor (black);
+            } else if ((x == -1) && (y == board->ysize)) {
+                c = ACS_LLCORNER;
+                setcolor (black);
+            } else if ((x == board->xsize) && (y == board->ysize)) {
+                c = ACS_LRCORNER;
+                setcolor (black);
+            } else if ((x == board->xsize) && (y == -1)) {
+                c = ACS_URCORNER;
+                setcolor (black);
+            } else if ((x == -1) || (x == board->xsize)) {
+                c = ACS_VLINE;
+                setcolor (black);
+            } else if ((y == -1) || (y == board->ysize)) {
+                c = ACS_HLINE;
+                setcolor (black);
+            } else {
+                c = board->tab[x + y * board->xsize];
+                switch (c) {
+                case 'O':
+                    setcolor (blue);
+                    break;
+                case 'X':
+                    setcolor (red);
+                    break;
+                case 'S':
+                    setcolor (yellow);
+                    break;
+                }
+                if (!mode) {
+                    c = ' ';
+                }
+            }
+
+            mvaddch (yoffset + y, xoffset + x, c);
+            setcolor (white);
+        }
+    }
+}
diff --git a/display.h b/display.h
new file mode 100644 (file)
index 0000000..9c95a43
--- /dev/null
+++ b/display.h
@@ -0,0 +1,10 @@
+#ifndef __DISPLAY_H__
+#define __DISPLAY_H__
+
+#include "board.h"
+
+#define KEY_ESC 0x1b
+
+void displayboard (board_t *board, int xoffset, int yoffset, int mode);
+
+#endif /* __DISPLAY_H__ */