limit dictionary reading
authorMazet Laurent <mazet@softndesign.org>
Wed, 12 Jun 2024 06:22:17 +0000 (08:22 +0200)
committerMazet Laurent <mazet@softndesign.org>
Wed, 12 Jun 2024 06:22:17 +0000 (08:22 +0200)
display.c
function.c
function.h
scrabble.c
type.h

index d4fd871707969a6188447bc0e9f2372fcba0c476..736b86af89f8bb7cd2541ba5a45acb67a7f4a29c 100644 (file)
--- 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 {
index 97fd3fccc39239540e5a9de6c121b88ae4968b3e..e53d93f0c3395dd60449d1fc58d5c32f55ce42c8 100644 (file)
@@ -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: */
index e30a0352e422292de2b6fdce1c87eb4c014ba194..fc29b423e3c352294c28e6488a79846b5977b014 100644 (file)
@@ -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__ */
 
index a14fd3ae9d65c9f106c7bf8d6d10c78d7d4dc28f..5d64b699485b41c21727ae1429b7a89cb606a91d 100644 (file)
@@ -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 d184813b4742143c4f08592500b6eed5b1a6d1b0..6afcaa7991e05556a89c93a333efb4181544ec6f 100644 (file)
--- 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 {