correct highlighting when one puts a tile and create same word horizontally and verti...
authorLaurent Mazet <mazet@softndesign.org>
Sat, 4 Jul 2020 12:53:35 +0000 (14:53 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 4 Jul 2020 12:53:35 +0000 (14:53 +0200)
public/trivabble.js

index 4bc9557f375d763e218a83d14f00fd7217d42b52..92c59c089093b275cba578d601f19b0e3fcd2f20 100644 (file)
         return (word.letters === initLetter) ? null : word;
     }
     
+    function isNewWord(newWord, words) {
+        if (newWord === null) {
+            return false;
+        }
+        for (const k of Object.keys(words)) {
+            if ((words[k].letters === newWord.letters) &&
+                (words[k].row === newWord.row) &&
+                (words[k].col === newWord.col) &&
+                (words[k].incRow === newWord.incRow) &&
+                (words[k].incCol === newWord.incCol)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     function searchNewWords() {
 
         const words = [];
 
             /* Look for word in column */
             const newWordInCol = searchWordInLine(row, col, 1, 0);
-            if ((newWordInCol !== null) &&  (!words.find(element => element.letters === newWordInCol.letters))) {
+            if (isNewWord(newWordInCol, words)) {
                 words.push(newWordInCol);
             }
 
             /* Look for word in row */
             const newWordInRow = searchWordInLine(row, col, 0, 1);
-            if ((newWordInRow !== null) &&  (!words.find(element => element.letters === newWordInRow.letters))) {
+            if (isNewWord(newWordInRow, words)) {
                 words.push(newWordInRow);
             }
 
     function checkSpelling() {
 
         let newWords = searchNewWords();
+        if (newWords.length === 0) {
+            myAlert(_("No new word found"));
+            return;
+        }
 
         /* Check if words are in dictionary */
         let words = [];
         let badWords = [];
         for (const k of Object.keys(newWords)) {
-            words.push(newWords[k].letters.replace(/ /g, "?"));
+            let currentWord = newWords[k].letters.replace(/ /g, "?");
+            if (words.indexOf(currentWord) === -1) {
+                words.push(currentWord);
+            }
             modifyTileClass(newWords[k], "tile-spelling-ok", true);
-            setTimeout(function () {modifyTileClass(newWords[k], "tile-spelling-ok", false);}, 5000);
             const pattern = "^" + newWords[k].letters.replace(/ /g, ".") + "$";
             if (!dictionary.find(element => element.match(pattern))) {
-                badWords.push(newWords[k].letters.replace(/ /g, "?"));
+                if (badWords.indexOf(currentWord) === -1) {
+                    badWords.push(currentWord);
+                }
                 modifyTileClass(newWords[k], "tile-spelling-nok", true);
-                setTimeout(function () {modifyTileClass(newWords[k], "tile-spelling-nok", false);}, 5000);
             }
         }
+        setTimeout(function () {
+                modifyTileClass(null, "tile-spelling-ok", false);
+                modifyTileClass(null, "tile-spelling-nok", false);
+            }, 5000);
         words = words.join(", ").trim();
         badWords = badWords.join(", ").trim();
 
         if (badWords.length > 0) {
             const msg = chatMessage("", format(_("Incorrect spelling for {0} from {1}"), badWords, words));
             msg.classList.add("error");
-        } else if (words.length > 0) {
-            chatMessage("", format(_("All words are correct: {0}"), words));
         } else {
-            myAlert(_("No new word found"));
+            chatMessage("", format(_("All words are correct: {0}"), words));
         }
     }