/*
* Copyright © 2006 Carl Worth
*
- * This program is free software; you can redistribute it and\/or modify
+ * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
#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);
- for (i = 0; i < 16; i++)
+ grid->size = size;
+ num_cubes = size * size;
+
+ if (size == 4)
+ cubes_source = cubes4;
+ else
+ cubes_source = cubes5;
+
+ 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)];
+}
+
+void
+grid_set_letters (grid_t *grid,
+ const char *letters)
+{
+ int i;
+ int num_cubes = grid->size * grid->size;
+ char letter;
+
+ if (strlen (letters) != num_cubes) {
+ fprintf (stderr, "Error: Invalid string for %dx%d grid. Expected %d letters: %s\n",
+ grid->size, grid->size, num_cubes, letters);
+ exit (1);
+ }
+
+ for (i = 0; i < num_cubes; i++) {
+ letter = tolower (letters[i]);
+ if (letter < 'a' || letter > 'z') {
+ fprintf (stderr, "Error: Invalid character '%c' in letters: %s\n",
+ letters[i], letters);
+ exit (1);
+ }
+ grid->letters[i / grid->size][i % grid->size] = letter;
+ }
}
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);
else
*s++ = ' ';
}
- if (y != 3)
+ if (y != (grid->size - 1))
*s++ = '\n';
}
*s = '\0';
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;
dict_cursor = dict_cursor_next (dict_cursor, 'u');
}
- if (strlen (word) > 2 &&
+ /* For the 4x4 grid any word of length 3 or more counts.
+ * For the 5x5 grid any word of length 4 or more counts.
+ */
+ if (strlen (word) >= (grid->size - 1) &&
DICT_ENTRY_IS_WORD (dict_cursor_resolve (dict_cursor)))
{
dict_add_word (grid->results, word);
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));
}