]> git.cworth.org Git - wordgame/blobdiff - rack-fancy.c
Fix to not give hints by putting answers found so far in their alphabetical position.
[wordgame] / rack-fancy.c
index 80279637458e08775e9d2ae9353bd0eb03389c82..834492af00ba8ca33509444c5ec5e63941b7677d 100644 (file)
@@ -48,6 +48,7 @@ typedef struct _rack
     bag_t bag;
     dict_t dict;
     dict_t solution;
+    int solution_total;
     GooCanvasItem *solution_item;
     gboolean done;
 } rack_t;
@@ -69,6 +70,13 @@ rack_tile_position (int i, int *x, int *y)
     *y += (LETTER_SIZE + LETTER_PAD);
 }
 
+typedef enum dict_paint_cursor_show
+{
+    DICT_PAINT_CURSOR_SHOW_FOUND,
+    DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS,
+    DICT_PAINT_CURSOR_SHOW_ALL
+} dict_paint_cursor_show_t;
+
 typedef struct _dict_paint_cursor
 {
     cairo_t *cr;
@@ -77,7 +85,7 @@ typedef struct _dict_paint_cursor
     int y;
     int max_column_width;
     int max_y;
-    gboolean show_all;
+    dict_paint_cursor_show_t show;
 } dict_paint_cursor_t;
 
 static void
@@ -86,21 +94,38 @@ dict_paint_action (void *closure, char *word, dict_entry_t *entry)
     dict_paint_cursor_t *cursor = closure;
     cairo_t *cr = cursor->cr;
     double new_x, new_y;
+    int found, show_blanks = FALSE;
 
     if (strlen (word) < 3)
        return;
 
-    cairo_move_to (cr, cursor->x, cursor->y);
-    if (*entry & RACK_DICT_ENTRY_FOUND || cursor->show_all) {
-       if (*entry & RACK_DICT_ENTRY_FOUND)
-           cairo_set_source_rgb (cr, 0, 0, 0); /* black */
-       else
+    found = *entry & RACK_DICT_ENTRY_FOUND;
+
+    cairo_set_source_rgb (cr, 0, 0, 0); /* black */
+
+    switch (cursor->show) {
+    case DICT_PAINT_CURSOR_SHOW_FOUND:
+       if (! found)
+           return;
+       break;
+    case DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS:
+       if (found)
+           return;
+       show_blanks = TRUE;
+       break;
+    case DICT_PAINT_CURSOR_SHOW_ALL:
+       if (! found)
            cairo_set_source_rgb (cr, 1, 0, 0); /* red */
-       cairo_show_text (cr, word);
-    } else {
+       break;
+    }
+
+    cairo_move_to (cr, cursor->x, cursor->y);
+    if (show_blanks) {
        int i, length = strlen (word);
        for (i = 0; i < length; i++)
            cairo_show_text (cr, "_");
+    } else {
+       cairo_show_text (cr, word);
     }
     cairo_get_current_point (cr, &new_x, &new_y);
     if (new_x > cursor->max_column_width)
@@ -120,6 +145,7 @@ dict_paint (cairo_t *cr, void *closure, double width, double height)
 {
     rack_t *rack = closure;
     dict_paint_cursor_t cursor;
+    int length, count;
 
     cairo_save (cr);
     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
@@ -136,11 +162,24 @@ dict_paint (cairo_t *cr, void *closure, double width, double height)
     cursor.max_column_width = 0;
     cursor.max_y = height;
 
-    cursor.show_all = rack->done;
-
-    dict_for_each_by_length (&rack->solution,
-                            dict_paint_action,
-                            &cursor);
+    length = 1;
+    count = 0;
+    do {
+       if (rack->done)
+           cursor.show = DICT_PAINT_CURSOR_SHOW_ALL;
+       else
+           cursor.show = DICT_PAINT_CURSOR_SHOW_FOUND;
+       count += dict_for_each_of_length (&rack->solution,
+                                         dict_paint_action, &cursor,
+                                         length, length);
+       if (! rack->done) {
+           cursor.show = DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS;
+           dict_for_each_of_length (&rack->solution,
+                                    dict_paint_action, &cursor,
+                                    length, length);
+       }
+       length++;
+    } while (count < rack->solution_total);
 
     cairo_restore (cr);
 }
@@ -285,6 +324,7 @@ rack_init (rack_t   *rack,
     dict_add_words_from_file (&rack->dict, "words.txt");
 
     dict_init (&rack->solution);
+    rack->solution_total = 0;
 
     for (i = 0; i < MAX_TILES; i++)
        rack->tiles[i] = tile_create (root, 'A', i);
@@ -321,6 +361,7 @@ rack_new_game (rack_t *rack)
 
     dict_init (&rack->solution);
     subanagram_expand (word, &rack->dict, &rack->solution);
+    rack->solution_total = dict_count (&rack->solution);
     goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
 }