check spelling function proof of concept
authorLaurent Mazet <mazet@softndesign.org>
Mon, 25 May 2020 21:38:23 +0000 (23:38 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 8 Jun 2020 05:34:23 +0000 (07:34 +0200)
public/index.html
public/trivabble.js

index 92117833fcd8274c3d3ca357a291f1d07e4619d1..8dc3943c82c3d230a1a9500fb742d5b70094ac58 100644 (file)
@@ -69,7 +69,7 @@
             <p id="help-turn" data-l10n="text-content">Who's turn? Click on the Turn button!</p>
             <button id="clear-rack" class="minibutton" data-l10n="text-content">Put back all the tiles of&#10;your rack in the bag</button>
             <button id="show-rack" class="minibutton" data-l10n="text-content">Show my rack to other players</button>
-            <button id="compute-word" class="minibutton" data-l10n="text-content">Compute words on board</button>
+            <button id="check-spelling" class="minibutton" data-l10n="text-content">Check spelling for new words</button>
         </div>
     </body>
 </html>
index 4f93c89f3126d93fa80b16081d94c06f7dacd02a..ce45109dc494080a8dd56facc3158d7dffc78540 100644 (file)
@@ -59,6 +59,7 @@
     let scoreOf;
     let bag;
     let boardLangSelect;
+    let dictionnary;
 
     const playerLetters = [];
     let currentPlayer = "";
@@ -79,7 +80,7 @@
     let remainingLetters = 0;
     let needsRestart = false;
     const gameInProgress = false;
-    let currentPlay = {};
+    let currentTilePlayed = {};
 
     let eventSource = null;
     let webSocket = null;
         /* Keep track of letter */
         if (moveCMD.from === "rack") {
             if (moveCMD.to === "board") {
-               currentPlay[moveCMD.indexTo] = "-";
+                currentTilePlayed[moveCMD.indexTo] = "-";
             }
         } else { /* moveCMD.from === "board" */
             if (moveCMD.to === "board") {
-               currentPlay[moveCMD.indexTo] = currentPlay[moveCMD.indexFrom];
+                currentTilePlayed[moveCMD.indexTo] = currentTilePlayed[moveCMD.indexFrom];
             }
-            delete currentPlay[moveCMD.indexFrom];
+            delete currentTilePlayed[moveCMD.indexFrom];
         }
 
         mouseUp(document,   dragTileEnd,  true);
                             ? format(_("You changed the language of the board to {0}"), newLang)
                             : format(_("{0} changed the language of the board to {1}"), msg.sender, newLang)
                     );
-                    dictionnary = JSON.parse();
-
+                    const code = document.getElementById("board-lang").value;
+                    const file = new XMLHttpRequest();
+                    file.overrideMimeType("application/json");
+                    file.open("GET", "dict/" + code + ".json", true);
+                    file.onreadystatechange = function () {
+                        if ((file.readyState === 4) && (file.status == "200")) {
+                            try {
+                                dictionnary = JSON.parse(file.responseText);
+                            } catch (e) {
+                                console.error(e);
+                            }
+                        }
+                    };
+                    file.send(null);
                     return;
                 }
             }
             tablePlayers[currentPlayer].classList.remove("current-player");
         }
 
-        currentPlay = {};
+        currentTilePlayed = {};
         currentPlayer = player;
         refreshCurrentPlayer();
     }
                 break;
             case "setCell":
                 setCell(data.indexTo, data.letter, Object.prototype.hasOwnProperty.call(data, "player") && data.player !== localStorage.trivabblePlayerName);
