Makefile.dep
drill2
-grid
+grid4
rack
*.o
*~
WGCFLAGS=-Wall -Wextra -Wmissing-prototypes -Wno-unused-parameter
-PROGRAMS=grid drill2 rack
+PROGRAMS=grid4 drill2 rack
all: $(PROGRAMS)
-LIBRARY=dict.o word-game.o
+LIBRARY=dict.o grid.o word-game.o
%: %.o $(LIBRARY)
$(CC) $(CFLAGS) $(WGCFLAGS) $(LDFLAGS) -lreadline -lm -o $@ $^
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
*/
-#include "word-game.h"
+#include "grid.h"
#include <string.h>
#include <ctype.h>
-#include <sys/time.h>
-#include <time.h>
char *cube_faces[16] = {
"aaeeng", "abbjoo", "achops", "affkps",
"eiosst", "elrtty", "himnqu", "hlnnrz"
};
-typedef struct _grid {
- char letters[4][4];
-
- /* Private, transient state used by enumerate */
- dict_t *results;
-} grid_t;
-
static int
rand_within (int num_values)
{
}
}
-static void
+void
grid_init (grid_t *grid)
{
int i;
grid->letters[i / 4][i % 4] = cube_faces[cubes[i]][rand_within(6)];
}
-/* ( 3 chars per cell
- * x 4 cells per row
- * + 1 newline per row
- * ) x 4 rows per grid
- * + 1 terminator character
- * = 53
- */
-#define GRID_STRING_MAX 53
-static void
-grid_to_string (grid_t *grid,
- char grid_string[GRID_STRING_MAX])
+char *
+grid_string (grid_t *grid)
{
char c;
int x, y;
- char *s = &grid_string[0];
+ char *s = grid->string;
for (y = 0; y < 4; y++) {
for (x = 0; x < 4; x++) {
*s++ = '\n';
}
*s = '\0';
+
+ return grid->string;
}
#define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
word [strlen (word) - 1] = '\0';
}
-static void
+void
grid_solve (grid_t *grid, dict_t *dict, dict_t *solution)
{
int x, y;
for (x = 0; x < 4; x++)
grid_enumerate (grid, x, y, seen, word, dict_root (dict));
}
-
-#define GAME_LENGTH (3 * 60)
-int
-main (void)
-{
- dict_t dict, solution;
- grid_t grid;
- char grid_string[GRID_STRING_MAX];
- struct timeval tv;
-
- gettimeofday (&tv, NULL);
- srand (tv.tv_sec ^ tv.tv_usec);
-
- dict_init (&dict);
- dict_add_words_from_file (&dict, "words.txt");
-
- grid_init (&grid);
- grid_to_string (&grid, grid_string);
-
- dict_init (&solution);
- grid_solve (&grid, &dict, &solution);
-
- word_game_play (grid_string, &dict, &solution, GAME_LENGTH);
-
- dict_fini (&solution);
- dict_fini (&dict);
-
- return 0;
-}
--- /dev/null
+/*
+ * Copyright © 2006 Carl Worth
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
+ */
+
+#ifndef _GRID_H_
+#define _GRID_H_
+
+#include "dict.h"
+
+/* ( 3 chars per cell
+ * x 4 cells per row
+ * + 1 newline per row
+ * ) x 4 rows per grid
+ * + 1 terminator character
+ * = 53
+ */
+#define GRID_STRING_MAX 53
+
+typedef struct _grid {
+ char letters[4][4];
+ char string[GRID_STRING_MAX];
+
+ /* Private, transient state used by enumerate */
+ dict_t *results;
+} grid_t;
+
+void
+grid_init (grid_t *grid);
+
+char *
+grid_string (grid_t *grid);
+
+void
+grid_solve (grid_t *grid, dict_t *dict, dict_t *solution);
+
+#endif /* _GRID_H_ */
--- /dev/null
+/*
+ * Copyright © 2006 Carl Worth
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
+ */
+
+#include "grid.h"
+#include "word-game.h"
+
+#include <sys/time.h>
+#include <time.h>
+
+#define GAME_LENGTH (3 * 60)
+int
+main (void)
+{
+ dict_t dict, solution;
+ grid_t grid;
+ struct timeval tv;
+
+ gettimeofday (&tv, NULL);
+ srand (tv.tv_sec ^ tv.tv_usec);
+
+ dict_init (&dict);
+ dict_add_words_from_file (&dict, "words.txt");
+
+ grid_init (&grid);
+
+ dict_init (&solution);
+ grid_solve (&grid, &dict, &solution);
+
+ word_game_play (grid_string (&grid),
+ &dict, &solution, GAME_LENGTH);
+
+ dict_fini (&solution);
+ dict_fini (&dict);
+
+ return 0;
+}