]> git.cworth.org Git - wordgame/blobdiff - grid.c
Ensure that there's at least one full-length word that's not obscure
[wordgame] / grid.c
diff --git a/grid.c b/grid.c
index eac8488799f348e2e0862772b35ac9231f0ca100..c68ae435353d4e09380cfbd46aa874a6cf91c7ec 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,55 @@ 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);
 
-    for (i = 0; i < 16; i++)
+    grid->size = size;
+    num_cubes = size * size;
+
+    if (size == 4)
+       cubes_source = cubes4;
+    else 
+       cubes_source = cubes5;
+
+    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)];
+}
+
+void
+grid_set_letters (grid_t       *grid,
+                 const char    *letters)
+{
+    int i;
+    int num_cubes = grid->size * grid->size;
+    char letter;
+
+    if (strlen (letters) != num_cubes) {
+       fprintf (stderr, "Error: Invalid string for %dx%d grid. Expected %d letters: %s\n",
+                grid->size, grid->size, num_cubes, letters);
+       exit (1);
+    }
+
+    for (i = 0; i < num_cubes; i++) {
+       letter = tolower (letters[i]);
+       if (letter < 'a' || letter > 'z') {
+           fprintf (stderr, "Error: Invalid character '%c' in letters: %s\n",
+                    letters[i], letters);
+           exit (1);
+       }
+       grid->letters[i / grid->size][i % grid->size] = letter;
+    }
 }
 
 char *
@@ -69,8 +118,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);
@@ -79,7 +128,7 @@ grid_string (grid_t *grid)
            else
                *s++ = ' ';
        }
-       if (y != 3)
+       if (y != (grid->size - 1))
            *s++ = '\n';
     }
     *s = '\0';
@@ -87,12 +136,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 +151,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;
@@ -120,7 +169,10 @@ grid_enumerate (grid_t             *grid,
        dict_cursor = dict_cursor_next (dict_cursor, 'u');
     }
 
-    if (strlen (word) > 2 &&
+    /* For the 4x4 grid any word of length 3 or more counts.
+     * For the 5x5 grid any word of length 4 or more counts.
+     */
+    if (strlen (word) >= (grid->size - 1) &&
        DICT_ENTRY_IS_WORD (dict_cursor_resolve (dict_cursor)))
     {
        dict_add_word (grid->results, word);
@@ -139,14 +191,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));
 }