From 4f69ec084decba5aca883f50a13812279f6ffd20 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 7 Jun 2020 22:31:17 +0200 Subject: [PATCH] initial spell checker --- dict-dists-extractor/Makefile | 28 +++++++++++++ public/index.html | 1 + public/trivabble.js | 75 +++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 dict-dists-extractor/Makefile diff --git a/dict-dists-extractor/Makefile b/dict-dists-extractor/Makefile new file mode 100644 index 0000000..c51ca26 --- /dev/null +++ b/dict-dists-extractor/Makefile @@ -0,0 +1,28 @@ +# -*- Makefile -*- + +ROOT_DICT = ../public/dict + +all: + +$(ROOT_DICT): + mkdir $(ROOT_DICT) + +$(ROOT_DICT)/fr.json: /usr/share/dict/french + cat $< | \ + grep -v '^.$$' | \ + grep -v "[.'-]" | \ + sed -e 's/[àâ]/a/g' \ + -e 's/[ç]/c/g' \ + -e 's/[éèêë]/e/g' \ + -e 's/[ïî]/i/g' \ + -e 's/[ô]/o/g' \ + -e 's/[ùûü]/u/g' | \ + tr '[a-z]' '[A-Z]' | \ + sort | \ + uniq | \ + awk 'BEGIN { print "[" } \ + { print " \"" $$0 "\"," } \ + END { print " \"\"\n]" }' > $@ + +check-%:$(ROOT_DICT)/%.json + grep -v '^ "[A-Z,]*",$$' $< | grep -v '\[' | grep -v '\]' | grep -v '""' || echo "$< is correct" diff --git a/public/index.html b/public/index.html index d598935..9211783 100644 --- a/public/index.html +++ b/public/index.html @@ -69,6 +69,7 @@

Who's turn? Click on the Turn button!

+ diff --git a/public/trivabble.js b/public/trivabble.js index 42884f6..4f93c89 100644 --- a/public/trivabble.js +++ b/public/trivabble.js @@ -79,6 +79,7 @@ let remainingLetters = 0; let needsRestart = false; const gameInProgress = false; + let currentPlay = {}; let eventSource = null; let webSocket = null; @@ -208,6 +209,18 @@ movingTile.style.width = ""; movingTile.style.height = ""; + /* Keep track of letter */ + if (moveCMD.from === "rack") { + if (moveCMD.to === "board") { + currentPlay[moveCMD.indexTo] = "-"; + } + } else { /* moveCMD.from === "board" */ + if (moveCMD.to === "board") { + currentPlay[moveCMD.indexTo] = currentPlay[moveCMD.indexFrom]; + } + delete currentPlay[moveCMD.indexFrom]; + } + mouseUp(document, dragTileEnd, true); mouseMove(document, dragTileMove, true); @@ -680,6 +693,7 @@ ? 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(); return; } @@ -703,6 +717,7 @@ tablePlayers[currentPlayer].classList.remove("current-player"); } + currentPlay = {}; currentPlayer = player; refreshCurrentPlayer(); } @@ -862,6 +877,9 @@ 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; + } break; case "setRackCell": setRackCell(data.indexTo, data.letter); @@ -1433,6 +1451,62 @@ } + function getLetterFromBoard(row, col) { + if ((row < 0) || (row >= 15) || (col < 0) || (col >= 15)) return ""; + let letter = ""; + 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; + } + + function searchWordInLine(row, col, incRow, incCol) { + + while (getLetterFromBoard(row - incRow, col - incCol) !== "") { + row -= incRow; + col -= incCol + } + + let word = ""; + let letter; + while ((letter = getLetterFromBoard(row, col)) !== "") { + word += letter; + row += incRow; + col += incCol; + } + 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); + } + newWord = searchWordInLine(row, col, 0, 1); + if ((newWord !== "") && (!words.includes(newWord))) { + words.push(newWord); + } + } + + myConfirm( + _("Words found\n" + str + "\n" + words), + function () {} + ); + } + function initChat() { chatMessages.style.width = chatMessages.offsetWidth + "px"; @@ -1602,6 +1676,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; helpClear.onclick = clearGame; } -- 2.30.2