]> git.cworth.org Git - wordgame/blob - grid.c
Begin to wean grid.c away from trie and toward dict.
[wordgame] / grid.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 <stdio.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <sys/time.h>
23 #include <time.h>
24 #include <math.h>
25
26 #include <readline/readline.h>
27 #include <readline/history.h>
28
29 #include "dict.h"
30
31 char *cube_faces[16] = {
32     "aaeeng", "abbjoo", "achops", "affkps",
33     "aoottw", "cimotu", "deilrx", "delrvy",
34     "distty", "eeghnw", "eeinsu", "ehrtvw",
35     "eiosst", "elrtty", "himnqu", "hlnnrz"
36 };
37
38 typedef struct _board {
39     char letters[4][4];
40
41     /* Private, transient state used by enumerate */
42     dict_t *results;
43 } board_t;
44
45 int
46 rand_within (int num_values)
47 {
48     return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
49 }
50
51 void
52 shuffle (int *array, int length)
53 {
54     int i, r, tmp;
55
56     for (i = 0; i < length; i++)
57     {
58         r = i + rand_within (length - i);
59         tmp = array[i];
60         array[i] = array[r];
61         array[r] = tmp;
62     }
63 }
64
65 void
66 board_init (board_t *board)
67 {
68     int i;
69     int cubes[16];
70
71     for (i = 0; i < 16; i++)
72         cubes[i] = i;
73     shuffle (cubes, 16);
74  
75     for (i = 0; i < 16; i++)
76         board->letters[i / 4][i % 4] = cube_faces[cubes[i]][rand_within(6)];
77 }
78
79 void
80 board_print (board_t *board)
81 {
82     int x, y;
83     char c;
84
85     printf ("\n");
86     for (y = 0; y < 4; y++) {
87         for (x = 0; x < 4; x++) {
88             c = board->letters[y][x];
89             printf (" %c%s", toupper (c),
90                     c == 'q' ? "u" : " ");
91         }
92         printf ("\n");
93     }
94     printf ("\n");
95 }
96
97 #define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
98 void
99 board_enumerate (board_t        *board,
100                  int             x,
101                  int             y,
102                  int16_t         seen,
103                  string_t       *word,
104                  trie_t         *dict_trie)
105 {
106     char c;
107     int dx, dy;
108
109     if (x < 0 || x >= 4 ||
110         y < 0 || y >= 4 ||
111         seen & SEEN_BIT (x, y))
112     {
113         return;
114     }
115
116     seen |= SEEN_BIT (x, y);
117
118     c = board->letters[y][x];
119     string_append_char (word, c);
120     dict_trie = dict_trie->next[TRIE_CHAR_TO_INDEX (c)];
121     if (dict_trie == NULL)
122         goto BAIL0;
123
124     if (c == 'q') {
125         string_append_char (word, 'u');
126         dict_trie = dict_trie->next[TRIE_CHAR_TO_INDEX ('u')];
127         if (dict_trie == NULL)
128             goto BAIL1;
129     }
130
131     if (dict_trie->flags & TRIE_FLAGS_IS_WORD)
132         dict_add_word (board->results, word->s);
133
134     for (dy = -1; dy <= 1; dy++)
135         for (dx = -1; dx <= 1; dx++)
136             board_enumerate (board, x + dx, y + dy, seen, word, dict_trie);
137
138   BAIL1:
139     if (c == 'q')
140         string_chop (word);
141   BAIL0:
142     string_chop (word);
143 }
144
145 void
146 board_solve (board_t *board, dict_t *dict, dict_t *solution)
147 {
148     int x, y;
149     int16_t seen = 0;
150     string_t word;
151
152     board->results = solution;
153
154     string_init (&word);
155
156     for (y = 0; y < 4; y++)
157         for (x = 0; x < 4; x++)
158             board_enumerate (board, x, y, seen, &word, dict);
159
160     string_fini (&word);
161 }
162
163 #define GAME_LENGTH (3 * 60)
164 int
165 main (void)
166 {
167     dict_t dict, solution;
168     board_t board;
169     struct timeval tv, tv_stop;
170     int remaining, minutes, seconds;
171     int found, missed;
172     char prompt[7], *response;
173     string_t word;
174
175     gettimeofday (&tv, NULL);
176     srand (tv.tv_sec ^ tv.tv_usec);
177
178     dict_init (&dict);
179     dict_add_words_from_file (&dict, "words.txt");
180     board_init (&board);
181
182     dict_init (&solution);
183     board_solve (&board, &dict, &solution);
184
185     board_print (&board);
186
187     gettimeofday (&tv, NULL);
188     tv_stop = tv;
189     tv_stop.tv_sec += GAME_LENGTH;
190     remaining = GAME_LENGTH;
191     do {
192         minutes = remaining / 60;
193         seconds = remaining % 60;
194         sprintf (prompt, "%02d:%02d ", minutes, seconds);
195         response = readline (prompt);
196         add_history (response);
197         chomp (response);
198         if (strlen (response) == 0) {
199             board_print (&board);
200         } else {
201             trie_t *t;
202             t = trie_find (&solution, response);
203             if (t && (t->flags & TRIE_FLAGS_IS_WORD)) {
204                 if (t->flags & TRIE_FLAGS_SEEN)
205                     printf ("(repeat)\n");
206                 else
207                     t->flags |= TRIE_FLAGS_SEEN;
208             } else {
209                 t = trie_find (&dict, response);
210                 if (t && (t->flags & TRIE_FLAGS_IS_WORD))
211                     printf ("(a good word, but it's not in the puzzle)\n");
212                 else
213                     printf ("*** %s is not a word\n", response);
214             }
215         }
216         free (response);
217         gettimeofday (&tv, NULL);
218         remaining = floor (0.5 + (tv_stop.tv_sec - tv.tv_sec) + (tv_stop.tv_usec - tv.tv_usec) / 1000000.0);
219         minutes = remaining / 60;
220     } while (remaining > 0);
221
222     printf ("\nWords you found:\n");
223     string_init (&word);
224     found = trie_print_seen (&solution, &word);
225     string_fini (&word);
226     printf ("\n");
227
228     printf ("\nWords you missed:\n");
229     string_init (&word);
230     missed = trie_print_unseen (&solution, &word);
231     string_fini (&word);
232     printf ("\n");
233
234     printf ("\nYou found %d of %d words (%.2f%%)\n",
235             found, found + missed,
236             100 * (double) found / (found + missed));
237
238     dict_fini (&dict);
239
240     return 0;
241 }