split code
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 27 May 2024 17:03:24 +0000 (19:03 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 27 May 2024 17:03:24 +0000 (19:03 +0200)
board.c
board.h
constant.c [new file with mode: 0644]
constant.h [new file with mode: 0644]
scrabble.c

diff --git a/board.c b/board.c
index 7a843b4a7393275c64e317fd9b0f08cfd0c3b055..af307de3817dbe45eb902806c468774a661cf493 100644 (file)
--- a/board.c
+++ b/board.c
@@ -2,36 +2,9 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "board.h"
 #include "debug.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;
-
-board_t _board =
-{
-    15, /* xsize */
-    15, /* ysize */
-    1, /* factor */
-    7, /* length */
-    50, /* premium */
-    "A1\0A8\0A15\0H1\0H15\0O1\0O8\0O15\0", /* TW */
-    "B2\0B14\0C3\0C13\0D4\0D12\0E5\0E11\0K5\0K11\0L4\0L12\0M3\0M13\0N2\0N14\0", /* DW */
-    "B6\0B10\0F2\0F6\0F10\0F14\0J2\0J6\0J10\0J14\0N6\0N10\0", /* TL */
-    "A4\0A12\0C7\0C9\0D1\0D8\0D15\0G3\0G7\0G9\0G13\0H4\0H12\0I3\0I7\0I9\0I13\0L1\0L8\0L15\0M7\0M9\0O4\0O12\0", /* DL */
-    "H8\0" /* CS */
-};
-board_t *board = &_board;
+#include "board.h"
 
 int contains (char *list, char *str)
 {
@@ -74,13 +47,13 @@ void set_color (color_t color)
     attrset (COLOR_PAIR(color));
 }
 
-play_t *initplay (void)
+play_t *initplay (int xsize, int ysize)
 {
     play_t *play = (play_t *) malloc (sizeof (play_t));
-    play->tab = (char *) malloc (board->xsize * board->ysize);
-    memset (play->tab, ' ', board->xsize * board->ysize);
-    play->xsize = board->xsize;
-    play->ysize = board->ysize;
+    play->tab = (char *) malloc (xsize * ysize);
+    memset (play->tab, ' ', xsize * ysize);
+    play->xsize = xsize;
+    play->ysize = ysize;
     return play;
 }
 
@@ -100,7 +73,7 @@ void showletter (char letter, int x, int y)
     set_color (white);
 }
 
