}
/* Look for last letter in line */
- let word = "";
+ let word = {letters: "", row: row, col: col, incRow: incRow, incCol: incCol};
let letter;
while ((letter = getLetterFromBoard(row, col)) !== "") {
- word += letter;
+ word.letters += letter;
row += incRow;
col += incCol;
}
- return (word === initLetter) ? "" : word;
+ return (word.letters === initLetter) ? null : word;
}
function searchNewWords() {
/* Look for word in column */
const newWordInCol = searchWordInLine(row, col, 1, 0);
- if ((newWordInCol !== "") && (!words.includes(newWordInCol))) {
+ if ((newWordInCol !== null) && (!words.find(element => element.letters === newWordInCol.letters))) {
words.push(newWordInCol);
}
/* Look for word in row */
const newWordInRow = searchWordInLine(row, col, 0, 1);
- if ((newWordInRow !== "") && (!words.includes(newWordInRow))) {
+ if ((newWordInRow !== null) && (!words.find(element => element.letters === newWordInCol.letters))) {
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));
+ if ((newWordInCol === null) && (newWordInRow === null)) {
+ words.push({letters: getLetterFromBoard(row, col), row: row, col: col, incRow: 0, incCol: 0});
}
}
return words;
}
+ function highlightWord(word, value, onOff) {
+ 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];
+ if (onOff) {
+ tile.classList.add(value);
+ } else {
+ tile.classList.remove(value);
+ }
+ }
+ }
+
function checkSpelling() {
- let words = searchNewWords();
+ let newWords = searchNewWords();
/* Check if words are in dictionary */
+ let words = [];
let badWords = [];
- for (const k of Object.keys(words)) {
- const pattern = "^" + words[k].replace(/ /g, ".") + "$";
- words[k] = words[k].replace(/ /g, "?");
+ for (const k of Object.keys(newWords)) {
+ words.push(newWords[k].letters.replace(/ /g, "?"));
+ highlightWord(newWords[k], "tile-spelling-ok", true);
+ setTimeout(function () {highlightWord(newWords[k], "tile-spelling-ok", false);}, 5000);
+ const pattern = "^" + newWords[k].letters.replace(/ /g, ".") + "$";
if (!dictionary.find(element => element.match(pattern))) {
- badWords.push(words[k]);
+ badWords.push(newWords[k]);
+ highlightWord(newWords[k], "tile-spelling-nok", true);
+ setTimeout(function () {highlightWord(newWords[k], "tile-spelling-nok", false);}, 5000);
}
}
words = words.join(", ")