]> git.cworth.org Git - wordgame/blobdiff - rack-fancy.c
Ensure that there's at least one full-length word that's not obscure
[wordgame] / rack-fancy.c
index 834492af00ba8ca33509444c5ec5e63941b7677d..fbc6e119a471df41aadef06008cde4a2d3aecb8f 100644 (file)
@@ -27,8 +27,9 @@
 #include "word-game.h"
 #include "demo-item.h"
 
-#define RACK_DICT_ENTRY_FOUND (1<<1)
-#define MAX_TILES 7
+#define RACK_DICT_ENTRY_OBSCURE        (1<<1)
+#define RACK_DICT_ENTRY_FOUND  (1<<2)
+#define MAX_TILES 6
 
 typedef struct _tile
 {
@@ -47,6 +48,7 @@ typedef struct _rack
     int guess_length;
     bag_t bag;
     dict_t dict;
+    dict_t obscure;
     dict_t solution;
     int solution_total;
     GooCanvasItem *solution_item;
@@ -119,6 +121,13 @@ dict_paint_action (void *closure, char *word, dict_entry_t *entry)
        break;
     }
 
+    /* Don't bother displaying unfound obscure words. */
+    if (*entry & RACK_DICT_ENTRY_OBSCURE) {
+       if (! found)
+           return;
+       cairo_set_source_rgb (cr, 0, 1, 0); /* green */
+    }
+
     cairo_move_to (cr, cursor->x, cursor->y);
     if (show_blanks) {
        int i, length = strlen (word);
@@ -323,6 +332,9 @@ rack_init (rack_t   *rack,
     dict_init (&rack->dict);
     dict_add_words_from_file (&rack->dict, "words.txt");
 
+    dict_init (&rack->obscure);
+    dict_add_words_from_file (&rack->obscure, "obscure.txt");
+
     dict_init (&rack->solution);
     rack->solution_total = 0;
 
@@ -332,37 +344,84 @@ rack_init (rack_t *rack,
 }
 
 static void
-rack_new_game (rack_t *rack)
+_flag_obscure_word (void *closure, char *word, dict_entry_t *entry)
 {
-    int i;
-    char *draw;
-    char word[8];
+    dict_t *obscure = closure;
+    dict_entry_t *obscure_entry;
 
-    /* Clean up any remnants from the last game */
-    dict_fini (&rack->solution);
+    obscure_entry = dict_lookup (obscure, word);
+    if (DICT_ENTRY_IS_WORD (obscure_entry))
+       *entry |= RACK_DICT_ENTRY_OBSCURE;
+}
 
-    bag_shuffle (&rack->bag);
+static void
+_at_least_one_is_not_obscure (void *closure, char *word, dict_entry_t *entry)
+{
+    int *result = closure;
 
-    /* Keep drawing until we get 7 non-blank tiles */
-    i = 0;
-    draw = rack->bag.tiles;
-    while (i < 7) {
-       if (*draw != '?')
-           word[i++] = *draw;
-       draw++;
-    }
-    word[7] = '\0';
+    if ((*entry & RACK_DICT_ENTRY_OBSCURE) == 0)
+       *result = 1;
+}
 
-    for (i = 0; i < 7; i++) {
-       rack->tiles[i]->letter = toupper (word[i]);
-       goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->tiles[i]->item), FALSE);
+static void
+rack_new_game (rack_t *rack)
+{
+    int i, bottom;
+    char word[MAX_TILES + 1];
+    int length = MAX_TILES;
+    int has_full_length_non_obscure_word;
+
+    /* We'll shuffle as many times as necessary until we can find a
+     * sequence of <length> letters with at least one full-length
+     * word. */
+    while (1) {
+       bag_shuffle (&rack->bag);
+
+       /* In this game, we're not interested in blank tiles, so first
+        * find any blanks and sort them to the bottom of the bag. */
+       i = 0;
+       bottom = BAG_SIZE - 1;
+       for (i = 0; i < bottom; i++) {
+           if (rack->bag.tiles[i] == '?') {
+               rack->bag.tiles[i] = rack->bag.tiles[bottom];
+               rack->bag.tiles[bottom] = '?';
+               bottom--;
+               /* Re-examine ith element */
+               i--;
+           }
+       }
+
+       /* Look at each successive run of tiles in the bag until
+        * finding one that has at least one non-obscure word using
+        * all of its leters. */
+       for (i = 0; i + length <= bottom + 1; i++) {
+           memcpy (word, &rack->bag.tiles[i], length);
+           word[length] = '\0';
+           dict_fini (&rack->solution);
+           dict_init (&rack->solution);
+           subanagram_expand (word, &rack->dict, &rack->solution);
+           dict_for_each (&rack->solution,
+                          _flag_obscure_word, &rack->obscure);
+           has_full_length_non_obscure_word = 0;
+           dict_for_each_of_length (&rack->solution,
+                                    _at_least_one_is_not_obscure,
+                                    &has_full_length_non_obscure_word,
+                                    length, length);
+           if (has_full_length_non_obscure_word)
+               goto DONE;
+           i++;
+       }
     }
-    rack->num_tiles = 7;
 
-    dict_init (&rack->solution);
-    subanagram_expand (word, &rack->dict, &rack->solution);
+  DONE:
     rack->solution_total = dict_count (&rack->solution);
     goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
+
+    for (i = 0; i < length; i++) {
+       rack->tiles[i]->letter = toupper (word[i]);
+       goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->tiles[i]->item), FALSE);
+    }
+    rack->num_tiles = length;
 }
 
 static gboolean
@@ -500,7 +559,7 @@ create_window (rack_t *rack)
     GtkWidget *window, *scrolled_window;
 
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-    gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
+    gtk_window_set_default_size (GTK_WINDOW (window), 430, 430);
     gtk_widget_show (window);
     g_signal_connect (window, "delete_event",
                      (GtkSignalFunc) on_delete_event, NULL);
@@ -528,8 +587,8 @@ create_canvas (GtkWidget *parent, rack_t *rack)
     GooCanvasItem *root;
 
     canvas = goo_canvas_new ();
-    gtk_widget_set_size_request (canvas, 400, 480);
-    goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 480);
+    gtk_widget_set_size_request (canvas, 400, 400);
+    goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400);
     gtk_widget_show (canvas);
     gtk_container_add (GTK_CONTAINER (parent), canvas);
 
@@ -538,7 +597,7 @@ create_canvas (GtkWidget *parent, rack_t *rack)
     rack->solution_item = goo_demo_item_new (root,
                                             20,
                                             LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
-                                            400 - 20, 480 - (LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD)),
+                                            400 - 20, 400 - (LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD)),
                                             dict_paint, rack,
                                             NULL);