-                if ((data.letter !== "") && (currentPlay[data.indexTo] === "-")) {
-                    currentPlay[data.indexTo] = data.letter;
+                if ((data.letter !== "") && (currentTilePlayed[data.indexTo] === "-")) {
+                    currentTilePlayed[data.indexTo] = data.letter;
                 }
                 break;
             case "setRackCell":
     }
 
     function getLetterFromBoard(row, col) {
-        if ((row < 0) || (row >= 15) || (col < 0) || (col >= 15)) return "";
-        let letter = "";
+        if ((row < 0) || (row >= 15) || (col < 0) || (col >= 15)) {
+            return "";
+        }
         try {
-            let index = row * 15 + col;
-            let tilePlaceholder = boardCells[index].getElementsByClassName("tile-placeholder")[0];
-            let tile = tilePlaceholder.getElementsByClassName("tile")[0];
-            letter = tile.firstChild.textContent;
-        } catch (e) {}
-        return letter;
+            const index = (row * 15) + col;
+            const tilePlaceholder = boardCells[index].getElementsByClassName("tile-placeholder")[0];
+            const tile = tilePlaceholder.getElementsByClassName("tile")[0];
+            return tile.firstChild.textContent;
+        } catch (e) {
+            return "";
+        }
     }
 
     function searchWordInLine(row, col, incRow, incCol) {
         
+        /* Look for first letter in line */
         while (getLetterFromBoard(row - incRow, col - incCol) !== "") {
             row -= incRow;
-            col -= incCol
+            col -= incCol;
         }
 
+        /* Look for last letter in line */
         let word = "";
         let letter;
         while ((letter = getLetterFromBoard(row, col)) !== "") {
         return (word.length > 1) ? word : "";
     }
     
-    function computeWord() {
-        let str = "letter:";
-        for (let i in currentPlay) {
-            str += " " + currentPlay[i] + ": " + i;
-        }
-
-        let words = [];
-        for (let i in currentPlay) {
-            let row = Math.floor(i / 15);
-            let col = i % 15;
-            let newWord;
-            newWord = searchWordInLine(row, col, 1, 0);
-            if ((newWord !== "") &&  (!words.includes(newWord))) {
-                words.push(newWord);
+    function searchNewWords() {
+
+        const words = [];
+        for (const i of Object.keys(currentTilePlayed)) {
+
+            /* Get board position */
+            const row = Math.floor(i / 15);
+            const col = i % 15;
+
+            /* Look for word in column */
+            const newWordInCol = searchWordInLine(row, col, 1, 0);
+            if ((newWordInCol !== "") &&  (!words.includes(newWordInCol))) {
+                words.push(newWordInCol);
             }
-            newWord = searchWordInLine(row, col, 0, 1);
-            if ((newWord !== "") &&  (!words.includes(newWord))) {
-                words.push(newWord);
+
+            /* Look for word in row */
+            const newWordInRow = searchWordInLine(row, col, 0, 1);
+            if ((newWordInRow !== "") &&  (!words.includes(newWordInRow))) {
+                words.push(newWordInRow);
+            }
+
+            /* Should we look for word in diag? */
+
+            /* If nothing found, it's a one letter word */
+            if ((newWordInCol === "") && (newWordInRow === "")) {
+                words.push(getLetterFromBoard(row, col));
             }
         }
 
-        myConfirm(
-            _("Words found\n" + str + "\n" + words),
-            function () {}
-        );
+        return words;
+    }
+
+    function checkSpelling() {
+
+        let words = searchNewWords();
+
+        /* Check if words are in dictionnary */
+        for (const k of Object.keys(words)) {
+            if (dictionnary.includes(words[k])) {
+                delete words[k];
+            }
+        }
+        words = words.join(" ");
+
+        /* Advice on erroneous spelling */
+        if (words.length > 0) {
+            myAlert(format(_("Incorrect speeling for {0}"), words));
+        }
     }
 
     function initChat() {
         document.getElementById("join-game").onclick   = joinGame;
         document.getElementById("clear-rack").onclick  = clearRack;
         document.getElementById("show-rack").onclick   = showRack;
-        document.getElementById("compute-word").onclick = computeWord;
+        document.getElementById("check-spelling").onclick = checkSpelling;
         helpClear.onclick  = clearGame;
     }