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 #include "word-game.h"
27 rand_within (int num_values)
29 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
33 shuffle (int *array, int length)
37 for (i = 0; i < length; i++)
39 r = i + rand_within (length - i);
46 static char pattern_enumerate_word[WORD_GAME_MAX_WORD_LENGTH];
47 static dict_t *pattern_enumerate_answers;
50 pattern_enumerate (const char *pattern_chunk,
53 char c = *pattern_chunk;
56 if (cursor == DICT_CURSOR_NIL)
60 if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
61 dict_add_word (pattern_enumerate_answers, pattern_enumerate_word);
65 last = strlen (pattern_enumerate_word);
67 if (c == '?' || c == '_') {
68 for (c = 'a'; c <= 'z'; c++) {
69 pattern_enumerate_word[last] = c;
70 pattern_enumerate (pattern_chunk + 1, dict_cursor_next (cursor, c));
73 pattern_enumerate_word[last] = c;
74 pattern_enumerate (pattern_chunk + 1, dict_cursor_next (cursor, c));
77 pattern_enumerate_word[last] = '\0';
81 pattern_expand (const char *pattern, dict_t *dict, dict_t *answers)
83 memset (pattern_enumerate_word, '\0', WORD_GAME_MAX_WORD_LENGTH);
84 pattern_enumerate_answers = answers;
86 pattern_enumerate (pattern, dict_root (dict));
93 char puzzle_string[3];
94 dict_t dict, solution;
97 gettimeofday (&tv, NULL);
98 srand (tv.tv_sec ^ tv.tv_usec);
100 for (i = 0; i < 52; i++)
102 shuffle (puzzle, 52);
105 dict_add_words_from_file (&dict, "words.txt");
107 puzzle_string[2] = '\0';
109 for (i = 0; i < 52; i++) {
112 puzzle_string[0] = 'a' + p;
113 puzzle_string[1] = '?';
115 puzzle_string[0] = '?';
116 puzzle_string[1] = 'a' + (p - 26);
118 dict_init (&solution);
119 pattern_expand (puzzle_string, &dict, &solution);
121 word_game_play (puzzle_string, &dict, &solution, 0);
123 dict_fini (&solution);