let scoreOf;
let bag;
let boardLangSelect;
+ let dictionnary;
const playerLetters = [];
let currentPlayer = "";
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;
}