split code
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Tue, 28 May 2024 14:09:13 +0000 (16:09 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Tue, 28 May 2024 14:09:13 +0000 (16:09 +0200)
board.c
board.h
constant.h
display.c [new file with mode: 0644]
display.h [new file with mode: 0644]
scrabble.c
type.h [new file with mode: 0644]

diff --git a/board.c b/board.c
index af307de3817dbe45eb902806c468774a661cf493..bbd7c8408d85d2d82642a2b8cdc0837f39420440 100644 (file)
--- a/board.c
+++ b/board.c
@@ -3,6 +3,7 @@
 #include <string.h>
 
 #include "debug.h"
+#include "type.h"
 
 #include "board.h"
 
@@ -17,36 +18,6 @@ int contains (char *list, char *str)
     return 0;
 }
 
-typedef enum {
-    white = 1,
-    red,
-    green,
-    blue,
-    cyan,
-    magenta,
-    yellow,
-    black,
-} color_t;
-
-void set_color (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 (magenta, COLOR_BLACK, COLOR_MAGENTA);
-        init_pair (yellow, COLOR_BLACK, COLOR_YELLOW);
-        init_pair (cyan, COLOR_BLACK, COLOR_CYAN);
-        init_pair (black, COLOR_BLACK, COLOR_WHITE);
-        init = 0;
-    }
-
-    attrset (COLOR_PAIR(color));
-}
-
 play_t *initplay (int xsize, int ysize)
 {
     play_t *play = (play_t *) malloc (sizeof (play_t));
@@ -66,71 +37,4 @@ int putletter (play_t *play, char letter, int x, int y)
     return ret;
 }
 
-void showletter (char letter, int x, int y)
-{
-    set_color (black);
-    mvaddch(y, x, letter);
-    set_color (white);
-}
-
-void showboard (play_t *play, board_t *board, int xoffset, int yoffset, int full)
-{
-    int x, y;
-    for (x = -1; x <= board->xsize; x++) {
-        for (y = -1; y <= board->ysize; y++) {
-            int c = ' ';
-            char pos[4] = {0};
-            sprintf (pos, "%c%d", 'A' + x, 1 + y);
-            if ((x == -1) && (y == -1)) {
-                c = ACS_ULCORNER;
-                set_color (black);
-            } else if ((x == board->xsize) && (y== -1)) {
-                c = ACS_URCORNER;
-                set_color (black);
-            } else if ((x == -1) && (y == board->ysize)) {
-                c = ACS_LLCORNER;
-                set_color (black);
-            } else if ((x == board->xsize) && (y == board->ysize)) {
-                c = ACS_LRCORNER;
-                set_color (black);
-            } else if ((x == -1) || (x == board->xsize)) {
-                c = ACS_VLINE;
-                set_color (black);
-            } else if ((y == -1) || (y == board->xsize)) {
-                c = ACS_HLINE;
-                set_color (black);
-            } else {
-                c = play->tab[x + y * board->xsize];
-                if (c == ' ') {
-                    if (contains (board->TW, pos)) {
-                        c = 'T';
-                        set_color (red);
-                    } else if (contains (board->DW, pos)) {
-                        c = 'D';
-                        set_color (magenta);
-                    } else if (contains (board->TL, pos)) {
-                        c = 't';
-                        set_color (blue);
-                    } else if (contains (board->DL, pos)) {
-                        c = 'd';
-                        set_color (cyan);
-                    } else if (contains (board->CS, pos)) {
-                        c = 'S';
-                        set_color (magenta);
-                    }
-                    if (!full) {
-                        c = ' ';
-                    }
-                }
-            }
-            VERBOSE (DEBUG, if ((c < 32) || (c > 255)) printf ("character: %c\n", c));
-            if (c) {
-                mvaddch(yoffset + y, xoffset + x, c);
-            }
-            set_color (white);
-
-        }
-    }
-}
-
 /* vim: set ts=4 sw=4 et: */
diff --git a/board.h b/board.h
index 4f60f99ea06baee8b8430121d9340c96e44cad09..d665ab28fb017a8bc00feaaa1628d2a0b309e9e5 100644 (file)
--- a/board.h
+++ b/board.h
@@ -1,24 +1,14 @@
 #ifndef __BOARD_H__
 #define __BOARD_H__
 
-#include "constant.h"
+#include "type.h"
 
-typedef struct {
-    int xsize;
-    int ysize;
-    char *tab;
-} play_t;
+int contains (char *list, char *str);
 
-char *initboard (int xsize, int ysize);
-
-play_t *initplay ();
+play_t *initplay (int xsize, int ysize);
 
 int putletter (play_t *play, char letter, int x, int y);
 
-void showletter (char letter, int x, int y);
-
-void showboard (play_t *play, board_t *board, int xoffset, int yoffset, int full);
-
 #endif /* __BOARD_H__ */
 
 /* vim: set ts=4 sw=4 et: */
