]> git.cworth.org Git - wordgame/blobdiff - grid.c
Don't print empty found/missed sections
[wordgame] / grid.c
diff --git a/grid.c b/grid.c
index 5b57a51964d01d9a7286c679504d6d241cf762c0..76428d98bad2b6275c635f635bbef7fdc0d4b852 100644 (file)
--- a/grid.c
+++ b/grid.c
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
  */
 
-#include <stdio.h>
-#include <stdlib.h>
+#include "word-game.h"
+
+#include <string.h>
 #include <ctype.h>
 #include <sys/time.h>
 #include <time.h>
-#include <math.h>
-
-#include <readline/readline.h>
-#include <readline/history.h>
-
-#include "dict.h"
-
-/* Remember that dict reserves the 0th bit for IS_WORD */
-#define GRID_WORD_SEEN (1<<1)
 
 char *cube_faces[16] = {
     "aaeeng", "abbjoo", "achops", "affkps",
@@ -79,22 +71,36 @@ board_init (board_t *board)
        board->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 board
+ *   + 1 terminator character
+ * = 53
+ */
+#define BOARD_STRING_MAX 53
 static void
-board_print (board_t *board)
+board_to_string (board_t       *board,
+                char            board_string[BOARD_STRING_MAX])
 {
-    int x, y;
     char c;
+    int x, y;
+    char *s = &board_string[0];
 
-    printf ("\n");
     for (y = 0; y < 4; y++) {
        for (x = 0; x < 4; x++) {
            c = board->letters[y][x];
-           printf (" %c%s", toupper (c),
-                   c == 'q' ? "u" : " ");
+           *s++ = ' ';
+           *s++ = toupper (c);
+           if (c == 'q')
+               *s++ = 'u';
+           else
+               *s++ = ' ';
        }
-       printf ("\n");
+       if (y != 3)
+           *s++ = '\n';
     }
-    printf ("\n");
+    *s = '\0';
 }
 
 #define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
@@ -147,101 +153,41 @@ board_solve (board_t *board, dict_t *dict, dict_t *solution)
 {
     int x, y;
     int16_t seen = 0;
-    char word[17];
+    char word[18];
 
     board->results = solution;
 
-    memset (word, '\0', 17);
+    memset (word, '\0', 18);
 
     for (y = 0; y < 4; y++)
        for (x = 0; x < 4; x++)
            board_enumerate (board, x, y, seen, word, dict_root (dict));
 }
 
-static bool_t
-seen_predicate (dict_entry_t entry)
-{
-    return entry & GRID_WORD_SEEN;
-}
-
-static bool_t
-unseen_predicate (dict_entry_t entry)
-{
-    return ! seen_predicate (entry);
-}
-
 #define GAME_LENGTH (3 * 60)
 int
 main (void)
 {
     dict_t dict, solution;
     board_t board;
-    struct timeval tv, tv_stop;
-    int remaining, minutes, seconds;
-    int found, missed;
-    char prompt[7], *response;
+    char board_string[BOARD_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");
+
     board_init (&board);
+    board_to_string (&board, board_string);
 
     dict_init (&solution);
     board_solve (&board, &dict, &solution);
 
-    board_print (&board);
-
-    gettimeofday (&tv, NULL);
-    tv_stop = tv;
-    tv_stop.tv_sec += GAME_LENGTH;
-    remaining = GAME_LENGTH;
-    do {
-       minutes = remaining / 60;
-       seconds = remaining % 60;
-       sprintf (prompt, "%02d:%02d ", minutes, seconds);
-       response = readline (prompt);
-       add_history (response);
-       if (strlen (response) == 0) {
-           board_print (&board);
-       } else {
-           dict_entry_t *entry;
-           if (response[strlen (response) - 1] == '\n')
-               response[strlen (response) - 1] = '\0';
-           entry = dict_lookup (&solution, response);
-           if (DICT_ENTRY_IS_WORD (entry)) {
-               if (*entry & GRID_WORD_SEEN)
-                   printf ("(repeat)\n");
-               else
-                   *entry |= GRID_WORD_SEEN;
-           } else {
-               entry = dict_lookup (&dict, response);
-               if (DICT_ENTRY_IS_WORD (entry))
-                   printf ("(a good word, but it's not in the puzzle)\n");
-               else
-                   printf ("*** %s is not a word\n", response);
-           }
-       }
-       free (response);
-       gettimeofday (&tv, NULL);
-       remaining = floor (0.5 + (tv_stop.tv_sec - tv.tv_sec) + (tv_stop.tv_usec - tv.tv_usec) / 1000000.0);
-       minutes = remaining / 60;
-    } while (remaining > 0);
-
-    board_print (&board);
-
-    printf ("Words you found:\n");
-    found = dict_print_by_length_if (&solution, seen_predicate);
-
-    printf ("\nWords you missed:\n");
-    missed = dict_print_by_length_if (&solution, unseen_predicate);
-    printf ("\n");
-
-    printf ("You found %d of %d words (%.2f%%)\n",
-           found, found + missed,
-           100 * (double) found / (found + missed));
+    word_game_play (board_string, &dict, &solution, GAME_LENGTH);
 
+    dict_fini (&solution);
     dict_fini (&dict);
 
     return 0;