]> git.cworth.org Git - wordgame/blobdiff - rack-fancy.c
Pass the dict entry flags to the for_each callback
[wordgame] / rack-fancy.c
index 393c7bed85396b4bf0da36bb5688514fddfca16c..87b39f56c74283d05bd34f4382d00d362b434365 100644 (file)
@@ -42,13 +42,13 @@ typedef struct _rack
 {
     tile_t *tiles[MAX_TILES];
     int num_tiles;
+    char guess[MAX_TILES+1];
+    int guess_length;
+    dict_t *solution;
 } rack_t;
 
 static rack_t the_rack;
 
-static char the_guess[MAX_TILES];
-static int the_guess_index = 0;
-
 #define LETTER_SIZE 60
 #define LETTER_PAD 5
 
@@ -77,7 +77,7 @@ typedef struct _dict_paint_cursor
 } dict_paint_cursor_t;
 
 static void
-dict_paint_action (void *closure, char *word)
+dict_paint_action (void *closure, char *word, dict_entry_t *entry)
 {
     dict_paint_cursor_t *cursor = closure;
     cairo_t *cr = cursor->cr;
@@ -243,7 +243,7 @@ shuffle (int *array, int length)
 }
 
 static void
-rack_init (rack_t *rack, GooCanvasItem *parent, char *word)
+rack_init (rack_t *rack, GooCanvasItem *parent, char *word, dict_t *solution)
 {
     int i;
 
@@ -252,6 +252,8 @@ rack_init (rack_t *rack, GooCanvasItem *parent, char *word)
     rack->num_tiles = i;
     while (i < MAX_TILES)
        rack->tiles[i] = NULL;
+    rack->guess_length = 0;
+    rack->solution = solution;
 }
 
 static gboolean
@@ -274,6 +276,31 @@ rack_shuffle (rack_t *rack)
     return TRUE;
 }
 
+static void
+rack_return_tile (rack_t *rack, tile_t *tile)
+{
+    int x, y;
+
+    rack_tile_position (tile->rack_index, &x, &y);
+    tile_glide_to (tile, x, y);
+    tile->guessed = FALSE;
+    rack->guess_length--;
+    rack->guess[rack->guess_length] = '\0';
+}
+
+static void
+rack_return_all (rack_t *rack)
+{
+    int i;
+
+    for (i = 0; i < rack->num_tiles; i++) {
+       if (rack->tiles[i]->guessed)
+           rack_return_tile (rack, rack->tiles[i]);
+    }
+    rack->guess_length = 0;
+    rack->guess[0] = '\0';
+}
+
 static gboolean
 on_key_press_event (GtkWidget  *widget,
                    GdkEventKey *event,
@@ -283,15 +310,19 @@ on_key_press_event (GtkWidget     *widget,
     char guess_letter;
     rack_t *rack = user_data;
 
+    if (event->keyval == GDK_Return) {
+       dict_entry_t *entry;
+       entry = dict_lookup (rack->solution, rack->guess);
+       if (DICT_ENTRY_IS_WORD (entry))
+           printf ("%s is good\n", rack->guess);
+       else
+           printf ("%s is not a word\n", rack->guess);
+       rack_return_all (rack);
+       return TRUE;
+    }
+
     if (event->keyval == GDK_space) {
-       for (i = 0; i < rack->num_tiles; i++) {
-           if (rack->tiles[i]->guessed) {
-               rack_tile_position (rack->tiles[i]->rack_index, &x, &y);
-               tile_glide_to (rack->tiles[i], x, y);
-               rack->tiles[i]->guessed = FALSE;
-           }
-       }
-       the_guess_index = 0;
+       rack_return_all (rack);
        rack_shuffle (rack);
        return TRUE;
     }
@@ -302,7 +333,7 @@ on_key_press_event (GtkWidget       *widget,
        x = 0;
        for (i = 0; i < rack->num_tiles; i++) {
            /* XXX: evil stuff here... please refactor a lot */
-           if (the_guess[the_guess_index-1] == rack->tiles[i]->letter &&
+           if (rack->guess[rack->guess_length-1] == rack->tiles[i]->letter &&
                rack->tiles[i]->guessed &&
                rack->tiles[i]->x > x)
            {
@@ -311,10 +342,7 @@ on_key_press_event (GtkWidget      *widget,
            }
        }
        if (found) {
-           rack_tile_position (rack->tiles[found_index]->rack_index, &x, &y);
-           tile_glide_to (rack->tiles[found_index], x, y);
-           rack->tiles[found_index]->guessed = FALSE;
-           the_guess_index--;
+           rack_return_tile (rack, rack->tiles[found_index]);
            return TRUE;
        }
        return FALSE;
@@ -328,10 +356,11 @@ on_key_press_event (GtkWidget     *widget,
        if (guess_letter == rack->tiles[i]->letter && 
            ! rack->tiles[i]->guessed)
        {
-           guess_tile_position (the_guess_index, &x, &y);
+           guess_tile_position (rack->guess_length, &x, &y);
            tile_glide_to (rack->tiles[i], x, y);
            rack->tiles[i]->guessed = TRUE;
-           the_guess[the_guess_index++] = guess_letter;
+           rack->guess[rack->guess_length++] = guess_letter;
+           rack->guess[rack->guess_length] = '\0';
            return TRUE;
        }
     }
@@ -380,7 +409,7 @@ create_canvas (GtkWidget *parent, char *word, dict_t *solution)
 
     root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
 
-    rack_init (&the_rack, root, word);
+    rack_init (&the_rack, root, word, solution);
 
     goo_demo_item_new (root,
                       LETTER_PAD,