]> git.cworth.org Git - wordgame/blobdiff - grid.c
Print board before result summary
[wordgame] / grid.c
diff --git a/grid.c b/grid.c
index 44ffa9d23f7487b90a8c0c71f542ae267d364c62..5b57a51964d01d9a7286c679504d6d241cf762c0 100644 (file)
--- a/grid.c
+++ b/grid.c
@@ -28,6 +28,9 @@
 
 #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",
     "aoottw", "cimotu", "deilrx", "delrvy",
@@ -42,13 +45,13 @@ typedef struct _board {
     dict_t *results;
 } board_t;
 
-int
+static int
 rand_within (int num_values)
 {
     return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
 }
 
-void
+static void
 shuffle (int *array, int length)
 {
     int i, r, tmp;
@@ -62,7 +65,7 @@ shuffle (int *array, int length)
     }
 }
 
-void
+static void
 board_init (board_t *board)
 {
     int i;
@@ -76,7 +79,7 @@ board_init (board_t *board)
        board->letters[i / 4][i % 4] = cube_faces[cubes[i]][rand_within(6)];
 }
 
-void
+static void
 board_print (board_t *board)
 {
     int x, y;
@@ -95,12 +98,12 @@ board_print (board_t *board)
 }
 
 #define SEEN_BIT(x, y) (1 << (4*(y)+(x)))
-void
+static void
 board_enumerate (board_t       *board,
                 int             x,
                 int             y,
                 int16_t         seen,
-                string_t       *word,
+                char           *word,
                 dict_cursor_t   dict_cursor)
 {
     char c;
@@ -119,42 +122,52 @@ board_enumerate (board_t  *board,
     seen |= SEEN_BIT (x, y);
 
     c = board->letters[y][x];
-    string_append_char (word, c);
+    word[strlen (word)] = c;
     dict_cursor = dict_cursor_next (dict_cursor, c);
 
     if (c == 'q') {
-       string_append_char (word, 'u');
+       word[strlen (word)] = 'u';
        dict_cursor = dict_cursor_next (dict_cursor, 'u');
     }
 
     if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (dict_cursor)))
-       dict_add_word (board->results, word->s);
+       dict_add_word (board->results, word);
 
     for (dy = -1; dy <= 1; dy++)
        for (dx = -1; dx <= 1; dx++)
            board_enumerate (board, x + dx, y + dy, seen, word, dict_cursor);
 
     if (c == 'q')
-       string_chop (word);
-    string_chop (word);
+       word [strlen (word) - 1] = '\0';
+    word [strlen (word) - 1] = '\0';
 }
 
-void
+static void
 board_solve (board_t *board, dict_t *dict, dict_t *solution)
 {
     int x, y;
     int16_t seen = 0;
-    string_t word;
+    char word[17];
 
     board->results = solution;
 
-    string_init (&word);
+    memset (word, '\0', 17);
 
     for (y = 0; y < 4; y++)
        for (x = 0; x < 4; x++)
-           board_enumerate (board, x, y, seen, &word, dict_root (dict));
+           board_enumerate (board, x, y, seen, word, dict_root (dict));
+}
+
+static bool_t
+seen_predicate (dict_entry_t entry)
+{
+    return entry & GRID_WORD_SEEN;
+}
 
-    string_fini (&word);
+static bool_t
+unseen_predicate (dict_entry_t entry)
+{
+    return ! seen_predicate (entry);
 }
 
 #define GAME_LENGTH (3 * 60)
@@ -167,7 +180,6 @@ main (void)
     int remaining, minutes, seconds;
     int found, missed;
     char prompt[7], *response;
-    string_t word;
 
     gettimeofday (&tv, NULL);
     srand (tv.tv_sec ^ tv.tv_usec);
@@ -191,17 +203,18 @@ main (void)
        sprintf (prompt, "%02d:%02d ", minutes, seconds);
        response = readline (prompt);
        add_history (response);
-       chomp (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 & TRIE_FLAGS_SEEN)
+               if (*entry & GRID_WORD_SEEN)
                    printf ("(repeat)\n");
                else
-                   *entry |= TRIE_FLAGS_SEEN;
+                   *entry |= GRID_WORD_SEEN;
            } else {
                entry = dict_lookup (&dict, response);
                if (DICT_ENTRY_IS_WORD (entry))
@@ -216,19 +229,16 @@ main (void)
        minutes = remaining / 60;
     } while (remaining > 0);
 
-    printf ("\nWords you found:\n");
-    string_init (&word);
-    found = trie_print_seen (&solution, &word);
-    string_fini (&word);
-    printf ("\n");
+    board_print (&board);
+
+    printf ("Words you found:\n");
+    found = dict_print_by_length_if (&solution, seen_predicate);
 
     printf ("\nWords you missed:\n");
-    string_init (&word);
-    missed = trie_print_unseen (&solution, &word);
-    string_fini (&word);
+    missed = dict_print_by_length_if (&solution, unseen_predicate);
     printf ("\n");
 
-    printf ("\nYou found %d of %d words (%.2f%%)\n",
+    printf ("You found %d of %d words (%.2f%%)\n",
            found, found + missed,
            100 * (double) found / (found + missed));