]> git.cworth.org Git - wordgame/blob - rack.c
Don't print empty found/missed sections
[wordgame] / rack.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 #include <ctype.h>
23 #include <sys/time.h>
24 #include <time.h>
25
26 char bag_orig[101] =
27 "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLL"
28 "MMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ??";
29
30 static int
31 rand_within (int num_values)
32 {
33     return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
34 }
35
36 static void
37 shuffle (char *array, int length)
38 {
39     int i, r, tmp;
40
41     for (i = 0; i < length; i++)
42     {
43         r = i + rand_within (length - i);
44         tmp = array[i];
45         array[i] = array[r];
46         array[r] = tmp;
47     }
48 }
49
50 static const char *subanagram_anagram;
51 static char subanagram_enumerate_word[WORD_GAME_MAX_WORD_LENGTH];
52 static dict_t *subanagram_enumerate_answers;
53
54 static void
55 subanagram_enumerate (dict_cursor_t      cursor,
56                       uint8_t            seen)
57 {
58     char c;
59     unsigned int i, last;
60     uint8_t next_seen;
61
62     if (cursor == DICT_CURSOR_NIL)
63         return;
64
65     if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
66         dict_add_word (subanagram_enumerate_answers, subanagram_enumerate_word);
67
68     last = strlen (subanagram_enumerate_word);
69
70     for (i = 0; i < strlen (subanagram_anagram); i++) {
71         if (seen & (1 << i))
72             continue;
73         next_seen = seen | (1 << i);
74         c = subanagram_anagram[i];
75         if (c == '?' || c == '_') {
76             for (c = 'a'; c <= 'z'; c++) {
77                 subanagram_enumerate_word[last] = c;
78                 subanagram_enumerate (dict_cursor_next (cursor, c), next_seen);
79             }
80         } else {
81             subanagram_enumerate_word[last] = c;
82             subanagram_enumerate (dict_cursor_next (cursor, c), next_seen);
83         }
84         subanagram_enumerate_word[last] = '\0';
85     }
86 }
87
88 static void
89 subanagram_expand (const char *anagram, dict_t *dict, dict_t *answers)
90 {
91     subanagram_anagram = anagram;
92     memset (subanagram_enumerate_word, '\0', WORD_GAME_MAX_WORD_LENGTH);
93     subanagram_enumerate_answers = answers;
94
95     subanagram_enumerate (dict_root (dict), 0);
96 }
97
98 static int
99 character_compare (const void *_a,
100                    const void *_b)
101 {
102     const char *a = _a;
103     const char *b = _b;
104
105     if (*a == '?')
106         return 1;
107     if (*b == '?')
108         return -1;
109     return *a - *b;
110 }
111
112 int
113 main (void)
114 {
115     dict_t dict, solution;
116     struct timeval tv;
117     char bag[100], rack[8];
118
119     gettimeofday (&tv, NULL);
120     srand (tv.tv_sec ^ tv.tv_usec);
121
122     memcpy (bag, bag_orig, 100);
123     shuffle (bag, 100);
124
125     memcpy (rack, bag, 7);
126     rack[7] = '\0';
127     qsort (rack, 7, 1, character_compare);
128
129     dict_init (&dict);
130     dict_add_words_from_file (&dict, "words.txt");
131
132     dict_init (&solution);
133     subanagram_expand (rack, &dict, &solution);
134
135     word_game_play (rack, &dict, &solution, 0);
136
137     dict_fini (&solution);
138     dict_fini (&dict);
139
140     return 0;
141 }