]> git.cworth.org Git - wordgame/blob - subanagram.c
Increase the window size a bit
[wordgame] / subanagram.c
1 /*
2  * Copyright © 2006 Carl Worth
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
17  */
18
19 #include "word-game.h"
20
21 #include <string.h>
22
23 static const char *subanagram_letters;
24 static char subanagram_enumerate_word[WORD_GAME_MAX_WORD_LENGTH];
25 static dict_t *subanagram_enumerate_answers;
26
27 static void
28 subanagram_enumerate (dict_cursor_t      cursor,
29                       uint8_t            seen)
30 {
31     char c;
32     unsigned int i, last;
33     uint8_t next_seen;
34
35     if (cursor == DICT_CURSOR_NIL)
36         return;
37
38     if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
39         dict_add_word (subanagram_enumerate_answers, subanagram_enumerate_word);
40
41     last = strlen (subanagram_enumerate_word);
42
43     for (i = 0; i < strlen (subanagram_letters); i++) {
44         if (seen & (1 << i))
45             continue;
46         next_seen = seen | (1 << i);
47         c = subanagram_letters[i];
48         if (c == '?' || c == '_') {
49             for (c = 'a'; c <= 'z'; c++) {
50                 subanagram_enumerate_word[last] = c;
51                 subanagram_enumerate (dict_cursor_next (cursor, c), next_seen);
52             }
53         } else {
54             subanagram_enumerate_word[last] = c;
55             subanagram_enumerate (dict_cursor_next (cursor, c), next_seen);
56         }
57         subanagram_enumerate_word[last] = '\0';
58     }
59 }
60
61 void
62 subanagram_expand (const char *letters, dict_t *dict, dict_t *answers)
63 {
64     subanagram_letters = letters;
65     memset (subanagram_enumerate_word, '\0', WORD_GAME_MAX_WORD_LENGTH);
66     subanagram_enumerate_answers = answers;
67
68     subanagram_enumerate (dict_root (dict), 0);
69 }