X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=grid.c;h=44ffa9d23f7487b90a8c0c71f542ae267d364c62;hb=dd4b319cdb6e2883ae76317036b57b670344160a;hp=b9c307bafd50e7a2e49c8b4653802de8a5ec6686;hpb=2eb18813b84ae8ad4faeb92b1d8570599135d537;p=wordgame diff --git a/grid.c b/grid.c index b9c307b..44ffa9d 100644 --- a/grid.c +++ b/grid.c @@ -39,7 +39,7 @@ typedef struct _board { char letters[4][4]; /* Private, transient state used by enumerate */ - trie_t *result_trie; + dict_t *results; } board_t; int @@ -101,11 +101,14 @@ board_enumerate (board_t *board, int y, int16_t seen, string_t *word, - trie_t *dict_trie) + dict_cursor_t dict_cursor) { char c; int dx, dy; + if (dict_cursor == DICT_CURSOR_NIL) + return; + if (x < 0 || x >= 4 || y < 0 || y >= 4 || seen & SEEN_BIT (x, y)) @@ -117,45 +120,39 @@ board_enumerate (board_t *board, c = board->letters[y][x]; string_append_char (word, c); - dict_trie = dict_trie->next[TRIE_CHAR_TO_INDEX (c)]; - if (dict_trie == NULL) - goto BAIL0; + dict_cursor = dict_cursor_next (dict_cursor, c); if (c == 'q') { string_append_char (word, 'u'); - dict_trie = dict_trie->next[TRIE_CHAR_TO_INDEX ('u')]; - if (dict_trie == NULL) - goto BAIL1; + dict_cursor = dict_cursor_next (dict_cursor, 'u'); } - if (dict_trie->flags & TRIE_FLAGS_IS_WORD) - trie_add (board->result_trie, word->s); + if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (dict_cursor))) + dict_add_word (board->results, word->s); for (dy = -1; dy <= 1; dy++) for (dx = -1; dx <= 1; dx++) - board_enumerate (board, x + dx, y + dy, seen, word, dict_trie); + board_enumerate (board, x + dx, y + dy, seen, word, dict_cursor); - BAIL1: if (c == 'q') string_chop (word); - BAIL0: string_chop (word); } 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); for (y = 0; y < 4; y++) for (x = 0; x < 4; x++) - board_enumerate (board, x, y, seen, &word, dict->trie); + board_enumerate (board, x, y, seen, &word, dict_root (dict)); string_fini (&word); } @@ -164,9 +161,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; @@ -176,11 +172,12 @@ main (void) gettimeofday (&tv, NULL); srand (tv.tv_sec ^ tv.tv_usec); - dict_init (&dict, "words.txt"); + dict_init (&dict); + 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); @@ -198,16 +195,16 @@ main (void) if (strlen (response) == 0) { board_print (&board); } else { - trie_t *t; - t = trie_find (solution, response); - if (t && (t->flags & TRIE_FLAGS_IS_WORD)) { - if (t->flags & TRIE_FLAGS_SEEN) + dict_entry_t *entry; + entry = dict_lookup (&solution, response); + if (DICT_ENTRY_IS_WORD (entry)) { + if (*entry & TRIE_FLAGS_SEEN) printf ("(repeat)\n"); else - t->flags |= TRIE_FLAGS_SEEN; + *entry |= TRIE_FLAGS_SEEN; } else { - t = trie_find (dict.trie, response); - if (t && (t->flags & TRIE_FLAGS_IS_WORD)) + entry = dict_lookup (&dict, response); + if (DICT_ENTRY_IS_WORD (entry)) printf ("(a good word, but it's not in the puzzle)\n"); else printf ("*** %s is not a word\n", response); @@ -221,13 +218,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");