]> git.cworth.org Git - wordgame/blobdiff - grid.c
Split grid into main program in grid4.c and library functions in grid.c
[wordgame] / grid.c
diff --git a/grid.c b/grid.c
index 8f30a1b9082ab8f82da1580cca146440a5c7767c..eac8488799f348e2e0862772b35ac9231f0ca100 100644 (file)
--- a/grid.c
+++ b/grid.c
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
  */
 
-#include "word-game.h"
+#include "grid.h"
 
 #include <string.h>
 #include <ctype.h>
-#include <sys/time.h>
-#include <time.h>
 
 char *cube_faces[16] = {
     "aaeeng", "abbjoo", "achops", "affkps",
@@ -30,13 +28,6 @@ char *cube_faces[16] = {
     "eiosst", "elrtty", "himnqu", "hlnnrz"
 };
 
-typedef struct _grid {
-    char letters[4][4];
-
-    /* Private, transient state used by enumerate */
-    dict_t *results;
-} grid_t;
-
 static int
 rand_within (int num_values)
 {
@@ -57,7 +48,7 @@ shuffle (int *array, int length)
     }
 }
 
-static void
+void
 grid_init (grid_t *grid)
 {
     int i;
@@ -71,21 +62,12 @@ grid_init (grid_t *grid)
        grid->letters[i / 4][i % 4] = cube_faces[cubes[i]][rand_within(6)];
 }
 
-/* (  3 chars per cell
- *  x 4 cells per row
- *  + 1 newline per row
- * ) x 4 rows per grid
- *   + 1 terminator character
- * = 53
- */
-#define GRID_STRING_MAX 53
-static void
-grid_to_string (grid_t *grid,
-               char     grid_string[GRID_STRING_MAX])
+char *
+grid_string (grid_t *grid)
 {
     char c;
     int x, y;
-    char *s = &grid_string[0];
+    char *s = grid->string;
 
     for (y = 0; y < 4; y++) {
        for (x = 0; x < 4; x++) {
@@ -101,6 +83,8 @@ grid_to_string (grid_t       *grid,
            *s++ = '\n';
     }
     *s = '\0';
+
+    return grid->string;
 }
 
 #define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
@@ -151,7 +135,7 @@ grid_enumerate (grid_t              *grid,
     word [strlen (word) - 1] = '\0';
 }
 
-static void
+void
 grid_solve (grid_t *grid, dict_t *dict, dict_t *solution)
 {
     int x, y;
@@ -166,32 +150,3 @@ grid_solve (grid_t *grid, dict_t *dict, dict_t *solution)
        for (x = 0; x < 4; x++)
            grid_enumerate (grid, x, y, seen, word, dict_root (dict));
 }
-
-#define GAME_LENGTH (3 * 60)
-int
-main (void)
-{
-    dict_t dict, solution;
-    grid_t grid;
-    char grid_string[GRID_STRING_MAX];
-    struct timeval tv;
-
-    gettimeofday (&tv, NULL);
-    srand (tv.tv_sec ^ tv.tv_usec);
-
-    dict_init (&dict);
-    dict_add_words_from_file (&dict, "words.txt");
-
-    grid_init (&grid);
-    grid_to_string (&grid, grid_string);
-
-    dict_init (&solution);
-    grid_solve (&grid, &dict, &solution);
-
-    word_game_play (grid_string, &dict, &solution, GAME_LENGTH);
-
-    dict_fini (&solution);
-    dict_fini (&dict);
-
-    return 0;
-}