initial board
authorLaurent Mazet <mazet@softndesign.org>
Thu, 23 May 2024 21:54:11 +0000 (23:54 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Thu, 23 May 2024 21:54:11 +0000 (23:54 +0200)
battleships.c
board.c [new file with mode: 0644]
board.h [new file with mode: 0644]

index d04328f369aa136e199ebedead9a8c77a9738576..a7ff248f51ea53ffd4fc6aecb13194510a3ed734 100644 (file)
@@ -1,12 +1,13 @@
 /* depend: */
 /* cflags: */
-/* linker: debug.o -lcurses */
-/* winlnk: debug.o -lpdcurses */
+/* linker: board.o debug.o -lcurses */
+/* winlnk: board.o debug.o -lpdcurses */
 
+#include <curses.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 
+#include "board.h"
 #include "debug.h"
 
 /* static variables */
@@ -63,6 +64,31 @@ int main (int argc, char *argv[])
 
     VERBOSE (DEBUG, fprintf (stdout, "debug mode\n"));
 
+    initscr ();
+    cbreak ();
+    noecho ();
+    curs_set (0);
+    start_color ();
+
+    board_t *board = initboard (20, 10);
+    board->tab[3] = 'X';
+    board->tab[4] = 'O';
+    board->tab[5] = 'S';
+    int stop = 0;
+    while (!stop) {
+        displayboard (board, 4, 4);
+        int c = getch ();
+        switch (c) {
+        case 'q':
+            stop = 1;
+            break;
+        case ERR:
+            break;
+        }
+    }
+
+    endwin ();
+
     return 0;
 }
 
diff --git a/board.c b/board.c
new file mode 100644 (file)
index 0000000..e0002f3
--- /dev/null
+++ b/board.c
@@ -0,0 +1,86 @@
+#include <curses.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "board.h"
+
+board_t *initboard (int xsize, int ysize)
+{
+    board_t *board = (board_t *) malloc (sizeof (board_t));
+    board->tab = (char *) malloc (xsize * ysize);
+    memset (board->tab, ' ', xsize * ysize);
+    board->xsize = xsize;
+    board->ysize = 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 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;
+                }
+            }
+
+            mvaddch (yoffset + 1 + y, xoffset + 1 + x, c);
+            setcolor (white);
+        }
+    }
+}
diff --git a/board.h b/board.h
new file mode 100644 (file)
index 0000000..18a1352
--- /dev/null
+++ b/board.h
@@ -0,0 +1,14 @@
+#ifndef __BOARD_H__
+#define __BOARD_H__
+
+typedef struct {
+    int xsize;
+    int ysize;
+    char *tab;
+} board_t;
+
+board_t *initboard (int xsize, int ysize);
+
+void displayboard (board_t *board, int xoffset, int yoffset);
+
+#endif /* __BOARD_H__ */