]> git.cworth.org Git - wordgame/blobdiff - grid.c
grid: Generalize to allow either 4x4 or 5x5 grid.
[wordgame] / grid.c
diff --git a/grid.c b/grid.c
index eac8488799f348e2e0862772b35ac9231f0ca100..acfee21831118ef1f1c28df7d975639b1fcee0fb 100644 (file)
--- a/grid.c
+++ b/grid.c
 
 #include <string.h>
 #include <ctype.h>
+#include <assert.h>
 
-char *cube_faces[16] = {
+#define GRID_CUBES_MAX (GRID_SIZE_MAX * GRID_SIZE_MAX)
+
+char *cubes4[16] = {
     "aaeeng", "abbjoo", "achops", "affkps",
     "aoottw", "cimotu", "deilrx", "delrvy",
     "distty", "eeghnw", "eeinsu", "ehrtvw",
     "eiosst", "elrtty", "himnqu", "hlnnrz"
 };
 
+char *cubes5[25] = {
+    "aaafrs", "aaeeee", "aafirs", "adennn", "aeeeem",
+    "aeegmu", "aegmnn", "afirsy", "bjkqxz", "ccnstw",
+    "ceiilt", "ceilpt", "ceipst", "ddlnor", "dhhlor",
+    "dhhnot", "dhlnor", "eiiitt", "emottt", "ensssu",
+    "fiprsy", "gorrvw", "hiprry", "nootuw", "ooottu"
+};
+
 static int
 rand_within (int num_values)
 {
@@ -49,17 +60,30 @@ shuffle (int *array, int length)
 }
 
 void
-grid_init (grid_t *grid)
+grid_init (grid_t *grid, int size)
 {
     int i;
-    int cubes[16];
+    int cubes[GRID_CUBES_MAX];
+    char **cubes_source;
+    int num_cubes;
+
+    assert (size == 4 || size == 5);
+
+    grid->size = size;
+    num_cubes = size * size;
+
+    if (size == 4)
+       cubes_source = cubes4;
+    else 
+       cubes_source = cubes5;
 
-    for (i = 0; i < 16; i++)
+    for (i = 0; i < num_cubes; i++)
        cubes[i] = i;
-    shuffle (cubes, 16);
+    shuffle (cubes, num_cubes);
  
-    for (i = 0; i < 16; i++)
-       grid->letters[i / 4][i % 4] = cube_faces[cubes[i]][rand_within(6)];
+    for (i = 0; i < num_cubes; i++)
+       grid->letters[i / grid->size][i % grid->size] =
+           cubes_source[cubes[i]][rand_within(6)];
 }
 
 char *
@@ -69,8 +93,8 @@ grid_string (grid_t *grid)
     int x, y;
     char *s = grid->string;
 
-    for (y = 0; y < 4; y++) {
-       for (x = 0; x < 4; x++) {
+    for (y = 0; y < grid->size; y++) {
+       for (x = 0; x < grid->size; x++) {
            c = grid->letters[y][x];
            *s++ = ' ';
            *s++ = toupper (c);
@@ -87,12 +111,12 @@ grid_string (grid_t *grid)
     return grid->string;
 }
 
-#define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
+#define SEEN_BIT(x, y) (1 << ((grid->size)*(y)+(x)))
 static void
 grid_enumerate (grid_t         *grid,
                int              x,
                int              y,
-               int16_t          seen,
+               int32_t          seen,
                char            *word,
                dict_cursor_t    dict_cursor)
 {
@@ -102,8 +126,8 @@ grid_enumerate (grid_t              *grid,
     if (dict_cursor == DICT_CURSOR_NIL)
        return;
 
-    if (x < 0 || x >= 4 ||
-       y < 0 || y >= 4 ||
+    if (x < 0 || x >= grid->size ||
+       y < 0 || y >= grid->size ||
        seen & SEEN_BIT (x, y))
     {
        return;
@@ -139,14 +163,14 @@ void
 grid_solve (grid_t *grid, dict_t *dict, dict_t *solution)
 {
     int x, y;
-    int16_t seen = 0;
+    int32_t seen = 0;
     char word[18];
 
     grid->results = solution;
 
     memset (word, '\0', 18);
 
-    for (y = 0; y < 4; y++)
-       for (x = 0; x < 4; x++)
+    for (y = 0; y < grid->size; y++)
+       for (x = 0; x < grid->size; x++)
            grid_enumerate (grid, x, y, seen, word, dict_root (dict));
 }