From 0e80955a71f8f8baa6c5e82e0b6321afde2ec781 Mon Sep 17 00:00:00 2001 From: Mazet Laurent Date: Wed, 12 Jun 2024 08:22:17 +0200 Subject: [PATCH] limit dictionary reading --- display.c | 12 +++++++++++- function.c | 56 ++++++++++++++++++++++++++++++++---------------------- function.h | 4 +++- scrabble.c | 3 ++- type.h | 5 +++-- 5 files changed, 52 insertions(+), 28 deletions(-) diff --git a/display.c b/display.c index d4fd871..736b86a 100644 --- a/display.c +++ b/display.c @@ -226,7 +226,17 @@ void spellwindow (word_t *words, int xoffset, int yoffset, int mode) for (i = 0; i < words->maxnbwords; i++) { if (words->status[i] != none) { if (mode) { - set_color ((words->status[i] == correct) ? wgreen : wred); + switch (words->status[i]) { + case correct: + set_color (wgreen); + break; + case incorrect: + set_color (wred); + break; + default: + set_color (white); + break; + } mvaddstr (yoffset + 2 + n, xoffset, words->tab[i]); set_color (white); } else { diff --git a/function.c b/function.c index 97fd3fc..e53d93f 100644 --- a/function.c +++ b/function.c @@ -359,27 +359,7 @@ void freeword (word_t *words) free (words); } -spelling_e _checkdict (char *word, char *dict) -{ - spelling_e ret = incorrect; - FILE *fd = fopen (dict, "r"); - if (fd) { - while (!feof (fd)) { - char str[128] = {0}; - fscanf (fd, "%s", str); - if (strcmp (word, str) == 0) { - ret = correct; - break; - } - } - fclose (fd); - } else { - ret = correct; - } - return ret; -} - -void checkspelling (word_t *words, play_t *play, play_t *turn, char *dict) +void findwords (word_t *words, play_t *play, play_t *turn) { int i, j; @@ -406,7 +386,7 @@ void checkspelling (word_t *words, play_t *play, play_t *turn, char *dict) if (((letter == ' ') || (i == play->xsize - 1)) && (accountable)) { if (nbletters > 1) { words->tab[n][nbletters] = '\0'; - words->status[n] = _checkdict (words->tab[n], dict); + words->status[n] = notchecked; } n++; } @@ -432,7 +412,7 @@ void checkspelling (word_t *words, play_t *play, play_t *turn, char *dict) if (((letter == ' ') || (i == play->xsize - 1)) && (accountable)) { if (nbletters > 1) { words->tab[n][nbletters] = '\0'; - words->status[n] = _checkdict (words->tab[n], dict); + words->status[n] = notchecked; } n++; } @@ -444,4 +424,34 @@ void checkspelling (word_t *words, play_t *play, play_t *turn, char *dict) } } +void checkspelling (word_t *words, char *dict) +{ + int i = 0; + FILE *fd = fopen (dict, "r"); + if (fd) { + char str[128] = {0}; + while (fscanf (fd, "%s", str) > 0) { + int stop = 1; + for (i = 0; i < words->maxnbwords; i++) { + if (words->status[i] == notchecked) { + if (strcmp (words->tab[i], str) == 0) { + words->status[i] = correct; + } else { + stop = 0; + } + } + } + if (stop) { + break; + } + } + fclose (fd); + 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 e30a035..fc29b42 100644 --- a/function.h +++ b/function.h @@ -45,7 +45,9 @@ word_t *initword (int maxnbwords, int maxlength); void freeword (word_t *words); -void checkspelling (word_t *words, play_t *play, play_t *turn, char *dict); +void findwords (word_t *words, play_t *play, play_t *turn); + +void checkspelling (word_t *words, char *dict); #endif /* __FUNCTION_H__ */ diff --git a/scrabble.c b/scrabble.c index a14fd3a..5d64b69 100644 --- a/scrabble.c +++ b/scrabble.c @@ -228,7 +228,8 @@ int main (int argc, char *argv[]) valuewindow (bag, xvaluewin, yvaluewin, 10, highlight); scorewindow (nbpoints, xscorewin, yscorewin); - checkspelling (words, play, turn[n], dict); + findwords (words, play, turn[n]); + checkspelling (words, dict); spellwindow (words, xspellwin, yspellwin, 1); int ch = getch (); spellwindow (words, xspellwin, yspellwin, 0); diff --git a/type.h b/type.h index d184813..6afcaa7 100644 --- a/type.h +++ b/type.h @@ -39,9 +39,10 @@ typedef struct { } draw_t; typedef enum { - incorrect = -1, none = 0, - correct = 1 + correct, + incorrect, + notchecked } spelling_e; typedef struct { -- 2.30.2