From 896bfd48616953ca8cbe848953b46abdb20f78a3 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 28 Sep 2006 20:50:09 -0700 Subject: [PATCH] Split grid into main program in grid4.c and library functions in grid.c --- .gitignore | 2 +- Makefile | 4 ++-- grid.c | 61 +++++++----------------------------------------------- grid.h | 50 ++++++++++++++++++++++++++++++++++++++++++++ grid4.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 56 deletions(-) create mode 100644 grid.h create mode 100644 grid4.c diff --git a/.gitignore b/.gitignore index f3b3bf4..cbf8982 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ Makefile.dep drill2 -grid +grid4 rack *.o *~ diff --git a/Makefile b/Makefile index b8f940d..d7441aa 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ 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 $@ $^ diff --git a/grid.c b/grid.c index 8f30a1b..eac8488 100644 --- a/grid.c +++ b/grid.c @@ -16,12 +16,10 @@ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA." */ -#include "word-game.h" +#include "grid.h" #include #include -#include -#include char *cube_faces[16] = { "aaeeng", "abbjoo", "achops", "affkps", @@ -30,13 +28,6 @@ char *cube_faces[16] = { "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) { @@ -57,7 +48,7 @@ shuffle (int *array, int length) } } -static void +void grid_init (grid_t *grid) { int i; @@ -71,21 +62,12 @@ grid_init (grid_t *grid) 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++) { @@ -101,6 +83,8 @@ grid_to_string (grid_t *grid, *s++ = '\n'; } *s = '\0'; + + return grid->string; } #define SEEN_BIT(x, y) (1 << (4*(y)+(x))) @@ -151,7 +135,7 @@ grid_enumerate (grid_t *grid, word [strlen (word) - 1] = '\0'; } -static void +void grid_solve (grid_t *grid, dict_t *dict, dict_t *solution) { int x, y; @@ -166,32 +150,3 @@ grid_solve (grid_t *grid, dict_t *dict, dict_t *solution) 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; -} diff --git a/grid.h b/grid.h new file mode 100644 index 0000000..3d200c7 --- /dev/null +++ b/grid.h @@ -0,0 +1,50 @@ +/* + * 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_ */ diff --git a/grid4.c b/grid4.c new file mode 100644 index 0000000..96cb5e3 --- /dev/null +++ b/grid4.c @@ -0,0 +1,51 @@ +/* + * 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 +#include + +#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; +} -- 2.43.0