index 3a202f939cdfa5d8993fb09e98e58f557d7f7a99..5cb82ffbd58127443a7f8ce858c863019d4fc5f2 100644 (file)
@@ -1,32 +1,7 @@
 #ifndef __CONSTANT_H__
 #define __CONSTANT_H__
 
-#include "board.h"
-
-typedef struct {
-    int xsize;
-    int ysize;
-    float factor;
-    int length;
-    int premium;
-    char *TW;
-    char *DW;
-    char *TL;
-    char *DL;
-    char *CS;
-} board_t;
-
-typedef struct {
-    char letter;
-    int value;
-} lettervalue_t;
-
-typedef struct {
-    int nbletters;
-    int nbtiles;
-    char *tiles;
-    lettervalue_t *lettervalues;
-} bag_t;
+#include "type.h"
 
 board_t *getboard (char *name);
 
diff --git a/display.c b/display.c
new file mode 100644 (file)
index 0000000..53a4f22
--- /dev/null
+++ b/display.c
@@ -0,0 +1,155 @@
+#include <curses.h>
+#include <stdlib.h>
+
+#include "board.h"
+#include "debug.h"
+
+#include "display.h"
+
+typedef enum {
+    white = 1,
+    red,
+    green,
+    blue,
+    cyan,
+    magenta,
+    yellow,
+    black,
+} color_t;
+
+void set_color (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 (magenta, COLOR_BLACK, COLOR_MAGENTA);
+        init_pair (yellow, COLOR_BLACK, COLOR_YELLOW);
+        init_pair (cyan, COLOR_BLACK, COLOR_CYAN);
+        init_pair (black, COLOR_BLACK, COLOR_WHITE);
+        init = 0;
+    }
+
+    attrset (COLOR_PAIR(color));
+}
+
+void showletter (char letter, int x, int y)
+{
+    set_color (black);
+    mvaddch(y, x, letter);
+    set_color (white);
+}
+
+void showboard (play_t *play, board_t *board, int xoffset, int yoffset, int full)
+{
+    int x, y;
+    for (x = -1; x <= board->xsize; x++) {
+        for (y = -1; y <= board->ysize; y++) {
+            int c = ' ';
+            char pos[4] = {0};
+            sprintf (pos, "%c%d", 'A' + x, 1 + y);
+            if ((x == -1) && (y == -1)) {
+                c = ACS_ULCORNER;
+                set_color (black);
+            } else if ((x == board->xsize) && (y== -1)) {
+                c = ACS_URCORNER;
+                set_color (black);
+            } else if ((x == -1) && (y == board->ysize)) {
+                c = ACS_LLCORNER;
+                set_color (black);
+            } else if ((x == board->xsize) && (y == board->ysize)) {
+                c = ACS_LRCORNER;
+                set_color (black);
+            } else if ((x == -1) || (x == board->xsize)) {
+                c = ACS_VLINE;
+                set_color (black);
+            } else if ((y == -1) || (y == board->xsize)) {
+                c = ACS_HLINE;
+                set_color (black);
+            } else {
+                c = play->tab[x + y * board->xsize];
+                if (c == ' ') {
+                    if (contains (board->TW, pos)) {
+                        c = 'T';
+                        set_color (red);
+                    } else if (contains (board->DW, pos)) {
+                        c = 'D';
+                        set_color (magenta);
+                    } else if (contains (board->TL, pos)) {
+                        c = 't';
+                        set_color (blue);
+                    } else if (contains (board->DL, pos)) {
+                        c = 'd';
+                        set_color (cyan);
+                    } else if (contains (board->CS, pos)) {
+                        c = 'S';
+                        set_color (magenta);
+                    }
+                    if (!full) {
+                        c = ' ';
+                    }
+                }
+            }
+            VERBOSE (DEBUG, if ((c < 32) || (c > 255)) printf ("character: %c\n", c));
+            if (c) {
+                mvaddch(yoffset + y, xoffset + x, c);
+            }
+            set_color (white);
+
+        }
+    }
+}
+
+void helpwindow (char *msg, int xoffset, int yoffset, int length)
+{
+    char *title = "Help message";
+    int i;
+
+    for (i = 0; (i < length) && (title[i] != '\0'); i++) {
+        mvaddch (yoffset , xoffset + i, title[i]);
+        mvaddch (yoffset + 1, xoffset + i, ACS_HLINE);
+    }
+
+    i = 0;
+    int j = 2;
+    while (*msg != '\0') {
+        if ((*msg == '\n') || (i  == length)) {
+            i = 0;
+            j++;
+        }
+        if (*msg != '\n') {
+            mvaddch (yoffset + j, xoffset + i, *msg);
+            i++;
+        }
+        msg++;
+    }
+}
+
+void valuewindow (bag_t *bag, int xoffset, int yoffset, int length)
+{
+    char *title = "Tile values";
+    int i;
+    int n = length / 5;
+
+    for (i = 0; (i < length) && (title[i] != '\0'); i++) {
+        mvaddch (yoffset, xoffset + i, title[i]);
+        mvaddch (yoffset + 1, xoffset + i, ACS_HLINE);
+    }
+
+    int N = (bag->nbletters + n - 1) / n;
+    for (i = 0; i < bag->nbletters; i++) {
+        int x = xoffset + (i % 2) + 5 * (i / N);
+        int y = yoffset + 2 + (i % N);
+        set_color (black);
+        mvaddch (y, x, (bag->lettervalues + i)->letter);
+        set_color (white);
+        char msg[3];
+        sprintf (msg, "%d", (bag->lettervalues + i)->value);
+        mvaddstr (y, x + 2 + ((i + 1) % 2), msg);
+    }
+}
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/display.h b/display.h
new file mode 100644 (file)
index 0000000..26efc10
--- /dev/null
+++ b/display.h
@@ -0,0 +1,18 @@
+#ifndef __DISPLAY_H__
+#define __DISPLAY_H__
+
+#include "type.h"
+
+int putletter (play_t *play, char letter, int x, int y);
+
+void showletter (char letter, int x, int y);
+
+void showboard (play_t *play, board_t *board, int xoffset, int yoffset, int full);
+
+void helpwindow (char *msg, int xoffset, int yoffset, int length);
+
+void valuewindow (bag_t *bag, int xoffset, int yoffset, int length);
+
+#endif /* __DISPLAY_H__ */
+
+/* vim: set ts=4 sw=4 et: */
index 8221e58b3171baa360cb41bfe958fa0dad835016..633cdf1ba00a1a8f4ab8af058cdaa535ca863478 100644 (file)
@@ -1,8 +1,8 @@
 /* depend: */
 /* cflags: */
