]> git.cworth.org Git - wordgame/blob - grid.c
Complete the dict layer by adding dict_cursor_t and dict_entry_t
[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                  dict_cursor_t   dict_cursor)
105 {
106     char c;
107     int dx, dy;
108
109     if (dict_cursor == DICT_CURSOR_NIL)
110         return;
111
112     if (x < 0 || x >= 4 ||
113         y < 0 || y >= 4 ||
114         seen & SEEN_BIT (x, y))
115     {
116         return;
117     }
118
119     seen |= SEEN_BIT (x, y);
120
121     c = board->letters[y][x];
122     string_append_char (word, c);
123     dict_cursor = dict_cursor_next (dict_cursor, c);
124
125     if (c == 'q') {
126         string_append_char (word, 'u');
127         dict_cursor = dict_cursor_next (dict_cursor, 'u');
128     }
129
130     if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (dict_cursor)))
131         dict_add_word (board->results, word->s);
132
133     for (dy = -1; dy <= 1; dy++)
134         for (dx = -1; dx <= 1; dx++)
135             board_enumerate (board, x + dx, y + dy, seen, word, dict_cursor);
136
137     if (c == 'q')
138         string_chop (word);
139     string_chop (word);
140 }
141
142 void
143 board_solve (board_t *board, dict_t *dict, dict_t *solution)
144 {
145     int x, y;
146     int16_t seen = 0;
147     string_t word;
148
149     board->results = solution;
150
151     string_init (&word);
152
153     for (y = 0; y < 4; y++)
154         for (x = 0; x < 4; x++)
155             board_enumerate (board, x, y, seen, &word, dict_root (dict));
156
157     string_fini (&word);
158 }
159
160 #define GAME_LENGTH (3 * 60)
161 int
162 main (void)
163 {
164     dict_t dict, solution;
165     board_t board;
166     struct timeval tv, tv_stop;
167     int remaining, minutes, seconds;
168     int found, missed;
169     char prompt[7], *response;
170     string_t word;
171
172     gettimeofday (&tv, NULL);
173     srand (tv.tv_sec ^ tv.tv_usec);
174
175     dict_init (&dict);
176     dict_add_words_from_file (&dict, "words.txt");
177     board_init (&board);
178
179     dict_init (&solution);
180     board_solve (&board, &dict, &solution);
181
182     board_print (&board);
183
184     gettimeofday (&tv, NULL);
185     tv_stop = tv;
186     tv_stop.tv_sec += GAME_LENGTH;
187     remaining = GAME_LENGTH;
188     do {
189         minutes = remaining / 60;
190         seconds = remaining % 60;
191         sprintf (prompt, "%02d:%02d ", minutes, seconds);
192         response = readline (prompt);
193         add_history (response);
194         chomp (response);
195         if (strlen (response) == 0) {
196             board_print (&board);
197         } else {
198             dict_entry_t *entry;
199             entry = dict_lookup (&solution, response);
200             if (DICT_ENTRY_IS_WORD (entry)) {
201                 if (*entry & TRIE_FLAGS_SEEN)
202                     printf ("(repeat)\n");
203                 else
204                     *entry |= TRIE_FLAGS_SEEN;
205             } else {
206                 entry = dict_lookup (&dict, response);
207                 if (DICT_ENTRY_IS_WORD (entry))
208                     printf ("(a good word, but it's not in the puzzle)\n");
209                 else
210                     printf ("*** %s is not a word\n", response);
211             }
212         }
213         free (response);
214         gettimeofday (&tv, NULL);
215         remaining = floor (0.5 + (tv_stop.tv_sec - tv.tv_sec) + (tv_stop.tv_usec - tv.tv_usec) / 1000000.0);
216         minutes = remaining / 60;
217     } while (remaining > 0);
218
219     printf ("\nWords you found:\n");
220     string_init (&word);
221     found = trie_print_seen (&solution, &word);
222     string_fini (&word);
223     printf ("\n");
224
225     printf ("\nWords you missed:\n");
226     string_init (&word);
227     missed = trie_print_unseen (&solution, &word);
228     string_fini (&word);
229     printf ("\n");
230
231     printf ("\nYou found %d of %d words (%.2f%%)\n",
232             found, found + missed,
233             100 * (double) found / (found + missed));
234
235     dict_fini (&dict);
236
237     return 0;
238 }