]> git.cworth.org Git - wordgame/blobdiff - grid.c
Allow a new game to be started by pressing Enter after Control-C
[wordgame] / grid.c
diff --git a/grid.c b/grid.c
index acfee21831118ef1f1c28df7d975639b1fcee0fb..c68ae435353d4e09380cfbd46aa874a6cf91c7ec 100644 (file)
--- a/grid.c
+++ b/grid.c
@@ -86,6 +86,31 @@ grid_init (grid_t *grid, int 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 *
 grid_string (grid_t *grid)
 {
@@ -103,7 +128,7 @@ grid_string (grid_t *grid)
            else
                *s++ = ' ';
        }
-       if (y != 3)
+       if (y != (grid->size - 1))
            *s++ = '\n';
     }
     *s = '\0';
@@ -144,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);