]> git.cworth.org Git - wordgame/commitdiff
Abstract subanagram code from rack into new bag.c
authorCarl Worth <cworth@cworth.org>
Fri, 1 Dec 2006 07:27:10 +0000 (23:27 -0800)
committerCarl Worth <cworth@cworth.org>
Fri, 1 Dec 2006 07:27:10 +0000 (23:27 -0800)
This will allow for some simpler sharing with the new rack-fancy

Makefile
rack.c
subanagram.c [new file with mode: 0644]
subanagram.h [new file with mode: 0644]
word-game.h

index 903deee3d4e999fd3b06e9ab27a93d1516fc8214..f8756be881f20d594884340048a7a843b3aaf27b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ FANCYLIBS=`pkg-config --libs goocanvas`
 PROGRAMS=grid4 grid5 drill2 rack rack-fancy
 all: $(PROGRAMS)
 
-LIBRARY=bag.o dict.o grid.o word-game.o
+LIBRARY=bag.o dict.o grid.o subanagram.o word-game.o
 FANCYLIBRARY=demo-item.o
 
 %: %.o $(LIBRARY)
diff --git a/rack.c b/rack.c
index 968a9ee20812565939ae3079ad264d769eccfc52..e3ef2096ba41cb9aa4d343d59f89eba9f823ccbe 100644 (file)
--- a/rack.c
+++ b/rack.c
 #include <sys/time.h>
 #include <time.h>
 
-static const char *subanagram_anagram;
-static char subanagram_enumerate_word[WORD_GAME_MAX_WORD_LENGTH];
-static dict_t *subanagram_enumerate_answers;
-
-static void
-subanagram_enumerate (dict_cursor_t     cursor,
-                     uint8_t            seen)
-{
-    char c;
-    unsigned int i, last;
-    uint8_t next_seen;
-
-    if (cursor == DICT_CURSOR_NIL)
-       return;
-
-    if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
-       dict_add_word (subanagram_enumerate_answers, subanagram_enumerate_word);
-
-    last = strlen (subanagram_enumerate_word);
-
-    for (i = 0; i < strlen (subanagram_anagram); i++) {
-       if (seen & (1 << i))
-           continue;
-       next_seen = seen | (1 << i);
-       c = subanagram_anagram[i];
-       if (c == '?' || c == '_') {
-           for (c = 'a'; c <= 'z'; c++) {
-               subanagram_enumerate_word[last] = c;
-               subanagram_enumerate (dict_cursor_next (cursor, c), next_seen);
-           }
-       } else {
-           subanagram_enumerate_word[last] = c;
-           subanagram_enumerate (dict_cursor_next (cursor, c), next_seen);
-       }
-       subanagram_enumerate_word[last] = '\0';
-    }
-}
-
-static void
-subanagram_expand (const char *anagram, dict_t *dict, dict_t *answers)
-{
-    subanagram_anagram = anagram;
-    memset (subanagram_enumerate_word, '\0', WORD_GAME_MAX_WORD_LENGTH);
-    subanagram_enumerate_answers = answers;
-
-    subanagram_enumerate (dict_root (dict), 0);
-}
-
 static int
 character_compare (const void *_a,
                   const void *_b)
diff --git a/subanagram.c b/subanagram.c
new file mode 100644 (file)
index 0000000..b726a40
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Copyright © 2006 Carl Worth
+ *
+ * This program is free software; you can redistribute it and\/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
+ */
+
+#include "word-game.h"
+
+#include <string.h>
+
+static const char *subanagram_letters;
+static char subanagram_enumerate_word[WORD_GAME_MAX_WORD_LENGTH];
+static dict_t *subanagram_enumerate_answers;
+
+static void
+subanagram_enumerate (dict_cursor_t     cursor,
+                     uint8_t            seen)
+{
+    char c;
+    unsigned int i, last;
+    uint8_t next_seen;
+
+    if (cursor == DICT_CURSOR_NIL)
+       return;
+
+    if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor)))
+       dict_add_word (subanagram_enumerate_answers, subanagram_enumerate_word);
+
+    last = strlen (subanagram_enumerate_word);
+
+    for (i = 0; i < strlen (subanagram_letters); i++) {
+       if (seen & (1 << i))
+           continue;
+       next_seen = seen | (1 << i);
+       c = subanagram_letters[i];
+       if (c == '?' || c == '_') {
+           for (c = 'a'; c <= 'z'; c++) {
+               subanagram_enumerate_word[last] = c;
+               subanagram_enumerate (dict_cursor_next (cursor, c), next_seen);
+           }
+       } else {
+           subanagram_enumerate_word[last] = c;
+           subanagram_enumerate (dict_cursor_next (cursor, c), next_seen);
+       }
+       subanagram_enumerate_word[last] = '\0';
+    }
+}
+
+void
+subanagram_expand (const char *letters, dict_t *dict, dict_t *answers)
+{
+    subanagram_letters = letters;
+    memset (subanagram_enumerate_word, '\0', WORD_GAME_MAX_WORD_LENGTH);
+    subanagram_enumerate_answers = answers;
+
+    subanagram_enumerate (dict_root (dict), 0);
+}
diff --git a/subanagram.h b/subanagram.h
new file mode 100644 (file)
index 0000000..32b614e
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright © 2006 Carl Worth
+ *
+ * This program is free software; you can redistribute it and\/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
+ */
+
+#ifndef _SUBANAGRAM_H_
+#define _SUBANAGRAM_H_
+
+void
+subanagram_expand (const char *letters, dict_t *dict, dict_t *answers);
+
+#endif /* _SUBANAGRAM_H_ */
index 9088a8904df1e521053bac063483b5db772baf70..a4b36090cadddb9abba5ac22bc441c5b8232c15d 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "dict.h"
 #include "bag.h"
+#include "subanagram.h"
 
 #define WORD_GAME_MAX_WORD_LENGTH 15