-void showboard (play_t *play, int xoffset, int yoffset, int full)
+void showboard (play_t *play, board_t *board, int xoffset, int yoffset, int full)
 {
     int x, y;
     for (x = -1; x <= board->xsize; x++) {
diff --git a/board.h b/board.h
index 546b59d4ecd8f2e0ce49c48b229c7bded2ecfea9..4f60f99ea06baee8b8430121d9340c96e44cad09 100644 (file)
--- a/board.h
+++ b/board.h
@@ -1,13 +1,15 @@
 #ifndef __BOARD_H__
 #define __BOARD_H__
 
+#include "constant.h"
+
 typedef struct {
     int xsize;
     int ysize;
     char *tab;
 } play_t;
 
-char *initboard (void);
+char *initboard (int xsize, int ysize);
 
 play_t *initplay ();
 
@@ -15,7 +17,7 @@ int putletter (play_t *play, char letter, int x, int y);
 
 void showletter (char letter, int x, int y);
 
-void showboard (play_t *play, int xoffset, int yoffset, int full);
+void showboard (play_t *play, board_t *board, int xoffset, int yoffset, int full);
 
 #endif /* __BOARD_H__ */
 
diff --git a/constant.c b/constant.c
new file mode 100644 (file)
index 0000000..6f9345e
--- /dev/null
@@ -0,0 +1,110 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include "board.h"
+
+#include "constant.h"
+
+static board_t _board_15x15_7 =
+{
+    15, /* xsize */
+    15, /* ysize */
+    1, /* factor */
+    7, /* length */
+    50, /* premium */
+    "A1\0A8\0A15\0H1\0H15\0O1\0O8\0O15\0", /* TW */
+    "B2\0B14\0C3\0C13\0D4\0D12\0E5\0E11\0K5\0K11\0L4\0L12\0M3\0M13\0N2\0N14\0", /* DW */
+    "B6\0B10\0F2\0F6\0F10\0F14\0J2\0J6\0J10\0J14\0N6\0N10\0", /* TL */
+    "A4\0A12\0C7\0C9\0D1\0D8\0D15\0G3\0G7\0G9\0G13\0H4\0H12\0I3\0I7\0I9\0I13\0L1\0L8\0L15\0M7\0M9\0O4\0O12\0", /* DL */
+    "H8\0" /* CS */
+};
+
+static char _tiles_fr[106] = {
+    ' ', ' ',
+    'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E',
+    'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
+    'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I',
+    'N', 'N', 'N', 'N', 'N', 'N',
+    'O', 'O', 'O', 'O', 'O', 'O',
+    'R', 'R', 'R', 'R', 'R', 'R',
+    'S', 'S', 'S', 'S', 'S', 'S',
+    'T', 'T', 'T', 'T', 'T', 'T',
+    'U', 'U', 'U', 'U', 'U', 'U',
+    'L', 'L', 'L', 'L', 'L',
+    'D', 'D', 'D',
+    'M', 'M', 'M',
+    'G', 'G',
+    'B', 'B',
+    'C', 'C',
+    'P', 'P',
+    'F', 'F',
+    'H', 'H',
+    'V', 'V',
+    'J',
+    'Q',
+    'K',
+    'W',
+    'X',
+    'Y',
+    'Z'
+};
+
+static lettervalue_t _lettervalues_fr[27] = {
+    {' ', 0},
+    {'E', 1},
+    {'A', 1},
+    {'I', 1},
+    {'N', 1},
+    {'O', 1},
+    {'R', 1},
+    {'S', 1},
+    {'T', 1},
+    {'U', 1},
+    {'L', 1},
+    {'D', 2},
+    {'M', 2},
+    {'G', 2},
+    {'B', 3},
+    {'C', 3},
+    {'P', 3},
+    {'F', 4},
+    {'H', 4},
+    {'V', 4},
+    {'J', 8},
+    {'Q', 8},
+    {'K', 10},
+    {'W', 10},
+    {'X', 10},
+    {'Y', 10},
+    {'Z', 10}
+};
+
+static bag_t _bag = {0};
+
+board_t *getboard (char *name)
+{
+    board_t *pt = NULL;
+
+    if (strcmp (name, "15x15-7") == 0) {
+        pt = &_board_15x15_7;
+    }
+
+    return pt;
+}
+
+bag_t *getbag (char *lang)
+{
+    bag_t *pt = NULL;
+
+    if (strcmp (lang, "fr") == 0) {
+        pt = &_bag;
+        pt->nbtiles = sizeof (_tiles_fr);
+        pt->tiles = _tiles_fr;
+        pt->nbletters = 27;
+        pt->lettervalues = _lettervalues_fr;
+    }
+
+    return pt;
+}
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/constant.h b/constant.h
new file mode 100644 (file)
index 0000000..3a202f9
--- /dev/null
@@ -0,0 +1,37 @@
+#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;
+
+board_t *getboard (char *name);
+
+bag_t *getbag (char *lang);
+
+#endif /* __CONSTANT_H__ */
+
+/* vim: set ts=4 sw=4 et: */
index 69c0cc2e912fd063aa3bb8514a1b15a66b8e502f..4819e8cb7458c3075d5d5392fba4726867a0c0a4 100644 (file)
@@ -1,13 +1,15 @@
 /* depend: */
 /* cflags: */
-/* linker: board.o debug.o -lcurses */
-/* winlnk: board.o debug.o -lpdcurses */
+/* linker: board.o constant.o debug.o -lcurses */
+/* winlnk: board.o constant.o debug.o -lpdcurses */
+/* winxxlnk: board.o constant.o debug.o -lncurses */
 
 #include <curses.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include "board.h"
+#include "constant.h"
 #include "debug.h"
 
 #define KEY_ESC 0x1b
@@ -92,6 +94,10 @@ int main (int argc, char *argv[])
         }
     }
 
+    board_t *board = getboard ("15x15-7");
+    play_t *play = initplay (board->xsize, board->ysize);
+    VERBOSE (DEBUG, printf ("play: 0x%p\nboard: 0x%p\n", play, board));
+
     initscr();
     //seed = time((time_t *)0);
     //srand(seed);
@@ -105,12 +111,10 @@ int main (int argc, char *argv[])
 
     help_window (50, 1, 30);
 
-    play_t *play = initplay ();
-
     int mode = 0;
     int xoffset = 3;
     int yoffset = 3;
-    showboard (play, xoffset, yoffset, mode);
+    showboard (play, board, xoffset, yoffset, mode);
 
     int x = (play->xsize + 1)/ 2 - 1;
     int y = (play->ysize + 1)/ 2 - 1;
@@ -162,7 +166,7 @@ int main (int argc, char *argv[])
         case ERR:
         default:
         }
-        showboard (play, xoffset, yoffset, mode);
+        showboard (play, board, xoffset, yoffset, mode);
     }
 
     endwin ();