]> git.cworth.org Git - wordgame/blob - drill2.c
Allow a new game to be started by pressing Enter after Control-C
[wordgame] / drill2.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 <string.h>
22 #include <ctype.h>
23 #include <sys/time.h>
24 #include <time.h>
25
26 static int
27 rand_within (int num_values)
28 {
29     return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
30 }
31
32 static void
33 shuffle (int *array, int length)
34 {
35     int i, r, tmp;
36
37     for (i = 0; i < length; i++)
38     {
39         r = i + rand_within (length - i);
40         tmp = array[i];
41         array[i] = array[r];
42         array[r] = tmp;
43     }
44 }
45
46 static char pattern_enumerate_word[WORD_GAME_MAX_WORD_LENGTH];
47 static dict_t *pattern_enumerate_answers;
48
49 static void
50 pattern_enumerate (const char           *pattern_chunk,
51                    dict_cursor_t         cursor)
52 {
53     char c = *pattern_chunk;
54     int last;
55
56     if (cursor == DICT_CURSOR_NIL)
57         return;
58
59     if (c == '\0') {
60         if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
61             dict_add_word (pattern_enumerate_answers, pattern_enumerate_word);
62         return;
63     }
64
65     last = strlen (pattern_enumerate_word);
66
67     if (c == '?') {
68         for (c = 'A'; c <= 'Z'; c++) {
69             pattern_enumerate_word[last] = c;
70             pattern_enumerate (pattern_chunk + 1, dict_cursor_next (cursor, c));
71         }
72     } else {
73         pattern_enumerate_word[last] = c;
74         pattern_enumerate (pattern_chunk + 1, dict_cursor_next (cursor, c));
75     }
76
77     pattern_enumerate_word[last] = '\0';
78 }
79
80 static void
81 pattern_expand (const char *pattern, dict_t *dict, dict_t *answers)
82 {
83     memset (pattern_enumerate_word, '\0', WORD_GAME_MAX_WORD_LENGTH);
84     pattern_enumerate_answers = answers;
85
86     pattern_enumerate (pattern, dict_root (dict));
87 }
88
89 int
90 main (void)
91 {
92     int i, p, puzzle[52];
93     char puzzle_string[3];
94     dict_t dict, solution;
95     struct timeval tv;
96
97     gettimeofday (&tv, NULL);
98     srand (tv.tv_sec ^ tv.tv_usec);
99
100     for (i = 0; i < 52; i++)
101         puzzle[i] = i;
102     shuffle (puzzle, 52);
103
104     dict_init (&dict);
105     dict_add_words_from_file (&dict, "words.txt");
106
107     puzzle_string[2] = '\0';
108
109     for (i = 0; i < 52; i++) {
110         p = puzzle[i];
111         if (p < 26) {
112             puzzle_string[0] = 'A' + p;
113             puzzle_string[1] = '?';
114         } else {
115             puzzle_string[0] = '?';
116             puzzle_string[1] = 'A' + (p - 26);
117         }
118         dict_init (&solution);
119         pattern_expand (puzzle_string, &dict, &solution);
120
121         word_game_play (puzzle_string, &dict, &solution, 0);
122
123         dict_fini (&solution);
124     }
125
126     dict_fini (&dict);
127
128     return 0;
129 }