#include <string.h>
#include <ctype.h>
+#include <assert.h>
-char *cube_faces[16] = {
+#define GRID_CUBES_MAX (GRID_SIZE_MAX * GRID_SIZE_MAX)
+
+char *cubes4[16] = {
"aaeeng", "abbjoo", "achops", "affkps",
"aoottw", "cimotu", "deilrx", "delrvy",
"distty", "eeghnw", "eeinsu", "ehrtvw",
"eiosst", "elrtty", "himnqu", "hlnnrz"
};
+char *cubes5[25] = {
+ "aaafrs", "aaeeee", "aafirs", "adennn", "aeeeem",
+ "aeegmu", "aegmnn", "afirsy", "bjkqxz", "ccnstw",
+ "ceiilt", "ceilpt", "ceipst", "ddlnor", "dhhlor",
+ "dhhnot", "dhlnor", "eiiitt", "emottt", "ensssu",
+ "fiprsy", "gorrvw", "hiprry", "nootuw", "ooottu"
+};
+
static int
rand_within (int num_values)
{
}
void
-grid_init (grid_t *grid)
+grid_init (grid_t *grid, int size)
{
int i;
- int cubes[16];
+ int cubes[GRID_CUBES_MAX];
+ char **cubes_source;
+ int num_cubes;
+
+ assert (size == 4 || size == 5);
+
+ grid->size = size;
+ num_cubes = size * size;
+
+ if (size == 4)
+ cubes_source = cubes4;
+ else
+ cubes_source = cubes5;
- for (i = 0; i < 16; i++)
+ for (i = 0; i < num_cubes; i++)
cubes[i] = i;
- shuffle (cubes, 16);
+ shuffle (cubes, num_cubes);
- for (i = 0; i < 16; i++)
- grid->letters[i / 4][i % 4] = cube_faces[cubes[i]][rand_within(6)];
+ for (i = 0; i < num_cubes; i++)
+ grid->letters[i / grid->size][i % grid->size] =
+ cubes_source[cubes[i]][rand_within(6)];
}
char *
int x, y;
char *s = grid->string;
- for (y = 0; y < 4; y++) {
- for (x = 0; x < 4; x++) {
+ for (y = 0; y < grid->size; y++) {
+ for (x = 0; x < grid->size; x++) {
c = grid->letters[y][x];
*s++ = ' ';
*s++ = toupper (c);
return grid->string;
}
-#define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
+#define SEEN_BIT(x, y) (1 << ((grid->size)*(y)+(x)))
static void
grid_enumerate (grid_t *grid,
int x,
int y,
- int16_t seen,
+ int32_t seen,
char *word,
dict_cursor_t dict_cursor)
{
if (dict_cursor == DICT_CURSOR_NIL)
return;
- if (x < 0 || x >= 4 ||
- y < 0 || y >= 4 ||
+ if (x < 0 || x >= grid->size ||
+ y < 0 || y >= grid->size ||
seen & SEEN_BIT (x, y))
{
return;
grid_solve (grid_t *grid, dict_t *dict, dict_t *solution)
{
int x, y;
- int16_t seen = 0;
+ int32_t seen = 0;
char word[18];
grid->results = solution;
memset (word, '\0', 18);
- for (y = 0; y < 4; y++)
- for (x = 0; x < 4; x++)
+ for (y = 0; y < grid->size; y++)
+ for (x = 0; x < grid->size; x++)
grid_enumerate (grid, x, y, seen, word, dict_root (dict));
}
#include "dict.h"
+#define GRID_SIZE_MAX 5
/* ( 3 chars per cell
- * x 4 cells per row
+ * x GRID_SIZE_MAX cells per row
* + 1 newline per row
- * ) x 4 rows per grid
+ * ) x GRID_SIZE_MAX rows per grid
* + 1 terminator character
- * = 53
*/
-#define GRID_STRING_MAX 53
+#define GRID_STRING_MAX (((3 * GRID_SIZE_MAX + 1) * GRID_SIZE_MAX) + 1)
typedef struct _grid {
- char letters[4][4];
+ int size;
+ char letters[GRID_SIZE_MAX][GRID_SIZE_MAX];
char string[GRID_STRING_MAX];
/* Private, transient state used by enumerate */
dict_t *results;
} grid_t;
+/* size must be 4 or 5 */
void
-grid_init (grid_t *grid);
+grid_init (grid_t *grid, int size);
char *
grid_string (grid_t *grid);