]> git.cworth.org Git - wordgame/commitdiff
Split grid into main program in grid4.c and library functions in grid.c
authorCarl Worth <cworth@raht.cworth.org>
Fri, 29 Sep 2006 03:50:09 +0000 (20:50 -0700)
committerCarl Worth <cworth@raht.cworth.org>
Fri, 29 Sep 2006 03:50:09 +0000 (20:50 -0700)
.gitignore
Makefile
grid.c
grid.h [new file with mode: 0644]
grid4.c [new file with mode: 0644]

index f3b3bf4c5de10544ed80ebb39cef8b40ef91163f..cbf8982f8d657cce88a39eea664a4f6d26f3995b 100644 (file)
@@ -1,6 +1,6 @@
 Makefile.dep
 drill2
-grid
+grid4
 rack
 *.o
 *~
index b8f940da2367b9a16fc3549dbdb33d5a2a579a93..d7441aa64aabbc65f8626d065ed20f314199e1d9 100644 (file)
--- 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 8f30a1b9082ab8f82da1580cca146440a5c7767c..eac8488799f348e2e0862772b35ac9231f0ca100 100644 (file)
--- a/grid.c
+++ b/grid.c
  * 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",
@@ -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 (file)
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 (file)
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 <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;
+}