]> git.cworth.org Git - wordgame/blob - word-game.c
Increase the window size a bit
[wordgame] / word-game.c
1 /*
2  * Copyright © 2006 Carl Worth
3  *
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)
7  * any later version.
8  *
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.
13  *
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."
17  */
18
19 #include "word-game.h"
20
21 #include <sys/time.h>
22 #include <time.h>
23 #include <math.h>
24
25 #include <readline/readline.h>
26 #include <readline/history.h>
27
28 /* Remember that dict reserves the 0th bit for IS_WORD */
29 #define GAME_WORD_SEEN (1<<1)
30
31 static bool_t
32 seen_predicate (dict_entry_t entry)
33 {
34     return entry & GAME_WORD_SEEN;
35 }
36
37 static bool_t
38 unseen_predicate (dict_entry_t entry)
39 {
40     return ! seen_predicate (entry);
41 }
42
43 static void
44 _count_possible (dict_cursor_t  cursor,
45                  int            depth,
46                  int            possible[17])
47 {
48     char c;
49
50     if (cursor == DICT_CURSOR_NIL)
51         return;
52
53     if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
54         possible[depth]++;
55
56     for (c = 'a'; c <= 'z'; c++)
57         _count_possible (dict_cursor_next (cursor, c), depth + 1, possible);
58 }
59
60 static void
61 _print_stats (int found[WORD_GAME_MAX_WORD_LENGTH+1],
62               int possible[WORD_GAME_MAX_WORD_LENGTH+1],
63               int found_total,
64               int possible_total)
65 {
66     int i;
67
68     for (i = 2; i <= 17; i++)
69         if (possible[i])
70             printf ("%d:(%d/%d) ", i, found[i], possible[i]);
71
72     printf ("total:(%d/%d = %.2f%%)\n",
73             found_total, possible_total,
74             100 * (double) found_total / possible_total);
75 }
76
77
78 void
79 word_game_play (const char      *puzzle,
80                 dict_t          *dict,
81                 dict_t          *answers,
82                 int              time_limit_seconds)
83 {
84     int i;
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;
92
93     for (i=0; i < WORD_GAME_MAX_WORD_LENGTH+1; i++) {
94         found[i] = 0;
95         possible[i] = 0;
96     }
97     _count_possible (dict_root (answers), 0, possible);
98     found_total = 0;
99     possible_total = 0;
100     for (i=0; i < 17; i++)
101         possible_total += possible[i];
102
103     printf ("%s\n", puzzle);
104     just_saw_puzzle = TRUE;
105
106     remaining = time_limit_seconds;
107     if (time_limit_seconds) {
108         gettimeofday (&tv, NULL);
109         tv_stop = tv;
110         tv_stop.tv_sec += time_limit_seconds;
111     } else {
112         sprintf (prompt, "> ");
113         remaining = 1;
114     }
115     while (1) {
116         if (time_limit_seconds) {
117             if (remaining > 0) {
118                 minutes = remaining / 60;
119                 seconds = remaining % 60;
120                 sprintf (prompt, "%02d:%02d ", minutes, seconds);
121             } else {
122                 printf ("Time's up.\a\n");
123                 sprintf (prompt, "> ");
124                 time_limit_seconds = 0;
125             }
126         }
127         response = readline (prompt);
128         if (response == NULL)
129             break;
130         if (strlen (response) == 0) {
131             if (! just_saw_puzzle) {
132                 printf ("%s\n", puzzle);
133                 just_saw_puzzle = TRUE;
134             } else {
135                 _print_stats (found, possible, found_total, possible_total);
136             }
137         } else {
138             dict_entry_t *entry;
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");
147                 } else {
148                     *entry |= GAME_WORD_SEEN;
149                     found [strlen (response)]++;
150                     found_total++;
151                 }
152             } else {
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");
156                 else
157                     printf ("*** %s is not a word\n", response);
158             }
159         }
160         free (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;
165         }
166     }
167
168     printf ("\n%s\n", puzzle);
169
170     printf ("\n");
171     _print_stats (found, possible, found_total, possible_total);
172     printf ("\n");
173
174     if (found_total) {
175         printf ("Words you found:\n");
176         dict_print_by_length_if (answers, seen_predicate);
177         printf ("\n");
178     }
179
180     if (found_total < possible_total) {
181         printf ("Words you missed:\n");
182         dict_print_by_length_if (answers, unseen_predicate);
183         printf ("\n");
184     }
185
186     printf ("You found %d of %d words (%.2f%%)\n",
187             found_total, possible_total,
188             100 * (double) found_total / possible_total);
189 }