2 * Copyright © 2006 Carl Worth
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)
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.
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."
19 /* Portably, schmortability. I want ease of programming. */
39 typedef struct _trie {
41 struct _trie *next[26];
51 fprintf (stderr, "Error: out of memory\n");
59 xrealloc (void *ptr, size_t size)
63 res = realloc (ptr, size);
65 fprintf (stderr, "Error: out of memory\n");
72 typedef struct _string {
79 string_init (string_t *string)
87 string_fini (string_t *string)
96 string_append_char (string_t *string, char c)
98 if (string->size == 0) {
100 string->s = xmalloc (string->size);
105 if (string->len + 1 == string->size) {
107 string->s = xrealloc (string->s, string->size);
110 string->s[string->len++] = c;
111 string->s[string->len] = '\0';
115 string_chop (string_t *string)
117 if (string->len == 0)
120 string->s[--string->len] = '\0';
124 trie_init (trie_t *trie)
127 trie->is_word = FALSE;
128 for (i = 0; i < 26; i++)
129 trie->next[i] = NULL;
137 trie = xmalloc (sizeof (trie_t));
145 trie_destroy (trie_t *trie)
149 for (i = 0; i < 26; i++)
151 trie_destroy (trie->next[i]);
156 #define TRIE_CHAR_TO_INDEX(c) (tolower (c) - 'a')
157 #define TRIE_INDEX_TO_CHAR(i) (i + 'a')
159 trie_add (trie_t *trie,
160 const char *word_chunk)
162 char c = word_chunk[0];
166 trie->is_word = TRUE;
170 i = TRIE_CHAR_TO_INDEX (c);
171 if (trie->next[i] == NULL)
172 trie->next[i] = trie_create ();
174 trie_add (trie->next[i], word_chunk + 1);
178 trie_contains (trie_t *trie,
179 const char *word_chunk)
181 char c = word_chunk[0];
185 return trie->is_word;
187 i = TRIE_CHAR_TO_INDEX (c);
188 if (trie->next[i] == NULL)
191 return trie_contains (trie->next[i], word_chunk + 1);
195 trie_print (trie_t *trie,
201 if (trie->is_word && string->s)
202 printf ("%s ", string->s);
204 /* Loop over each element, appending the character and recursing. */
205 for (i = 0; i < 26; i++) {
206 if (trie->next[i] == NULL)
209 c = TRIE_INDEX_TO_CHAR (i);
211 string_append_char (string, c);
212 trie_print (trie->next[i], string);
213 string_chop (string);
217 typedef struct _dict {
222 dict_init (dict_t *dict,
223 const char *filename)
227 size_t line_size = 0;
230 file = fopen (filename, "r");
232 fprintf (stderr, "Error: failed to open %s: %s\n",
233 filename, strerror (errno));
237 dict->trie = trie_create ();
240 bytes_read = getline (&line, &line_size, file);
241 if (bytes_read == -1)
243 if (line[strlen (line) - 1] == '\n')
244 line[strlen (line) - 1] = '\0';
245 trie_add (dict->trie, line);
254 dict_fini (dict_t *dict)
256 trie_destroy (dict->trie);
260 dict_print (dict_t *dict)
264 string_init (&string);
266 trie_print (dict->trie, &string);
268 string_fini (&string);
271 char *cube_faces[16] = {
272 "aaeeng", "abbjoo", "achops", "affkps",
273 "aoottw", "cimotu", "deilrx", "delrvy",
274 "distty", "eeghnw", "eeinsu", "ehrtvw",
275 "eiosst", "elrtty", "himnqu", "hlnnrz"
278 typedef struct _board {
281 /* Private, transient state used by enumerate */
286 rand_within (int num_values)
288 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
292 shuffle (int *array, int length)
296 for (i = 0; i < length; i++)
298 r = i + rand_within (length - i);
306 board_init (board_t *board)
311 for (i = 0; i < 16; i++)
315 for (i = 0; i < 16; i++)
316 board->letters[i / 4][i % 4] = cube_faces[cubes[i]][rand_within(6)];
320 board_print (board_t *board)
325 for (y = 0; y < 4; y++) {
326 for (x = 0; x < 4; x++) {
327 c = board->letters[y][x];
328 printf (" %c%s", toupper (c),
329 c == 'q' ? "u" : " ");
335 #define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
337 board_enumerate (board_t *board,
347 if (x < 0 || x >= 4 ||
349 seen & SEEN_BIT (x, y))
354 seen |= SEEN_BIT (x, y);
356 c = board->letters[y][x];
357 string_append_char (word, c);
358 dict_trie = dict_trie->next[TRIE_CHAR_TO_INDEX (c)];
359 if (dict_trie == NULL)
363 string_append_char (word, 'u');
364 dict_trie = dict_trie->next[TRIE_CHAR_TO_INDEX ('u')];
365 if (dict_trie == NULL)
369 if (dict_trie->is_word)
370 trie_add (board->result_trie, word->s);
372 for (dy = -1; dy <= 1; dy++)
373 for (dx = -1; dx <= 1; dx++)
374 board_enumerate (board, x + dx, y + dy, seen, word, dict_trie);
384 board_solve (board_t *board, dict_t *dict)
390 board->result_trie = trie_create ();
394 for (y = 0; y < 4; y++)
395 for (x = 0; x < 4; x++)
396 board_enumerate (board, x, y, seen, &word, dict->trie);
402 trie_print (board->result_trie, &word);
406 trie_destroy (board->result_trie);
416 gettimeofday (&tv, NULL);
417 srand (tv.tv_sec ^ tv.tv_usec);
419 dict_init (&dict, "words.txt");
421 board_print (&board);
423 /* sleep (3 * 60); */
425 board_solve (&board, &dict);