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"
25 #include <readline/readline.h>
26 #include <readline/history.h>
28 /* Remember that dict reserves the 0th bit for IS_WORD */
29 #define GAME_WORD_SEEN (1<<1)
32 seen_predicate (dict_entry_t entry)
34 return entry & GAME_WORD_SEEN;
38 unseen_predicate (dict_entry_t entry)
40 return ! seen_predicate (entry);
44 _count_possible (dict_cursor_t cursor,
50 if (cursor == DICT_CURSOR_NIL)
53 if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
56 for (c = 'a'; c <= 'z'; c++)
57 _count_possible (dict_cursor_next (cursor, c), depth + 1, possible);
61 _print_stats (int found[WORD_GAME_MAX_WORD_LENGTH+1],
62 int possible[WORD_GAME_MAX_WORD_LENGTH+1],
68 for (i = 2; i <= 17; i++)
70 printf ("%d:(%d/%d) ", i, found[i], possible[i]);
72 printf ("total:(%d/%d = %.2f%%)\n",
73 found_total, possible_total,
74 100 * (double) found_total / possible_total);
79 word_game_play (const char *puzzle,
82 int time_limit_seconds)
85 struct timeval tv, tv_stop;
86 int remaining, minutes, seconds;
87 char prompt[7], *response;
88 bool_t just_saw_puzzle;
89 int found[WORD_GAME_MAX_WORD_LENGTH+1];
90 int possible[WORD_GAME_MAX_WORD_LENGTH+1];
91 int found_total, possible_total;
93 for (i=0; i < WORD_GAME_MAX_WORD_LENGTH+1; i++) {
97 _count_possible (dict_root (answers), 0, possible);
100 for (i=0; i < 17; i++)
101 possible_total += possible[i];
103 printf ("%s\n", puzzle);
104 just_saw_puzzle = TRUE;
106 remaining = time_limit_seconds;
107 if (time_limit_seconds) {
108 gettimeofday (&tv, NULL);
110 tv_stop.tv_sec += time_limit_seconds;
112 sprintf (prompt, "> ");
116 if (time_limit_seconds) {
118 minutes = remaining / 60;
119 seconds = remaining % 60;
120 sprintf (prompt, "%02d:%02d ", minutes, seconds);
122 printf ("Time's up.\a\n");
123 sprintf (prompt, "> ");
124 time_limit_seconds = 0;
127 response = readline (prompt);
128 if (response == NULL)
130 if (strlen (response) == 0) {
131 if (! just_saw_puzzle) {
132 printf ("%s\n", puzzle);
133 just_saw_puzzle = TRUE;
135 _print_stats (found, possible, found_total, possible_total);
139 just_saw_puzzle = FALSE;
140 add_history (response);
141 if (response[strlen (response) - 1] == '\n')
142 response[strlen (response) - 1] = '\0';
143 entry = dict_lookup (answers, response);
144 if (DICT_ENTRY_IS_WORD (entry)) {
145 if (*entry & GAME_WORD_SEEN) {
146 printf ("(repeat)\n");
148 *entry |= GAME_WORD_SEEN;
149 found [strlen (response)]++;
153 entry = dict_lookup (dict, response);
154 if (DICT_ENTRY_IS_WORD (entry))
155 printf ("(a good word, but it's not in the puzzle)\n");
157 printf ("*** %s is not a word\n", response);
161 if (time_limit_seconds) {
162 gettimeofday (&tv, NULL);
163 remaining = floor (0.5 + (tv_stop.tv_sec - tv.tv_sec) + (tv_stop.tv_usec - tv.tv_usec) / 1000000.0);
164 minutes = remaining / 60;
168 printf ("\n%s\n", puzzle);
171 _print_stats (found, possible, found_total, possible_total);
175 printf ("Words you found:\n");
176 dict_print_by_length_if (answers, seen_predicate);
180 if (found_total < possible_total) {
181 printf ("Words you missed:\n");
182 dict_print_by_length_if (answers, unseen_predicate);
186 printf ("You found %d of %d words (%.2f%%)\n",
187 found_total, possible_total,
188 100 * (double) found_total / possible_total);