From 4af5ce4dd24d24df0909f75106c06514f565954d Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Sat, 23 Sep 2006 01:32:25 -0700 Subject: [PATCH] Add new drill2 game for drilling 2-letter words. --- .gitignore | 1 + Makefile | 4 +- drill2.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 drill2.c diff --git a/.gitignore b/.gitignore index 6793c73..0d0835b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ Makefile.dep +drill2 grid *.o *~ diff --git a/Makefile b/Makefile index 2e1e7e0..dd05ade 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ WGCFLAGS=-Wall -Wextra -Wmissing-prototypes -Wno-unused-parameter -PROGRAMS=grid +PROGRAMS=grid drill2 all: $(PROGRAMS) LIBRARY=dict.o word-game.o -grid: grid.o $(LIBRARY) +%: %.o $(LIBRARY) $(CC) $(CFLAGS) $(WGCFLAGS) $(LDFLAGS) -lreadline -lm -o $@ $^ %.o: %.c diff --git a/drill2.c b/drill2.c new file mode 100644 index 0000000..6f4a26c --- /dev/null +++ b/drill2.c @@ -0,0 +1,129 @@ +/* + * 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 +#include +#include +#include + +static int +rand_within (int num_values) +{ + return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0))); +} + +static void +shuffle (int *array, int length) +{ + int i, r, tmp; + + for (i = 0; i < length; i++) + { + r = i + rand_within (length - i); + tmp = array[i]; + array[i] = array[r]; + array[r] = tmp; + } +} + +static char pattern_enumerate_word[WORD_GAME_MAX_WORD_LENGTH]; +static dict_t *pattern_enumerate_answers; + +static void +pattern_enumerate (const char *pattern_chunk, + dict_cursor_t cursor) +{ + char c = *pattern_chunk; + int last; + + if (cursor == DICT_CURSOR_NIL) + return; + + if (c == '\0') { + if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor))) + dict_add_word (pattern_enumerate_answers, pattern_enumerate_word); + return; + } + + last = strlen (pattern_enumerate_word); + + if (c == '?' || c == '_') { + for (c = 'a'; c <= 'z'; c++) { + pattern_enumerate_word[last] = c; + pattern_enumerate (pattern_chunk + 1, dict_cursor_next (cursor, c)); + } + } else { + pattern_enumerate_word[last] = c; + pattern_enumerate (pattern_chunk + 1, dict_cursor_next (cursor, c)); + } + + pattern_enumerate_word[last] = '\0'; +} + +static void +pattern_expand (const char *pattern, dict_t *dict, dict_t *answers) +{ + memset (pattern_enumerate_word, '\0', WORD_GAME_MAX_WORD_LENGTH); + pattern_enumerate_answers = answers; + + pattern_enumerate (pattern, dict_root (dict)); +} + +int +main (void) +{ + int i, p, puzzle[52]; + char puzzle_string[3]; + dict_t dict, solution; + struct timeval tv; + + gettimeofday (&tv, NULL); + srand (tv.tv_sec ^ tv.tv_usec); + + for (i = 0; i < 52; i++) + puzzle[i] = i; + shuffle (puzzle, 52); + + dict_init (&dict); + dict_add_words_from_file (&dict, "words.txt"); + + puzzle_string[2] = '\0'; + + for (i = 0; i < 52; i++) { + p = puzzle[i]; + if (p < 26) { + puzzle_string[0] = 'a' + p; + puzzle_string[1] = '?'; + } else { + puzzle_string[0] = '?'; + puzzle_string[1] = 'a' + (p - 26); + } + dict_init (&solution); + pattern_expand (puzzle_string, &dict, &solution); + + word_game_play (puzzle_string, &dict, &solution, 0); + + dict_fini (&solution); + } + + dict_fini (&dict); + + return 0; +} -- 2.43.0