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 word_game_play (const char *puzzle,
64 int time_limit_seconds)
67 struct timeval tv, tv_stop;
68 int remaining, minutes, seconds;
69 char prompt[7], *response;
70 bool_t just_saw_puzzle;
71 int found[WORD_GAME_MAX_WORD_LENGTH+1];
72 int possible[WORD_GAME_MAX_WORD_LENGTH+1];
73 int found_total, possible_total;
75 for (i=0; i < WORD_GAME_MAX_WORD_LENGTH+1; i++) {
79 _count_possible (dict_root (answers), 0, possible);
82 for (i=0; i < 17; i++)
83 possible_total += possible[i];
85 printf ("%s\n\n", puzzle);
86 just_saw_puzzle = TRUE;
88 remaining = time_limit_seconds;
89 if (time_limit_seconds) {
90 gettimeofday (&tv, NULL);
92 tv_stop.tv_sec += time_limit_seconds;
94 sprintf (prompt, "> ");
98 if (time_limit_seconds) {
99 minutes = remaining / 60;
100 seconds = remaining % 60;
101 sprintf (prompt, "%02d:%02d ", minutes, seconds);
103 response = readline (prompt);
104 if (response == NULL)
106 if (strlen (response) == 0) {
107 if (! just_saw_puzzle) {
108 printf ("%s\n\n", puzzle);
109 just_saw_puzzle = TRUE;
111 for (i = 2; i <= 17; i++)
113 printf ("%d:(%d/%d) ", i, found[i], possible[i]);
114 printf ("total:(%d/%d = %.2f%%)\n",
115 found_total, possible_total,
116 100 * (double) found_total / possible_total);
120 just_saw_puzzle = FALSE;
121 add_history (response);
122 if (response[strlen (response) - 1] == '\n')
123 response[strlen (response) - 1] = '\0';
124 entry = dict_lookup (answers, response);
125 if (DICT_ENTRY_IS_WORD (entry)) {
126 if (*entry & GAME_WORD_SEEN) {
127 printf ("(repeat)\n");
129 *entry |= GAME_WORD_SEEN;
130 found [strlen (response)]++;
134 entry = dict_lookup (dict, response);
135 if (DICT_ENTRY_IS_WORD (entry))
136 printf ("(a good word, but it's not in the puzzle)\n");
138 printf ("*** %s is not a word\n", response);
142 if (time_limit_seconds) {
143 gettimeofday (&tv, NULL);
144 remaining = floor (0.5 + (tv_stop.tv_sec - tv.tv_sec) + (tv_stop.tv_usec - tv.tv_usec) / 1000000.0);
145 minutes = remaining / 60;
147 } while (remaining > 0);
149 printf ("%s\n\n", puzzle);
151 printf ("Words you found:\n");
152 dict_print_by_length_if (answers, seen_predicate);
154 printf ("\nWords you missed:\n");
155 dict_print_by_length_if (answers, unseen_predicate);
158 printf ("You found %d of %d words (%.2f%%)\n",
159 found_total, possible_total,
160 100 * (double) found_total / possible_total);