2 * Copyright © 2006 Carl Worth
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)
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.
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."
24 char *cube_faces[16] = {
25 "aaeeng", "abbjoo", "achops", "affkps",
26 "aoottw", "cimotu", "deilrx", "delrvy",
27 "distty", "eeghnw", "eeinsu", "ehrtvw",
28 "eiosst", "elrtty", "himnqu", "hlnnrz"
32 rand_within (int num_values)
34 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
38 shuffle (int *array, int length)
42 for (i = 0; i < length; i++)
44 r = i + rand_within (length - i);
52 grid_init (grid_t *grid)
57 for (i = 0; i < 16; i++)
61 for (i = 0; i < 16; i++)
62 grid->letters[i / 4][i % 4] = cube_faces[cubes[i]][rand_within(6)];
66 grid_string (grid_t *grid)
70 char *s = grid->string;
72 for (y = 0; y < 4; y++) {
73 for (x = 0; x < 4; x++) {
74 c = grid->letters[y][x];
90 #define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
92 grid_enumerate (grid_t *grid,
97 dict_cursor_t dict_cursor)
102 if (dict_cursor == DICT_CURSOR_NIL)
105 if (x < 0 || x >= 4 ||
107 seen & SEEN_BIT (x, y))
112 seen |= SEEN_BIT (x, y);
114 c = grid->letters[y][x];
115 word[strlen (word)] = c;
116 dict_cursor = dict_cursor_next (dict_cursor, c);
119 word[strlen (word)] = 'u';
120 dict_cursor = dict_cursor_next (dict_cursor, 'u');
123 if (strlen (word) > 2 &&
124 DICT_ENTRY_IS_WORD (dict_cursor_resolve (dict_cursor)))
126 dict_add_word (grid->results, word);
129 for (dy = -1; dy <= 1; dy++)
130 for (dx = -1; dx <= 1; dx++)
131 grid_enumerate (grid, x + dx, y + dy, seen, word, dict_cursor);
134 word [strlen (word) - 1] = '\0';
135 word [strlen (word) - 1] = '\0';
139 grid_solve (grid_t *grid, dict_t *dict, dict_t *solution)
145 grid->results = solution;
147 memset (word, '\0', 18);
149 for (y = 0; y < 4; y++)
150 for (x = 0; x < 4; x++)
151 grid_enumerate (grid, x, y, seen, word, dict_root (dict));