-/* linker: board.o constant.o debug.o -lcurses */
-/* winlnk: board.o constant.o debug.o -lpdcurses */
-/* winxxlnk: board.o constant.o debug.o -lncurses */
+/* linker: board.o constant.o display.o debug.o -lcurses */
+/* winlnk: board.o constant.o display.o debug.o -lpdcurses */
+/* winxxlnk: board.o constant.o display.o debug.o -lncurses */
 
 #include <curses.h>
 #include <stdio.h>
@@ -10,6 +10,7 @@
 
 #include "board.h"
 #include "constant.h"
+#include "display.h"
 #include "debug.h"
 
 #define KEY_ESC 0x1b
@@ -21,6 +22,11 @@ char *version = "0.1";
 char *boardname = "15x15-7";
 char *language = "fr";
 
+int xoffset = 4;
+int yoffset = 3;
+
+char *help = "<d> Draw tile\n<i>, <j>, <k>, <l> Move tile\n<p> Put tile<q> Quit\n<t> Toggle case legend\n";
+
 int usage (int ret)
 {
     FILE *fd = ret ? stderr : stdout;
@@ -34,23 +40,6 @@ int usage (int ret)
     return ret;
 }
 
-void help_window (int xoffset, int yoffset, int length)
-{
-    char *message = "Help message\n------------\n<q> Quit\n<t> Toggle case legend\n";
-    int l = 0, n = 0;
-    while (*message != '\0') {
-        if ((*message == '\n') || (l  == length)) {
-            l = 0;
-            n++;
-        }
-        if (*message != '\n') {
-            mvaddch (yoffset + n, xoffset + l, *message);
-            l++;
-        }
-        message++;
-    }
-}
-
 /* main function */
 int main (int argc, char *argv[])
 {
@@ -130,11 +119,10 @@ int main (int argc, char *argv[])
 
     start_color ();
 
-    help_window (50, 1, 30);
+    valuewindow (bag, 2 * xoffset + board->xsize, yoffset - 1, 10);
+    helpwindow (help, 3 * xoffset + board->xsize + 10, yoffset - 1, 30);
 
     int mode = 0;
-    int xoffset = 3;
-    int yoffset = 3;
     showboard (play, board, xoffset, yoffset, mode);
 
     int x = (play->xsize + 1)/ 2 - 1;
diff --git a/type.h b/type.h
new file mode 100644 (file)
index 0000000..a12df41
--- /dev/null
+++ b/type.h
@@ -0,0 +1,37 @@
+#ifndef __TYPE_H__
+#define __TYPE_H__
+
+typedef struct {
+    int xsize;
+    int ysize;
+    float factor;
+    int length;
+    int premium;
+    char *TW;
+    char *DW;
+    char *TL;
+    char *DL;
+    char *CS;
+} board_t;
+
+typedef struct {
+    char letter;
+    int value;
+} lettervalue_t;
+
+typedef struct {
+    int nbletters;
+    int nbtiles;
+    char *tiles;
+    lettervalue_t *lettervalues;
+} bag_t;
+
+typedef struct {
+    int xsize;
+    int ysize;
+    char *tab;
+} play_t;
+
+#endif /* __TYPE_H__ */
+
+/* vim: set ts=4 sw=4 et: */