]> git.cworth.org Git - wordgame/blobdiff - grid.c
grid: Add the ability to print stats at any point in the game.
[wordgame] / grid.c
diff --git a/grid.c b/grid.c
index 960b79626ebead506ffdc371b35375801d2a6634..0ebd7863a32e3ba5b309271eae5ede69d0346169 100644 (file)
--- a/grid.c
+++ b/grid.c
@@ -45,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;
@@ -65,7 +65,7 @@ shuffle (int *array, int length)
     }
 }
 
-void
+static void
 board_init (board_t *board)
 {
     int i;
@@ -79,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;
@@ -98,7 +98,7 @@ 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,
@@ -147,11 +147,11 @@ 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++)
@@ -170,16 +170,37 @@ unseen_predicate (dict_entry_t entry)
     return ! seen_predicate (entry);
 }
 
+static void
+_count_possible (dict_cursor_t cursor,
+                int            depth,
+                int            possible[17])
+{
+    char c;
+
+    if (cursor == DICT_CURSOR_NIL)
+       return;
+
+    if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
+       possible[depth]++;
+
+    for (c = 'a'; c <= 'z'; c++)
+       _count_possible (dict_cursor_next (cursor, c), depth + 1, possible);
+}
+
 #define GAME_LENGTH (3 * 60)
 int
 main (void)
 {
+    int i;
     dict_t dict, solution;
     board_t board;
     struct timeval tv, tv_stop;
     int remaining, minutes, seconds;
-    int found, missed;
+    /* 16 tiles + Qu on one tile = max 17 letters in one word. */
+    int found[17], possible[17];
+    int found_total, possible_total;
     char prompt[7], *response;
+    bool_t just_saw_board;
 
     gettimeofday (&tv, NULL);
     srand (tv.tv_sec ^ tv.tv_usec);
@@ -191,7 +212,18 @@ main (void)
     dict_init (&solution);
     board_solve (&board, &dict, &solution);
 
+    for (i=0; i < 17; i++) {
+       found[i] = 0;
+       possible[i] = 0;
+    }
+    _count_possible (dict_root (&solution), 0, possible);
+    found_total = 0;
+    possible_total = 0;
+    for (i=0; i < 17; i++)
+       possible_total += possible[i];
+
     board_print (&board);
+    just_saw_board = TRUE;
 
     gettimeofday (&tv, NULL);
     tv_stop = tv;
@@ -204,17 +236,31 @@ main (void)
        response = readline (prompt);
        add_history (response);
        if (strlen (response) == 0) {
-           board_print (&board);
+           if (! just_saw_board) {
+               board_print (&board);
+               just_saw_board = TRUE;
+           } else {
+               for (i = 2; i <= 17; i++)
+                   if (possible[i])
+                       printf ("%d: (%d/%d) ", i, found[i], possible[i]);
+               printf ("total: (%d/%d = %.2f%%)\n",
+                       found_total, possible_total,
+                       100 * (double) found_total / possible_total);
+           }
        } else {
            dict_entry_t *entry;
+           just_saw_board = FALSE;
            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)
+               if (*entry & GRID_WORD_SEEN) {
                    printf ("(repeat)\n");
-               else
+               } else {
                    *entry |= GRID_WORD_SEEN;
+                   found [strlen (response)]++;
+                   found_total++;
+               }
            } else {
                entry = dict_lookup (&dict, response);
                if (DICT_ENTRY_IS_WORD (entry))
@@ -229,16 +275,18 @@ main (void)
        minutes = remaining / 60;
     } while (remaining > 0);
 
-    printf ("\nWords you found:\n");
-    found = dict_print_by_length_if (&solution, seen_predicate);
+    board_print (&board);
+
+    printf ("Words you found:\n");
+    dict_print_by_length_if (&solution, seen_predicate);
 
     printf ("\nWords you missed:\n");
-    missed = dict_print_by_length_if (&solution, unseen_predicate);
+    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));
+           found_total, possible_total,
+           100 * (double) found_total / possible_total);
 
     dict_fini (&dict);