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 "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLL"
28 "MMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ??";
31 rand_within (int num_values)
33 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
37 shuffle (char *array, int length)
41 for (i = 0; i < length; i++)
43 r = i + rand_within (length - i);
50 static const char *subanagram_anagram;
51 static char subanagram_enumerate_word[WORD_GAME_MAX_WORD_LENGTH];
52 static dict_t *subanagram_enumerate_answers;
55 subanagram_enumerate (dict_cursor_t cursor,
62 if (cursor == DICT_CURSOR_NIL)
65 if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
66 dict_add_word (subanagram_enumerate_answers, subanagram_enumerate_word);
68 last = strlen (subanagram_enumerate_word);
70 for (i = 0; i < strlen (subanagram_anagram); i++) {
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);
81 subanagram_enumerate_word[last] = c;
82 subanagram_enumerate (dict_cursor_next (cursor, c), next_seen);
84 subanagram_enumerate_word[last] = '\0';
89 subanagram_expand (const char *anagram, dict_t *dict, dict_t *answers)
91 subanagram_anagram = anagram;
92 memset (subanagram_enumerate_word, '\0', WORD_GAME_MAX_WORD_LENGTH);
93 subanagram_enumerate_answers = answers;
95 subanagram_enumerate (dict_root (dict), 0);
99 character_compare (const void *_a,
115 dict_t dict, solution;
117 char bag[100], rack[8];
119 gettimeofday (&tv, NULL);
120 srand (tv.tv_sec ^ tv.tv_usec);
122 memcpy (bag, bag_orig, 100);
125 memcpy (rack, bag, 7);
127 qsort (rack, 7, 1, character_compare);
130 dict_add_words_from_file (&dict, "words.txt");
132 dict_init (&solution);
133 subanagram_expand (rack, &dict, &solution);
135 word_game_play (rack, &dict, &solution, 0);
137 dict_fini (&solution);