add multiple boards and bags
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Fri, 31 May 2024 17:04:14 +0000 (19:04 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Fri, 31 May 2024 17:04:14 +0000 (19:04 +0200)
14 files changed:
bag-de.h [new file with mode: 0644]
bag-en.h [new file with mode: 0644]
bag-fr.h [new file with mode: 0644]
board.c [deleted file]
board.h [deleted file]
board_15x15-7.h [new file with mode: 0644]
board_22x22-7.h [new file with mode: 0644]
board_8x15-7.h [new file with mode: 0644]
board_8x8-7.h [new file with mode: 0644]
constant.c
display.c
function.c [new file with mode: 0644]
function.h [new file with mode: 0644]
scrabble.c

diff --git a/bag-de.h b/bag-de.h
new file mode 100644 (file)
index 0000000..fca3c9e
--- /dev/null
+++ b/bag-de.h
@@ -0,0 +1,76 @@
+#ifndef __BAG_DE__
+#define __BAG_DE__
+
+#include "type.h"
+
+#define NB_TILES_DE 102
+static char _tiles_de[NB_TILES_DE] = {
+    '.', '.',
+    'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E',
+    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
+    'S', 'S', 'S', 'S', 'S', 'S', 'S',
+    'I', 'I', 'I', 'I', 'I', 'I',
+    'R', 'R', 'R', 'R', 'R', 'R',
+    'T', 'T', 'T', 'T', 'T', 'T',
+    'U', 'U', 'U', 'U', 'U', 'U',
+    'A', 'A', 'A', 'A', 'A',
+    'D', 'D', 'D', 'D',
+    'H', 'H', 'H', 'H',
+    'G', 'G', 'G',
+    'L', 'L', 'L',
+    'O', 'O', 'O',
+    'M', 'M', 'M', 'M',
+    'B', 'B',
+    'W',
+    'Z',
+    'C', 'C',
+    'F', 'F',
+    'K', 'K',
+    'P',
+    'Ä',
+    'J',
+    'Ü',
+    'V',
+    'Ö',
+    'X',
+    'Q',
+    'Y'
+};
+
+#define NB_LETTERVALUES_DE 30
+static lettervalue_t _lettervalues_de[NB_LETTERVALUES_DE] = {
+    {'.', 0},
+    {'E', 1},
+    {'N', 1},
+    {'S', 1},
+    {'I', 1},
+    {'R', 1},
+    {'T', 1},
+    {'U', 1},
+    {'A', 1},
+    {'D', 1},
+    {'H', 2},
+    {'G', 2},
+    {'L', 2},
+    {'O', 2},
+    {'M', 3},
+    {'B', 3},
+    {'W', 3},
+    {'Z', 3},
+    {'C', 4},
+    {'F', 4},
+    {'K', 4},
+    {'P', 4},
+    {'Ä', 6},
+    {'J', 6},
+    {'Ü', 6},
+    {'V', 6},
+    {'Ö', 8},
+    {'X', 8},
+    {'Q', 10},
+    {'Y', 10}
+};
+
+#endif /*  __BAG_DE__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/bag-en.h b/bag-en.h
new file mode 100644 (file)
index 0000000..3496962
--- /dev/null
+++ b/bag-en.h
@@ -0,0 +1,70 @@
+#ifndef __BAG_EN__
+#define __BAG_EN__
+
+#include "type.h"
+
+#define NB_TILES_EN 100
+static char _tiles_en[NB_TILES_EN] = {
+    '.', '.',
+    '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', 'I',
+    'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O',
+    'R', 'R', 'R', 'R', 'R', 'R',
+    'N', 'N', 'N', 'N', 'N', 'N',
+    'T', 'T', 'T', 'T', 'T', 'T',
+    'L', 'L', 'L', 'L',
+    'S', 'S', 'S', 'S',
+    'U', 'U', 'U', 'U',
+    'D', 'D', 'D', 'D',
+    'G', 'G', 'G',
+    'B', 'B',
+    'C', 'C',
+    'M', 'M',
+    'P', 'P',
+    'F', 'F',
+    'H', 'H',
+    'V', 'V',
+    'W', 'W',
+    'Y', 'Y',
+    'K',
+    'J',
+    'X',
+    'Q',
+    'Z'
+};
+
+#define NB_LETTERVALUES_EN 27
+static lettervalue_t _lettervalues_en[NB_LETTERVALUES_EN] = {
+    {'.', 0},
+    {'E', 1},
+    {'A', 1},
+    {'I', 1},
+    {'O', 1},
+    {'R', 1},
+    {'N', 1},
+    {'T', 1},
+    {'L', 1},
+    {'S', 1},
+    {'U', 1},
+    {'D', 2},
+    {'G', 2},
+    {'B', 3},
+    {'C', 3},
+    {'M', 3},
+    {'P', 3},
+    {'F', 4},
+    {'H', 4},
+    {'V', 4},
+    {'W', 4},
+    {'Y', 4},
+    {'K', 5},
+    {'J', 8},
+    {'X', 8},
+    {'Q', 10},
+    {'Z', 10}
+};
+
+#endif /*  __BAG_EN__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/bag-fr.h b/bag-fr.h
new file mode 100644 (file)
index 0000000..50d97aa
--- /dev/null
+++ b/bag-fr.h
@@ -0,0 +1,70 @@
+#ifndef __BAG_FR__
+#define __BAG_FR__
+
+#include "type.h"
+
+#define NB_TILES_FR 102
+static char _tiles_fr[NB_TILES_FR] = {
+    '.', '.',
+    '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'
+};
+
+#define NB_LETTERVALUES_FR 27
+static lettervalue_t _lettervalues_fr[NB_LETTERVALUES_FR] = {
+    {'.', 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}
+};
+
+#endif /*  __BAG_FR__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/board.c b/board.c
deleted file mode 100644 (file)
index 927106f..0000000
--- a/board.c
+++ /dev/null
@@ -1,132 +0,0 @@
-#include <curses.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "debug.h"
-#include "type.h"
-
-#include "board.h"
-
-int contains (char *list, char *str)
-{
-    while (*list) {
-        if (strcmp (list, str) == 0) {
-            return 1;
-        }
-        list += strlen (list) + 1;
-    }
-    return 0;
-}
-
-play_t *initplay (int xsize, int ysize)
-{
-    play_t *play = (play_t *) malloc (sizeof (play_t));
-    play->tab = (char *) malloc (xsize * ysize);
-    memset (play->tab, ' ', xsize * ysize);
-    play->xsize = xsize;
-    play->ysize = ysize;
-    return play;
-}
-
-int putletter (play_t *play, char letter, int x, int y)
-{
-    int ret = (letter != '\0') && (play->tab[x + play->xsize * y] == ' ');
-    if (ret) {
-        play->tab[x + play->xsize * y] = letter;
-    }
-    return ret;
-}
-
-void removeletter (play_t *play, int x, int y)
-{
-    play->tab[x + play->xsize * y] = ' ';
-}
-
-char gessletter (play_t *play, int x, int y)
-{
-    return play->tab[x + play->xsize * y];
-}
-
-draw_t *newdraw (int nbtiles)
-{
-    draw_t *draw = (draw_t *) malloc (sizeof (draw_t));
-    draw->tiles = (char *) calloc (1, nbtiles);
-    draw->nbtiles = nbtiles;
-    return draw;
-}
-
-draw_t *initdraw (bag_t *bag)
-{
-    draw_t *draw = newdraw (bag->nbtiles);
-    memcpy (draw->tiles, bag->tiles, bag->nbtiles);
-    return draw;
-}
-
-draw_t *retrievetiles (draw_t *draw, int nbtiles, draw_t *game)
-{
-    draw_t *ret = (game == NULL) ? newdraw (nbtiles) : game;
-    int i;
-    for (i = 0; i < ret->nbtiles; i++) {
-        if (ret->tiles[i] != '\0') {
-            draw->tiles[draw->nbtiles++] = ret->tiles[i];
-            ret->tiles[i] = '\0';
-        }
-    }
-    for (i = 0; (i < ret->nbtiles) && (i < draw->nbtiles); i++) {
-        int j = rand () % draw->nbtiles;
-        ret->tiles[i] = draw->tiles[j];
-        if (j < draw->nbtiles - 1) {
-            char *pt = draw->tiles;
-            memmove(pt + j, pt + j + 1, draw->nbtiles - j - 1);
-        }
-        draw->nbtiles--;
-        draw->tiles[draw->nbtiles] = '\0';
-    }
-    ret->nbtiles = i;
-    ret->index = -1;
-    return ret;
-}
-
-void freedraw (draw_t *draw)
-{
-    free (draw->tiles);
-    free (draw);
-}
-
-char getnextletter (draw_t *game)
-{
-    char letter = 0;
-    int i;
-    for (i = 0; i < game->nbtiles; i++) {
-        game->index = (game->index + 1) % game->nbtiles;
-        if (game->tiles[game->index] != '\0') {
-            letter = game->tiles[game->index];
-            break;
-        }
-    }
-    return letter;
-}
-
-char getnewletter (draw_t *game)
-{
-    if (game->index != -1) {
-        game->tiles[game->index] = '\0';
-    }
-    return getnextletter (game);
-}
-
-int returnletter (draw_t *game, char letter)
-{
-    int i, ret = 0;
-    for (i = 0; i < game->nbtiles; i++) {
-        if (game->tiles[i] == '\0') {
-            game->tiles[i] = letter;
-            game->index = i - 1;
-            ret = 1;
-            break;
-        }
-    }
-    return ret;
-}
-
-/* vim: set ts=4 sw=4 et: */
diff --git a/board.h b/board.h
deleted file mode 100644 (file)
index 05f4456..0000000
--- a/board.h
+++ /dev/null
@@ -1,32 +0,0 @@
-#ifndef __BOARD_H__
-#define __BOARD_H__
-
-#include "type.h"
-
-int contains (char *list, char *str);
-
-play_t *initplay (int xsize, int ysize);
-
-int putletter (play_t *play, char letter, int x, int y);
-
-void removeletter (play_t *play, int x, int y);
-
-char gessletter (play_t *play, int x, int y);
-
-draw_t *newdraw (int nbtiles);
-
-draw_t *initdraw (bag_t *bag);
-
-draw_t *retrievetiles (draw_t *draw, int nbtiles, draw_t *game);
-
-void freedraw (draw_t *draw);
-
-char getnextletter (draw_t *game);
-
-char getnewletter (draw_t *game);
-
-int returnletter (draw_t *game, char letter);
-
-#endif /* __BOARD_H__ */
-
-/* vim: set ts=4 sw=4 et: */
diff --git a/board_15x15-7.h b/board_15x15-7.h
new file mode 100644 (file)
index 0000000..81c231a
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef __15X15_7__
+#define __15X15_7__
+
+#include "type.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 */
+};
+
+#endif /* __15X15_7__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/board_22x22-7.h b/board_22x22-7.h
new file mode 100644 (file)
index 0000000..4fb6dd4
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef __22X22_7__
+#define __22X22_7__
+
+#include "type.h"
+
+static board_t _board_22x22_7 =
+{
+    22, 22, 2, 7, 50,
+    "A1\0A8\0A15\0A22\0H1\0H8\0H15\0H22\0O1\0O8\0O15\0O22\0V1\0V8\0V15\0V22\0",
+    "B2\0B10\0B21\0C3\0C20\0D4\0D12\0D19\0E5\0E18\0I9\0I14\0J21\0K4\0L12\0L19\0M2\0N9\0N14\0R5\0R18\0S4\0S11\0S19\0T3\0T20\0U2\0U13\0U21\0",
+    "B6\0B17\0E14\0F2\0F6\0F10\0F17\0F21\0I5\0J13\0J17\0M6\0M10\0N18\0Q2\0Q6\0Q13\0Q17\0Q21\0R9\0U6\0U17\0",
+    "A4\0A12\0A19\0B13\0C7\0C11\0C16\0D1\0D8\0D15\0D22\0E9\0F13\0G3\0G7\0G11\0G16\0G20\0H4\0H12\0H19\0I18\0J2\0J6\0J10\0K1\0K8\0K16\0K20\0L3\0L7\0L15\0L22\0M13\0M17\0M21\0N5\0O4\0O11\0O19\0P3\0P7\0P12\0P16\0P20\0Q10\0R14\0S1\0S8\0S15\0S22\0T7\0T12\0T16\0U10\0V4\0V11\0V19\0",
+    "K11\0"
+};
+
+#endif /* __22X22_7__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/board_8x15-7.h b/board_8x15-7.h
new file mode 100644 (file)
index 0000000..4e7a6fe
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef __8X15_7__
+#define __8X15_7__
+
+#include "type.h"
+
+static board_t _board_8x15_7 =
+{
+    15, 8, 0.6, 7, 50,
+    "A1\0A8\0A15\0H1\0H8\0H15\0",
+    "B2\0C3\0C10\0E9\0F6\0F13\0G14\0",
+    "B6\0B13\0C14\0D11\0E5\0F2\0G3\0G10\0",
+    "A5\0A12\0B4\0B9\0B11\0C8\0D2\0D4\0D15\0E1\0E12\0E14\0F8\0G5\0G7\0G12\0H4\0H11\0",
+    "D7\0"
+};
+
+#endif /* __8X15_7__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/board_8x8-7.h b/board_8x8-7.h
new file mode 100644 (file)
index 0000000..a951570
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef __8X8_7__
+#define __8X8_7__
+
+#include "type.h"
+
+static board_t _board_8x8_7 =
+{
+    8, 8, 0.3, 7, 50,
+    "A1\0A8\0H1\0H8\0",
+    "B2\0B7\0E5\0G2\0G7\0",
+    "B5\0E2\0D7\0G4\0",
+    "A4\0C3\0C6\0D1\0E8\0F3\0F6\0H5\0",
+    "D4\0"
+};
+
+#endif /* __8X8_7__ */
+
+/* vim: set ts=4 sw=4 et: */
index 798be5a1957876f43f863d438c711c18d1c93cf8..390ec7dfee789754011f25e2639908f9b96dd28e 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-#include "board.h"
+#include "type.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 */
-};
-
-#define NB_TILES_FR 102
-static char _tiles_fr[NB_TILES_FR] = {
-    '.', '.',
-    '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'
-};
-
-#define NB_LETTERVALUES_FR 27
-static lettervalue_t _lettervalues_fr[NB_LETTERVALUES_FR] = {
-    {'.', 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};
+#include "board_8x8-7.h"
+#include "board_8x15-7.h"
+#include "board_15x15-7.h"
+#include "board_22x22-7.h"
 
 board_t *getboard (char *name)
 {
     board_t *pt = NULL;
 
-    if (strcmp (name, "15x15-7") == 0) {
+    if (strcmp (name, "8x8-7") == 0) {
+        pt = &_board_8x8_7;
+    } else if (strcmp (name, "8x15-7") == 0) {
+        pt = &_board_8x15_7;
+    } else if (strcmp (name, "15x15-7") == 0) {
         pt = &_board_15x15_7;
+    } else if (strcmp (name, "22x22-7") == 0) {
+        pt = &_board_22x22_7;
     }
 
     return pt;
 }
 
+#include "bag-de.h"
+#include "bag-en.h"
+#include "bag-fr.h"
+
+static bag_t _bag = {0};
+
 bag_t *getbag (char *lang)
 {
     bag_t *pt = NULL;
 
-    if (strcmp (lang, "fr") == 0) {
+    if (strcmp (lang, "de") == 0) {
+        pt = &_bag;
+        pt->nbtiles = NB_TILES_DE;
+        pt->tiles = _tiles_de;
+        pt->nbletters = NB_LETTERVALUES_DE;
+        pt->lettervalues = _lettervalues_de;
+    } else if (strcmp (lang, "en") == 0) {
+        pt = &_bag;
+        pt->nbtiles = NB_TILES_EN;
+        pt->tiles = _tiles_en;
+        pt->nbletters = NB_LETTERVALUES_EN;
+        pt->lettervalues = _lettervalues_en;
+    } else if (strcmp (lang, "fr") == 0) {
         pt = &_bag;
         pt->nbtiles = NB_TILES_FR;
         pt->tiles = _tiles_fr;
index a7e2d64fdbd27d911521a9fb985006bb792ba46d..db14c788a50965843d3ff58ce8fbee3bd468d55c 100644 (file)
--- a/display.c
+++ b/display.c
@@ -1,8 +1,9 @@
 #include <curses.h>
 #include <stdlib.h>
 
-#include "board.h"
 #include "debug.h"
+#include "function.h"
+#include "type.h"
 
 #include "display.h"
 
@@ -64,7 +65,7 @@ void showboard (play_t *play, board_t *board, int xoffset, int yoffset, int full
         for (y = -1; y <= board->ysize; y++) {
             int c = ' ';
             char pos[12] = {0};
-            sprintf (pos, "%c%d", 'A' + x, 1 + y);
+            sprintf (pos, "%c%d", 'A' + y, 1 + x);
             if ((x == -1) && (y == -1)) {
                 c = ACS_ULCORNER;
                 set_color (black);
@@ -80,7 +81,7 @@ void showboard (play_t *play, board_t *board, int xoffset, int yoffset, int full
             } else if ((x == -1) || (x == board->xsize)) {
                 c = ACS_VLINE;
                 set_color (black);
-            } else if ((y == -1) || (y == board->xsize)) {
+            } else if ((y == -1) || (y == board->ysize)) {
                 c = ACS_HLINE;
                 set_color (black);
             } else {
diff --git a/function.c b/function.c
new file mode 100644 (file)
index 0000000..866a3a8
--- /dev/null
@@ -0,0 +1,132 @@
+#include <curses.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "debug.h"
+#include "type.h"
+
+#include "function.h"
+
+int contains (char *list, char *str)
+{
+    while (*list) {
+        if (strcmp (list, str) == 0) {
+            return 1;
+        }
+        list += strlen (list) + 1;
+    }
+    return 0;
+}
+
+play_t *initplay (int xsize, int ysize)
+{
+    play_t *play = (play_t *) malloc (sizeof (play_t));
+    play->tab = (char *) malloc (xsize * ysize);
+    memset (play->tab, ' ', xsize * ysize);
+    play->xsize = xsize;
+    play->ysize = ysize;
+    return play;
+}
+
+int putletter (play_t *play, char letter, int x, int y)
+{
+    int ret = (letter != '\0') && (play->tab[x + play->xsize * y] == ' ');
+    if (ret) {
+        play->tab[x + play->xsize * y] = letter;
+    }
+    return ret;
+}
+
+void removeletter (play_t *play, int x, int y)
+{
+    play->tab[x + play->xsize * y] = ' ';
+}
+
+char gessletter (play_t *play, int x, int y)
+{
+    return play->tab[x + play->xsize * y];
+}
+
+draw_t *newdraw (int nbtiles)
+{
+    draw_t *draw = (draw_t *) malloc (sizeof (draw_t));
+    draw->tiles = (char *) calloc (1, nbtiles);
+    draw->nbtiles = nbtiles;
+    return draw;
+}
+
+draw_t *initdraw (bag_t *bag)
+{
+    draw_t *draw = newdraw (bag->nbtiles);
+    memcpy (draw->tiles, bag->tiles, bag->nbtiles);
+    return draw;
+}
+
+draw_t *retrievetiles (draw_t *draw, int nbtiles, draw_t *game)
+{
+    draw_t *ret = (game == NULL) ? newdraw (nbtiles) : game;
+    int i;
+    for (i = 0; i < ret->nbtiles; i++) {
+        if (ret->tiles[i] != '\0') {
+            draw->tiles[draw->nbtiles++] = ret->tiles[i];
+            ret->tiles[i] = '\0';
+        }
+    }
+    for (i = 0; (i < ret->nbtiles) && (i < draw->nbtiles); i++) {
+        int j = rand () % draw->nbtiles;
+        ret->tiles[i] = draw->tiles[j];
+        if (j < draw->nbtiles - 1) {
+            char *pt = draw->tiles;
+            memmove(pt + j, pt + j + 1, draw->nbtiles - j - 1);
+        }
+        draw->nbtiles--;
+        draw->tiles[draw->nbtiles] = '\0';
+    }
+    ret->nbtiles = i;
+    ret->index = -1;
+    return ret;
+}
+
+void freedraw (draw_t *draw)
+{
+    free (draw->tiles);
+    free (draw);
+}
+
+char getnextletter (draw_t *game)
+{
+    char letter = 0;
+    int i;
+    for (i = 0; i < game->nbtiles; i++) {
+        game->index = (game->index + 1) % game->nbtiles;
+        if (game->tiles[game->index] != '\0') {
+            letter = game->tiles[game->index];
+            break;
+        }
+    }
+    return letter;
+}
+
+char getnewletter (draw_t *game)
+{
+    if (game->index != -1) {
+        game->tiles[game->index] = '\0';
+    }
+    return getnextletter (game);
+}
+
+int returnletter (draw_t *game, char letter)
+{
+    int i, ret = 0;
+    for (i = 0; i < game->nbtiles; i++) {
+        if (game->tiles[i] == '\0') {
+            game->tiles[i] = letter;
+            game->index = i - 1;
+            ret = 1;
+            break;
+        }
+    }
+    return ret;
+}
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/function.h b/function.h
new file mode 100644 (file)
index 0000000..736c84a
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef __FUNCTION_H__
+#define __FUNCTION_H__
+
+#include "type.h"
+
+int contains (char *list, char *str);
+
+play_t *initplay (int xsize, int ysize);
+
+int putletter (play_t *play, char letter, int x, int y);
+
+void removeletter (play_t *play, int x, int y);
+
+char gessletter (play_t *play, int x, int y);
+
+draw_t *newdraw (int nbtiles);
+
+draw_t *initdraw (bag_t *bag);
+
+draw_t *retrievetiles (draw_t *draw, int nbtiles, draw_t *game);
+
+void freedraw (draw_t *draw);
+
+char getnextletter (draw_t *game);
+
+char getnewletter (draw_t *game);
+
+int returnletter (draw_t *game, char letter);
+
+#endif /* __FUNCTION_H__ */
+
+/* vim: set ts=4 sw=4 et: */
index 7eb2aa91f379a994c0db550c709dd514a21114a2..206a1c9498f32202c652a52268db35fbf69c2d6a 100644 (file)
@@ -1,22 +1,24 @@
 /* depend: */
 /* cflags: */
-/* 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 */
+/* linker: constant.o debug.o display.o function.o -lcurses */
+/* winlnk: constant.o debug.o display.o function.o -lpdcurses */
 
 #include <curses.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 
-#include "board.h"
 #include "constant.h"
-#include "display.h"
 #include "debug.h"
+#include "display.h"
+#include "function.h"
+#include "type.h"
 
 #define KEY_ESC 0x1b
 #define KEY_DELETE 0x014a
 
+#define max(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
+
 /* static variables */
 char *progname = NULL;
 char *version = "0.1";
@@ -135,8 +137,15 @@ int main (int argc, char *argv[])
 
     start_color ();
 
-    valuewindow (bag, 2 * xoffset + board->xsize, yoffset - 1, 10);
-    helpwindow (help, 3 * xoffset + board->xsize + 10, yoffset - 1, 30);
+    int xvaluewin = 2 * xoffset + max (board->xsize, board->length * 2 -1);
+    int yvaluewin = yoffset - 1;
+    int xhelpwin = xvaluewin + xoffset + 10;
+    int yhelpwin = yoffset - 1;
+    int xdrawwin = xoffset;
+    int ydrawwin = 2 * yoffset + max (board->ysize, 1 + (bag->nbletters + 1) /2);
+
+    valuewindow (bag, xvaluewin, yvaluewin, 10);
+    helpwindow (help, xhelpwin, yhelpwin, 30);
 
     int mode = 0;
     showboard (play, board, xoffset, yoffset, mode);
@@ -145,7 +154,7 @@ int main (int argc, char *argv[])
     int y = (play->ysize + 1)/ 2 - 1;
 
     char current, letter = getnextletter (game);
-    drawwindow (game, xoffset, 2 * yoffset + board->ysize, board->length);
+    drawwindow (game, xdrawwin, ydrawwin, board->length);
     int stop = 0;
     while (!stop) {
         current = gessletter (play, x, y);
@@ -159,7 +168,7 @@ int main (int argc, char *argv[])
         case '\t':
         case 'c':
             letter = getnextletter (game);
-            drawwindow (game, xoffset, 2 * yoffset + board->ysize, board->length);
+            drawwindow (game, xdrawwin, ydrawwin, board->length);
             break;
         case KEY_ENTER:
         case '\n':
@@ -167,7 +176,7 @@ int main (int argc, char *argv[])
         case 'd':
             game = retrievetiles (draw, board->length, game);
             letter = getnextletter (game);
-            drawwindow (game, xoffset, 2 * yoffset + board->ysize, board->length);
+            drawwindow (game, xdrawwin, ydrawwin, board->length);
             break;
         case KEY_UP:
         case 'i':
@@ -204,7 +213,7 @@ int main (int argc, char *argv[])
         case 'v':
             if (putletter (play, letter, x, y)) {
                 letter = getnewletter (game);
-                drawwindow (game, xoffset, 2 * yoffset + board->ysize, board->length);
+                drawwindow (game, xdrawwin, ydrawwin, board->length);
             }
             break;
         case KEY_BACKSPACE:
@@ -217,7 +226,7 @@ int main (int argc, char *argv[])
                 if (returnletter (game, current)) {
                     removeletter (play, x, y);
                     letter = getnextletter (game);
-                    drawwindow (game, xoffset, 2 * yoffset + board->ysize, board->length);
+                    drawwindow (game, xdrawwin, ydrawwin, board->length);
                 }
             }
             break;