]> git.cworth.org Git - wordgame/blob - word-game.c
grid: Don't consider 2-letter words as valid.
[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 void
61 word_game_play (const char      *puzzle,
62                 dict_t          *dict,
63                 dict_t          *answers,
64                 int              time_limit_seconds)
65 {
66     int i;
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;
74
75     for (i=0; i < WORD_GAME_MAX_WORD_LENGTH+1; i++) {
76         found[i] = 0;
77         possible[i] = 0;
78     }
79     _count_possible (dict_root (answers), 0, possible);
80     found_total = 0;
81     possible_total = 0;
82     for (i=0; i < 17; i++)
83         possible_total += possible[i];
84
85     printf ("%s\n", puzzle);
86     just_saw_puzzle = TRUE;
87
88     remaining = time_limit_seconds;
89     if (time_limit_seconds) {
90         gettimeofday (&tv, NULL);
91         tv_stop = tv;
92         tv_stop.tv_sec += time_limit_seconds;
93     } else {
94         sprintf (prompt, "> ");
95         remaining = 1;
96     }
97     do {
98         if (time_limit_seconds) {
99             minutes = remaining / 60;
100             seconds = remaining % 60;
101             sprintf (prompt, "%02d:%02d ", minutes, seconds);
102         }
103         response = readline (prompt);
104         if (response == NULL)
105             break;
106         if (strlen (response) == 0) {
107             if (! just_saw_puzzle) {
108                 printf ("%s\n", puzzle);
109                 just_saw_puzzle = TRUE;
110             } else {
111                 for (i = 2; i <= 17; i++)
112                     if (possible[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);
117             }
118         } else {
119             dict_entry_t *entry;
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");
128                 } else {
129                     *entry |= GAME_WORD_SEEN;
130                     found [strlen (response)]++;
131                     found_total++;
132                 }
133             } else {
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");
137                 else
138                     printf ("*** %s is not a word\n", response);
139             }
140         }
141         free (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;
146         }
147     } while (remaining > 0);
148
149     printf ("%s\n", puzzle);
150
151     if (found_total) {
152         printf ("Words you found:\n");
153         dict_print_by_length_if (answers, seen_predicate);
154         printf ("\n");
155     }
156
157     if (found_total < possible_total) {
158         printf ("Words you missed:\n");
159         dict_print_by_length_if (answers, unseen_predicate);
160         printf ("\n");
161     }
162
163     printf ("You found %d of %d words (%.2f%%)\n",
164             found_total, possible_total,
165             100 * (double) found_total / possible_total);
166 }