From 013a96af89ee97c8da2699365d1d1da811c4896a Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Thu, 13 Jun 2024 17:15:50 +0200 Subject: [PATCH] add dictionary caching function --- function.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- function.h | 8 ++++++- scrabble.c | 25 +++++++++++++++---- todo.txt | 2 +- 4 files changed, 97 insertions(+), 8 deletions(-) diff --git a/function.c b/function.c index 94d406f..cec4f70 100644 --- a/function.c +++ b/function.c @@ -432,11 +432,11 @@ void findwords (word_t *words, play_t *play, play_t *turn) } } -void checkspelling (word_t *words, char *dict) +void checkspellingfromfile (word_t *words, char *dict) { - int i = 0; FILE *fd = fopen (dict, "r"); if (fd) { + int i; char str[128] = {0}; while (fscanf (fd, "%s", str) > 0) { int stop = 1; @@ -462,4 +462,70 @@ void checkspelling (word_t *words, char *dict) } } +#define NBWORDS 4096 + +char **cachedictionary (char *dict) +{ + char **listofwords = NULL; + int nbwords = 0; + FILE *fd = fopen (dict, "r"); + if (fd) { + char str[128] = {0}; + listofwords = (char **) calloc (NBWORDS + 1, sizeof (char *)); + CHECKALLOC (listofwords); + while (fscanf (fd, "%s", str) > 0) { + listofwords[nbwords] = strdup (str); + CHECKALLOC (listofwords[nbwords]); + nbwords++; + if (nbwords % NBWORDS == 0) { + listofwords = (char **) realloc (listofwords, (nbwords + NBWORDS + 1) * sizeof (char *)); + CHECKALLOC (listofwords); + } + } + fclose (fd); + } + return listofwords; +} + +void freecache (char **listofwords) +{ + if (listofwords) { + char **pt = listofwords; + while (*pt) { + free (*pt); + pt++; + } + } + free (listofwords); +} + + +void checkspellingfromcache (word_t *words, char **listofwords) +{ + if (listofwords) { + int i; + while (*listofwords) { + int stop = 1; + for (i = 0; i < words->maxnbwords; i++) { + if (words->status[i] == notchecked) { + if (strcmp (words->tab[i], *listofwords) == 0) { + words->status[i] = correct; + } else { + stop = 0; + } + } + } + listofwords++; + if (stop) { + break; + } + } + for (i = 0; i < words->maxnbwords; i++) { + if (words->status[i] == notchecked) { + words->status[i] = incorrect; + } + } + } +} + /* vim: set ts=4 sw=4 et: */ diff --git a/function.h b/function.h index 1aab1b6..fa27ebb 100644 --- a/function.h +++ b/function.h @@ -55,7 +55,13 @@ void freeword (word_t *words); void findwords (word_t *words, play_t *play, play_t *turn); -void checkspelling (word_t *words, char *dict); +void checkspellingfromfile (word_t *words, char *dict); + +char **cachedictionary (char *dict); + +void freecache (char **listofwords); + +void checkspellingfromcache (word_t *words, char **listofwords); #endif /* __FUNCTION_H__ */ diff --git a/scrabble.c b/scrabble.c index c289cae..d2d1b5a 100644 --- a/scrabble.c +++ b/scrabble.c @@ -28,6 +28,7 @@ char *boardname = "15x15-7"; char *language = "fr"; int nbplayers = 2; char *dict = NULL; +int cache = 0; int xoffset = 4; int yoffset = 3; @@ -50,8 +51,9 @@ char *help = int usage (int ret) { FILE *fd = ret ? stderr : stdout; - fprintf (fd, "usage: %s [-b board] [-d dict] [-h] [-l lang] [-n nb] [-v level]\n", progname); + fprintf (fd, "usage: %s [-b board] [-c] [-d dict] [-h] [-l lang] [-n nb] [-v level]\n", progname); fprintf (fd, " -b: board (%s)\n", boardname); + fprintf (fd, " -c: cache dictionary\n"); fprintf (fd, " -d: dictionary (%s)\n", (dict) ? dict : "none"); fprintf (fd, " -h: help message\n"); fprintf (fd, " -l: language (%s)\n", language); @@ -94,6 +96,9 @@ int main (int argc, char *argv[]) return usage (1); } break; + case 'c': + cache = 1; + break; case 'd': arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; if (arg) { @@ -157,12 +162,15 @@ int main (int argc, char *argv[]) return 1; } } + char **listofwords = NULL; + if (cache && dict) { + listofwords = cachedictionary (dict); + } srand (time (NULL)); play_t *play = initplay (board->xsize, board->ysize); VERBOSE (DEBUG, printf ("play: 0x%p\nboard: 0x%p\n", play, board)); - draw_t *draw = initdraw (bag); draw_t **game = (draw_t **) calloc (nbplayers, sizeof (draw_t *)); CHECKALLOC (game); @@ -292,7 +300,11 @@ int main (int argc, char *argv[]) if (putletter (play, letter, x, y)) { putletter (turn[n], letter, x, y); findwords (words, play, turn[n]); - checkspelling (words, dict); + if (cache) { + checkspellingfromcache (words, listofwords); + } else { + checkspellingfromfile (words, dict); + } letter = getnewletter (game[n]); drawwindow (game[n], score[n], xdrawwin[n], ydrawwin[n], 1); } @@ -308,7 +320,11 @@ int main (int argc, char *argv[]) removeletter (play, x, y); removeletter (turn[n], x, y); findwords (words, play, turn[n]); - checkspelling (words, dict); + if (cache) { + checkspellingfromcache (words, listofwords); + } else { + checkspellingfromfile (words, dict); + } letter = getnextletter (game[n]); drawwindow (game[n], score[n], xdrawwin[n], ydrawwin[n], 1); } @@ -345,6 +361,7 @@ int main (int argc, char *argv[]) free (score); freedraw (draw); freeword (words); + freecache (listofwords); return 0; } diff --git a/todo.txt b/todo.txt index 5d74d90..16b6432 100644 --- a/todo.txt +++ b/todo.txt @@ -5,4 +5,4 @@ = comptage des points = déplacement des lettre dans le présentoir = score -* vérification orthograpphe += vérification orthograpphe -- 2.30.2