/* spellathon.c -- Spellathon Copyright (C) 2011 Stephen D. Cofer. Released under the GPL version 2 ONLY. 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 of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Usage: spellathon [spellathon-grid] Spellathon grid is the thirteen letters in the puzzle organized from left to right and top to bottom and passed as a single argument with no spaces. EXAMPLE: spellathon HTCYNLAILGBUR for H T C Y N L A I L G B U R You need the default word dictionary installed on your system (usually as /usr/share/dict/words); for other dictionaries or for Windows, manually adjust DICTIONARY below to point to a full dictionary. By default this is for 5-letter Spellathons, the most common. For a different one, you'll need to adjust some code. */ #include #include #include #include #include #include #define MAX_WORDS 100 #define DICTIONARY "/usr/share/dict/words" #define USAGE "USAGE:\nspellathon [spellathon-grid]\nSpellathon grid is the thirteen letters in the puzzle organized from left to right and top to bottom and passed as a single argument with no spaces.\nEXAMPLE:\nspellathon HTCYNLAILGBUR\n" char spell_nodes [13]; int node_links [13][12] = {{1,2,3,5,6,-1,-1,-1,-1,-1,-1,-1}, {0,2,4,6,7,-1,-1,-1,-1,-1,-1,-1}, {0,1,6,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {0,5,6,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {1,6,7,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {0,3,6,8,11,-1,-1,-1,-1,-1,-1,-1}, {0,1,2,3,4,5,7,8,9,10,11,12}, {1,4,6,9,12,-1,-1,-1,-1,-1,-1,-1}, {5,6,11,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {6,7,12,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {6,11,12,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {5,6,8,10,12,-1,-1,-1,-1,-1,-1,-1}, {6,7,9,10,11,-1,-1,-1,-1,-1,-1,-1}}; /* this could be done better or with actual node pointers but I'm lazy */ int found = 0; char *wordlist[MAX_WORDS]; int main (int argc, char*argv[]) { FILE *dict; char entry[1024]; int i; if (argc != 2) { printf ("Error: Not enough arguments\n"); printf (USAGE); return EXIT_FAILURE; } if (strlen (argv[1]) != 13) { printf ("Error: The spellathon grid must be exactly thirteen characters.\n"); printf (USAGE); return EXIT_FAILURE; } snprintf (spell_nodes, 14, "%s", argv[1]); for (i=0; i<13; i++) spell_nodes[i] = tolower(spell_nodes[i]); dict = fopen (DICTIONARY, "r"); if (dict == NULL) { int eno = errno; fprintf (stderr, "Error opening %s: %s\n", DICTIONARY, strerror(eno)); return EXIT_FAILURE; } while (fgets (entry, 1024, dict) != NULL) { int ent_len; ent_len = strlen (entry) -1; if (ent_len != 5) continue; entry[ent_len] = '\0'; /* fgets includes newline */ for (i=0; i<13; i++) { if (spell_nodes[i] == entry[0]) { find_next_node (i, entry, 1); } } } for (i=0; i