+ 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;
+ }