]> git.cworth.org Git - wordgame/commitdiff
Begin to wean grid.c away from trie and toward dict.
authorCarl Worth <cworth@raht.cworth.org>
Thu, 21 Sep 2006 06:59:59 +0000 (23:59 -0700)
committerCarl Worth <cworth@raht.cworth.org>
Thu, 21 Sep 2006 06:59:59 +0000 (23:59 -0700)
dict.c
grid.c

diff --git a/dict.c b/dict.c
index a783ffcef36809a732f1b2789314ed838ae7891c..00d54e04036b79576a4fa2dc155853ee9d663864 100644 (file)
--- a/dict.c
+++ b/dict.c
@@ -247,6 +247,13 @@ dict_fini (dict_t *dict)
     trie_fini (dict);
 }
 
+void
+dict_add_word (dict_t          *dict,
+              const char       *word)
+{
+    trie_add (dict, word);
+}
+
 void
 dict_add_words_from_file (dict_t       *dict,
                          const char    *filename)
@@ -268,7 +275,7 @@ dict_add_words_from_file (dict_t    *dict,
        if (bytes_read == -1)
            break;
        chomp (line);
-       trie_add (dict, line);
+       dict_add_word (dict, line);
     }
     if (line)
        free (line);
diff --git a/grid.c b/grid.c
index c7858080b1fa84d473392b000f9d1fbb5369fe89..9b0ffbe7eeb30896af40c006dae551373463a70d 100644 (file)
--- a/grid.c
+++ b/grid.c
@@ -39,7 +39,7 @@ typedef struct _board {
     char letters[4][4];
 
     /* Private, transient state used by enumerate */
-    dict_t *result_trie;
+    dict_t *results;
 } board_t;
 
 int
@@ -129,7 +129,7 @@ board_enumerate (board_t    *board,
     }
 
     if (dict_trie->flags & TRIE_FLAGS_IS_WORD)
-       trie_add (board->result_trie, word->s);
+       dict_add_word (board->results, word->s);
 
     for (dy = -1; dy <= 1; dy++)
        for (dx = -1; dx <= 1; dx++)
@@ -143,13 +143,13 @@ board_enumerate (board_t  *board,
 }
 
 void
-board_solve (board_t *board, dict_t *dict, trie_t *solution)
+board_solve (board_t *board, dict_t *dict, dict_t *solution)
 {
     int x, y;
     int16_t seen = 0;
     string_t word;
 
-    board->result_trie = solution;
+    board->results = solution;
 
     string_init (&word);
 
@@ -164,9 +164,8 @@ board_solve (board_t *board, dict_t *dict, trie_t *solution)
 int
 main (void)
 {
-    dict_t dict;
+    dict_t dict, solution;
     board_t board;
-    trie_t *solution;
     struct timeval tv, tv_stop;
     int remaining, minutes, seconds;
     int found, missed;
@@ -180,8 +179,8 @@ main (void)
     dict_add_words_from_file (&dict, "words.txt");
     board_init (&board);
 
-    solution = trie_create ();
-    board_solve (&board, &dict, solution);
+    dict_init (&solution);
+    board_solve (&board, &dict, &solution);
 
     board_print (&board);
 
@@ -200,7 +199,7 @@ main (void)
            board_print (&board);
        } else {
            trie_t *t;
-           t = trie_find (solution, response);
+           t = trie_find (&solution, response);
            if (t && (t->flags & TRIE_FLAGS_IS_WORD)) {
                if (t->flags & TRIE_FLAGS_SEEN)
                    printf ("(repeat)\n");
@@ -222,13 +221,13 @@ main (void)
 
     printf ("\nWords you found:\n");
     string_init (&word);
-    found = trie_print_seen (solution, &word);
+    found = trie_print_seen (&solution, &word);
     string_fini (&word);
     printf ("\n");
 
     printf ("\nWords you missed:\n");
     string_init (&word);
-    missed = trie_print_unseen (solution, &word);
+    missed = trie_print_unseen (&solution, &word);
     string_fini (&word);
     printf ("\n");