]> git.cworth.org Git - wordgame/blob - word-game.c
Move the main game logic from grid.c to new word-game.c
[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     }
96     do {
97         if (time_limit_seconds) {
98             minutes = remaining / 60;
99             seconds = remaining % 60;
100             sprintf (prompt, "%02d:%02d ", minutes, seconds);
101         }
102         response = readline (prompt);
103         add_history (response);
104         if (strlen (response) == 0) {
105             if (! just_saw_puzzle) {
106                 printf ("%s\n", puzzle);
107                 just_saw_puzzle = TRUE;
108             } else {
109                 for (i = 2; i <= 17; i++)
110                     if (possible[i])
111                         printf ("%d:(%d/%d) ", i, found[i], possible[i]);
112                 printf ("total:(%d/%d = %.2f%%)\n",
113                         found_total, possible_total,
114                         100 * (double) found_total / possible_total);
115             }
116         } else {
117             dict_entry_t *entry;
118             just_saw_puzzle = FALSE;
119             if (response[strlen (response) - 1] == '\n')
120                 response[strlen (response) - 1] = '\0';
121             entry = dict_lookup (answers, response);
122             if (DICT_ENTRY_IS_WORD (entry)) {
123                 if (*entry & GAME_WORD_SEEN) {
124                     printf ("(repeat)\n");
125                 } else {
126                     *entry |= GAME_WORD_SEEN;
127                     found [strlen (response)]++;
128                     found_total++;
129                 }
130             } else {
131                 entry = dict_lookup (dict, response);
132                 if (DICT_ENTRY_IS_WORD (entry))
133                     printf ("(a good word, but it's not in the puzzle)\n");
134                 else
135                     printf ("*** %s is not a word\n", response);
136             }
137         }
138         free (response);
139         if (time_limit_seconds) {
140             gettimeofday (&tv, NULL);
141             remaining = floor (0.5 + (tv_stop.tv_sec - tv.tv_sec) + (tv_stop.tv_usec - tv.tv_usec) / 1000000.0);
142             minutes = remaining / 60;
143         }
144     } while (remaining > 0);
145
146     printf ("%s\n", puzzle);
147
148     printf ("Words you found:\n");
149     dict_print_by_length_if (answers, seen_predicate);
150
151     printf ("\nWords you missed:\n");
152     dict_print_by_length_if (answers, unseen_predicate);
153     printf ("\n");
154
155     printf ("You found %d of %d words (%.2f%%)\n",
156             found_total, possible_total,
157             100 * (double) found_total / possible_total);
158 }