}
+ function getLetterFromBoard(row, col) {
+ if ((row < 0) || (row >= 15) || (col < 0) || (col >= 15)) {
+ return "";
+ }
+ try {
+ 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) {
+
+ const initLetter = getLetterFromBoard(row, col);
+
+ /* Look for first letter in line */
+ while (getLetterFromBoard(row - incRow, col - incCol) !== "") {
+ row -= incRow;
+ col -= incCol;
+ }
+
+ /* Look for last letter in line */
+ const word = {letters: "", row: row, col: col, incRow: incRow, incCol: incCol};
+ let letter;
+ while ((letter = getLetterFromBoard(row, col)) !== "") {
+ word.letters += letter;
+ row += incRow;
+ col += incCol;
+ }
+ 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 = [];
+ 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 (isNewWord(newWordInCol, words)) {
+ words.push(newWordInCol);
+ }
+
+ /* Look for word in row */
+ const newWordInRow = searchWordInLine(row, col, 0, 1);
+ if (isNewWord(newWordInRow, words)) {
+ words.push(newWordInRow);
+ }
+
+ /* Should we look for word in diag? */
+
+ /* If nothing found, it's a one letter word */
+ if ((newWordInCol === null) && (newWordInRow === null)) {
+ words.push({letters: getLetterFromBoard(row, col), row: row, col: col, incRow: 0, incCol: 0});
+ }
+ }
+
+ return words;
+ }
+
+ function modifyTileClass(word, value, action) {
+
+ /* action: true for add, false for remove */
+
+ if (action) {
+
+ /* only for new words */
+ for (let l = 0; l < word.letters.length; l++) {
+ const index = ((word.row + (l * word.incRow)) * 15) + word.col + (l * word.incCol);
+ const tilePlaceholder = boardCells[index].getElementsByClassName("tile-placeholder")[0];
+ const tile = tilePlaceholder.getElementsByClassName("tile")[0];
+
+ tile.classList.add(value);
+ }
+
+ } else {
+
+ /* tiles on board */
+ for (let index = 0; index < 15 * 15; index++) {
+ const tilePlaceholder = boardCells[index].getElementsByClassName("tile-placeholder")[0];
+ const tile = tilePlaceholder.getElementsByClassName("tile")[0];
+ if (typeof tile !== "undefined") {
+ tile.classList.remove(value);
+ }
+ }
+
+ /* tiles on rack */
+ for (const tilePlaceholder of rack.getElementsByClassName("tile-placeholder")) {
+ const tile = tilePlaceholder.getElementsByClassName("tile")[0];
+ if (typeof tile !== "undefined") {
+ tile.classList.remove(value);
+ }
+ }
+
+ /* tile on the move */
+ if (movingTile !== null) {
+ movingTile.classList.remove(value);
+ }
+ }
+ }
+
+ function checkSpelling() {
+
+ const 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)) {
+ const currentWord = newWords[k].letters.replace(/ /gu, "?");
+ if (words.indexOf(currentWord) === -1) {
+ words.push(currentWord);
+ }
+ modifyTileClass(newWords[k], "tile-spelling-ok", true);
+ const pattern = "^" + newWords[k].letters.replace(/ /gu, ".") + "$";
+ if (!dictionary.find(function (word) {return word.match(pattern);})) {
+ if (badWords.indexOf(currentWord) === -1) {
+ badWords.push(currentWord);
+ }
+ modifyTileClass(newWords[k], "tile-spelling-nok", true);
+ }
+ }
+ setTimeout(
+ function () {
+ modifyTileClass(null, "tile-spelling-ok", false);
+ modifyTileClass(null, "tile-spelling-nok", false);
+ }, 5000);
+ words = words.join(", ").trim();
+ badWords = badWords.join(", ").trim();
+
+ /* Advice on erroneous spelling */
+ if (badWords.length > 0) {
+ const msg = chatMessage("", format(_("Incorrect spelling for {0} from {1}"), badWords, words));
+ msg.classList.add("error");
+ } else {
+ chatMessage("", format(_("All words are correct: {0}"), words));
+ }
+ }
+
function initChat() {
- chatMessages.style.width = chatMessages.offsetWidth + "px";
-
const btn = document.getElementById("chat-btn");
chatTextarea.onmouseup = function () {
document.getElementById("join-game").onclick = joinGame;
document.getElementById("clear-rack").onclick = clearRack;
document.getElementById("show-rack").onclick = showRack;
+ document.getElementById("enable-spell-checker").onclick = enableSpellChecker;
+ document.getElementById("check-spelling").onclick = checkSpelling;
helpClear.onclick = clearGame;
+
+ document.getElementById("modal-config-btn").onclick = showConfig;
+ document.getElementById("modal-close-btn").onclick = showConfig;
}
function initGame() {