]> 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 fe3d5402d2b52fbcdfb8a49f51e0ffde2da949ce..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 _board {
-    char letters[4][4];
-
-    /* Private, transient state used by enumerate */
-    dict_t *results;
-} board_t;
-
 static int
 rand_within (int num_values)
 {
@@ -57,8 +48,8 @@ shuffle (int *array, int length)
     }
 }
 
-static void
-board_init (board_t *board)
+void
+grid_init (grid_t *grid)
 {
     int i;
     int cubes[16];
@@ -68,28 +59,19 @@ board_init (board_t *board)
     shuffle (cubes, 16);
  
     for (i = 0; i < 16; i++)
-       board->letters[i / 4][i % 4] = cube_faces[cubes[i]][rand_within(6)];
+       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 board
- *   + 1 terminator character
- * = 53
- */
-#define BOARD_STRING_MAX 53
-static void
-board_to_string (board_t       *board,
-                char            board_string[BOARD_STRING_MAX])
+char *
+grid_string (grid_t *grid)
 {
     char c;
     int x, y;
-    char *s = &board_string[0];
+    char *s = grid->string;
 
     for (y = 0; y < 4; y++) {
        for (x = 0; x < 4; x++) {
-           c = board->letters[y][x];
+           c = grid->letters[y][x];
            *s++ = ' ';
            *s++ = toupper (c);
            if (c == 'q')
@@ -101,16 +83,18 @@ board_to_string (board_t   *board,
            *s++ = '\n';
     }
     *s = '\0';
+
+    return grid->string;
 }
 
 #define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
 static void
-board_enumerate (board_t       *board,
-                int             x,
-                int             y,
-                int16_t         seen,
-                char           *word,
-                dict_cursor_t   dict_cursor)
+grid_enumerate (grid_t         *grid,
+               int              x,
+               int              y,
+               int16_t          seen,
+               char            *word,
+               dict_cursor_t    dict_cursor)
 {
     char c;
     int dx, dy;
@@ -127,7 +111,7 @@ board_enumerate (board_t    *board,
 
     seen |= SEEN_BIT (x, y);
 
-    c = board->letters[y][x];
+    c = grid->letters[y][x];
     word[strlen (word)] = c;
     dict_cursor = dict_cursor_next (dict_cursor, c);
 
@@ -139,59 +123,30 @@ board_enumerate (board_t  *board,
     if (strlen (word) > 2 &&
        DICT_ENTRY_IS_WORD (dict_cursor_resolve (dict_cursor)))
     {
-       dict_add_word (board->results, word);
+       dict_add_word (grid->results, word);
     }
 
     for (dy = -1; dy <= 1; dy++)
        for (dx = -1; dx <= 1; dx++)
-           board_enumerate (board, x + dx, y + dy, seen, word, dict_cursor);
+           grid_enumerate (grid, x + dx, y + dy, seen, word, dict_cursor);
 
     if (c == 'q')
        word [strlen (word) - 1] = '\0';
     word [strlen (word) - 1] = '\0';
 }
 
-static void
-board_solve (board_t *board, dict_t *dict, dict_t *solution)
+void
+grid_solve (grid_t *grid, dict_t *dict, dict_t *solution)
 {
     int x, y;
     int16_t seen = 0;
     char word[18];
 
-    board->results = solution;
+    grid->results = solution;
 
     memset (word, '\0', 18);
 
     for (y = 0; y < 4; y++)
        for (x = 0; x < 4; x++)
-           board_enumerate (board, x, y, seen, word, dict_root (dict));
-}
-
-#define GAME_LENGTH (3 * 60)
-int
-main (void)
-{
-    dict_t dict, solution;
-    board_t board;
-    char board_string[BOARD_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");
-
-    board_init (&board);
-    board_to_string (&board, board_string);
-
-    dict_init (&solution);
-    board_solve (&board, &dict, &solution);
-
-    word_game_play (board_string, &dict, &solution, GAME_LENGTH);
-
-    dict_fini (&solution);
-    dict_fini (&dict);
-
-    return 0;
+           grid_enumerate (grid, x, y, seen, word, dict_root (dict));
 }