]> git.cworth.org Git - wordgame/commitdiff
Ensure that there is at least one word using all letters in the rack.
authorCarl Worth <cworth@cworth.org>
Tue, 19 Dec 2006 08:53:03 +0000 (00:53 -0800)
committerCarl Worth <cworth@cworth.org>
Tue, 19 Dec 2006 08:53:03 +0000 (00:53 -0800)
dict.c
rack-fancy.c

diff --git a/dict.c b/dict.c
index b23da18b6024aca02e68a605a3a8037b56821eee..11ddaf1ebd1afe73db7ede92b7438c4622dd01f3 100644 (file)
--- a/dict.c
+++ b/dict.c
@@ -227,7 +227,8 @@ trie_for_each_of_length_if (trie_t          *trie,
     {
        count = 1;
 
-       action (closure, string->s, &trie->flags);
+       if (action)
+           action (closure, string->s, &trie->flags);
     }
 
     if (length == max_length)
index 834492af00ba8ca33509444c5ec5e63941b7677d..7f0542d0dc3fc0ba3523154a35fd4d1938b579f8 100644 (file)
@@ -334,35 +334,56 @@ rack_init (rack_t *rack,
 static void
 rack_new_game (rack_t *rack)
 {
-    int i;
-    char *draw;
-    char word[8];
-
-    /* Clean up any remnants from the last game */
-    dict_fini (&rack->solution);
-
-    bag_shuffle (&rack->bag);
+    int i, bottom;
+    char word[MAX_TILES + 1];
+    int length = MAX_TILES;
+    int count;
+
+    /* 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--;
+           }
+       }
 
-    /* Keep drawing until we get 7 non-blank tiles */
-    i = 0;
-    draw = rack->bag.tiles;
-    while (i < 7) {
-       if (*draw != '?')
-           word[i++] = *draw;
-       draw++;
+       for (i = 0; i + length <= bottom + 1; i++) {
+           memcpy (word, &rack->bag.tiles[i], length);
+           word[length] = '\0';
+           printf ("Candidate word %s\n", word);
+           dict_fini (&rack->solution);
+           dict_init (&rack->solution);
+           subanagram_expand (word, &rack->dict, &rack->solution);
+           count = dict_for_each_of_length (&rack->solution,
+                                            NULL, NULL,
+                                            length, length);
+           if (count)
+               goto DONE;
+           i++;
+       }
     }
-    word[7] = '\0';
 
-    for (i = 0; i < 7; i++) {
+  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 = 7;
-
-    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);
+    rack->num_tiles = length;
 }
 
 static gboolean