]> git.cworth.org Git - wordgame/blob - grid.c
Eliminate useless layer of dict wrapping trie.
[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 *result_trie;
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         trie_add (board->result_trie, 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, trie_t *solution)
147 {
148     int x, y;
149     int16_t seen = 0;
150     string_t word;
151
152     board->result_trie = 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;
168     board_t board;
169     trie_t *solution;
170     struct timeval tv, tv_stop;
171     int remaining, minutes, seconds;
172     int found, missed;
173     char prompt[7], *response;
174     string_t word;
175
176     gettimeofday (&tv, NULL);
177     srand (tv.tv_sec ^ tv.tv_usec);
178
179     dict_init (&dict);
180     dict_add_words_from_file (&dict, "words.txt");
181     board_init (&board);
182
183     solution = trie_create ();
184     board_solve (&board, &dict, solution);
185
186     board_print (&board);
187
188     gettimeofday (&tv, NULL);
189     tv_stop = tv;
190     tv_stop.tv_sec += GAME_LENGTH;
191     remaining = GAME_LENGTH;
192     do {
193         minutes = remaining / 60;
194         seconds = remaining % 60;
195         sprintf (prompt, "%02d:%02d ", minutes, seconds);
196         response = readline (prompt);
197         add_history (response);
198         chomp (response);
199         if (strlen (response) == 0) {
200             board_print (&board);
201         } else {
202             trie_t *t;
203             t = trie_find (solution, response);
204             if (t && (t->flags & TRIE_FLAGS_IS_WORD)) {
205                 if (t->flags & TRIE_FLAGS_SEEN)
206                     printf ("(repeat)\n");
207                 else
208                     t->flags |= TRIE_FLAGS_SEEN;
209             } else {
210                 t = trie_find (&dict, response);
211                 if (t && (t->flags & TRIE_FLAGS_IS_WORD))
212                     printf ("(a good word, but it's not in the puzzle)\n");
213                 else
214                     printf ("*** %s is not a word\n", response);
215             }
216         }
217         free (response);
218         gettimeofday (&tv, NULL);
219         remaining = floor (0.5 + (tv_stop.tv_sec - tv.tv_sec) + (tv_stop.tv_usec - tv.tv_usec) / 1000000.0);
220         minutes = remaining / 60;
221     } while (remaining > 0);
222
223     printf ("\nWords you found:\n");
224     string_init (&word);
225     found = trie_print_seen (solution, &word);
226     string_fini (&word);
227     printf ("\n");
228
229     printf ("\nWords you missed:\n");
230     string_init (&word);
231     missed = trie_print_unseen (solution, &word);
232     string_fini (&word);
233     printf ("\n");
234
235     printf ("\nYou found %d of %d words (%.2f%%)\n",
236             found, found + missed,
237             100 * (double) found / (found + missed));
238
239     dict_fini (&dict);
240
241     return 0;
242 }