--- /dev/null
+/*
+ * Copyright © 2006 Carl Worth
+ *
+ * This program is free software; you can redistribute it and\/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
+ */
+
+#include "word-game.h"
+
+#include <string.h>
+#include <ctype.h>
+#include <sys/time.h>
+#include <time.h>
+
+static int
+rand_within (int num_values)
+{
+ return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
+}
+
+static void
+shuffle (int *array, int length)
+{
+ int i, r, tmp;
+
+ for (i = 0; i < length; i++)
+ {
+ r = i + rand_within (length - i);
+ tmp = array[i];
+ array[i] = array[r];
+ array[r] = tmp;
+ }
+}
+
+static char pattern_enumerate_word[WORD_GAME_MAX_WORD_LENGTH];
+static dict_t *pattern_enumerate_answers;
+
+static void
+pattern_enumerate (const char *pattern_chunk,
+ dict_cursor_t cursor)
+{
+ char c = *pattern_chunk;
+ int last;
+
+ if (cursor == DICT_CURSOR_NIL)
+ return;
+
+ if (c == '\0') {
+ if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
+ dict_add_word (pattern_enumerate_answers, pattern_enumerate_word);
+ return;
+ }
+
+ last = strlen (pattern_enumerate_word);
+
+ if (c == '?' || c == '_') {
+ for (c = 'a'; c <= 'z'; c++) {
+ pattern_enumerate_word[last] = c;
+ pattern_enumerate (pattern_chunk + 1, dict_cursor_next (cursor, c));
+ }
+ } else {
+ pattern_enumerate_word[last] = c;
+ pattern_enumerate (pattern_chunk + 1, dict_cursor_next (cursor, c));
+ }
+
+ pattern_enumerate_word[last] = '\0';
+}
+
+static void
+pattern_expand (const char *pattern, dict_t *dict, dict_t *answers)
+{
+ memset (pattern_enumerate_word, '\0', WORD_GAME_MAX_WORD_LENGTH);
+ pattern_enumerate_answers = answers;
+
+ pattern_enumerate (pattern, dict_root (dict));
+}
+
+int
+main (void)
+{
+ int i, p, puzzle[52];
+ char puzzle_string[3];
+ dict_t dict, solution;
+ struct timeval tv;
+
+ gettimeofday (&tv, NULL);
+ srand (tv.tv_sec ^ tv.tv_usec);
+
+ for (i = 0; i < 52; i++)
+ puzzle[i] = i;
+ shuffle (puzzle, 52);
+
+ dict_init (&dict);
+ dict_add_words_from_file (&dict, "words.txt");
+
+ puzzle_string[2] = '\0';
+
+ for (i = 0; i < 52; i++) {
+ p = puzzle[i];
+ if (p < 26) {
+ puzzle_string[0] = 'a' + p;
+ puzzle_string[1] = '?';
+ } else {
+ puzzle_string[0] = '?';
+ puzzle_string[1] = 'a' + (p - 26);
+ }
+ dict_init (&solution);
+ pattern_expand (puzzle_string, &dict, &solution);
+
+ word_game_play (puzzle_string, &dict, &solution, 0);
+
+ dict_fini (&solution);
+ }
+
+ dict_fini (&dict);
+
+ return 0;
+}