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."
26 #include <readline/readline.h>
27 #include <readline/history.h>
31 char *cube_faces[16] = {
32 "aaeeng", "abbjoo", "achops", "affkps",
33 "aoottw", "cimotu", "deilrx", "delrvy",
34 "distty", "eeghnw", "eeinsu", "ehrtvw",
35 "eiosst", "elrtty", "himnqu", "hlnnrz"
38 typedef struct _board {
41 /* Private, transient state used by enumerate */
46 rand_within (int num_values)
48 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
52 shuffle (int *array, int length)
56 for (i = 0; i < length; i++)
58 r = i + rand_within (length - i);
66 board_init (board_t *board)
71 for (i = 0; i < 16; i++)
75 for (i = 0; i < 16; i++)
76 board->letters[i / 4][i % 4] = cube_faces[cubes[i]][rand_within(6)];
80 board_print (board_t *board)
86 for (y = 0; y < 4; y++) {
87 for (x = 0; x < 4; x++) {
88 c = board->letters[y][x];
89 printf (" %c%s", toupper (c),
90 c == 'q' ? "u" : " ");
97 #define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
99 board_enumerate (board_t *board,
104 dict_cursor_t dict_cursor)
109 if (dict_cursor == DICT_CURSOR_NIL)
112 if (x < 0 || x >= 4 ||
114 seen & SEEN_BIT (x, y))
119 seen |= SEEN_BIT (x, y);
121 c = board->letters[y][x];
122 string_append_char (word, c);
123 dict_cursor = dict_cursor_next (dict_cursor, c);
126 string_append_char (word, 'u');
127 dict_cursor = dict_cursor_next (dict_cursor, 'u');
130 if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (dict_cursor)))
131 dict_add_word (board->results, word->s);
133 for (dy = -1; dy <= 1; dy++)
134 for (dx = -1; dx <= 1; dx++)
135 board_enumerate (board, x + dx, y + dy, seen, word, dict_cursor);
143 board_solve (board_t *board, dict_t *dict, dict_t *solution)
149 board->results = solution;
153 for (y = 0; y < 4; y++)
154 for (x = 0; x < 4; x++)
155 board_enumerate (board, x, y, seen, &word, dict_root (dict));
160 #define GAME_LENGTH (3 * 60)
164 dict_t dict, solution;
166 struct timeval tv, tv_stop;
167 int remaining, minutes, seconds;
169 char prompt[7], *response;
172 gettimeofday (&tv, NULL);
173 srand (tv.tv_sec ^ tv.tv_usec);
176 dict_add_words_from_file (&dict, "words.txt");
179 dict_init (&solution);
180 board_solve (&board, &dict, &solution);
182 board_print (&board);
184 gettimeofday (&tv, NULL);
186 tv_stop.tv_sec += GAME_LENGTH;
187 remaining = GAME_LENGTH;
189 minutes = remaining / 60;
190 seconds = remaining % 60;
191 sprintf (prompt, "%02d:%02d ", minutes, seconds);
192 response = readline (prompt);
193 add_history (response);
195 if (strlen (response) == 0) {
196 board_print (&board);
199 entry = dict_lookup (&solution, response);
200 if (DICT_ENTRY_IS_WORD (entry)) {
201 if (*entry & TRIE_FLAGS_SEEN)
202 printf ("(repeat)\n");
204 *entry |= TRIE_FLAGS_SEEN;
206 entry = dict_lookup (&dict, response);
207 if (DICT_ENTRY_IS_WORD (entry))
208 printf ("(a good word, but it's not in the puzzle)\n");
210 printf ("*** %s is not a word\n", response);
214 gettimeofday (&tv, NULL);
215 remaining = floor (0.5 + (tv_stop.tv_sec - tv.tv_sec) + (tv_stop.tv_usec - tv.tv_usec) / 1000000.0);
216 minutes = remaining / 60;
217 } while (remaining > 0);
219 printf ("\nWords you found:\n");
221 found = trie_print_seen (&solution, &word);
225 printf ("\nWords you missed:\n");
227 missed = trie_print_unseen (&solution, &word);
231 printf ("\nYou found %d of %d words (%.2f%%)\n",
232 found, found + missed,
233 100 * (double) found / (found + missed));