From 3b3f5e4f4c7e43bfb3c115903d82df2656e4efbc Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Mon, 25 May 2020 23:38:23 +0200 Subject: [PATCH] check spelling function proof of concept --- public/index.html | 2 +- public/trivabble.js | 120 +++++++++++++++++++++++++++++--------------- 2 files changed, 81 insertions(+), 41 deletions(-) diff --git a/public/index.html b/public/index.html index 9211783..8dc3943 100644 --- a/public/index.html +++ b/public/index.html @@ -69,7 +69,7 @@

Who's turn? Click on the Turn button!

- + diff --git a/public/trivabble.js b/public/trivabble.js index 4f93c89..ce45109 100644 --- a/public/trivabble.js +++ b/public/trivabble.js @@ -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; @@ -212,13 +213,13 @@ /* 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); @@ -693,8 +694,20 @@ ? 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; } } @@ -717,7 +730,7 @@ tablePlayers[currentPlayer].classList.remove("current-player"); } - currentPlay = {}; + currentTilePlayed = {}; currentPlayer = player; refreshCurrentPlayer(); } @@ -877,8 +890,8 @@ 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": @@ -1452,24 +1465,28 @@ } 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)) !== "") { @@ -1480,31 +1497,54 @@ 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() { @@ -1676,7 +1716,7 @@ 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; } -- 2.30.2