From 2ddb56a06d4b1be9399b36e9f992b181acd762df Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Mon, 7 Sep 2020 10:31:53 +0200 Subject: [PATCH] initial scoring function --- public/index.html | 1 + public/trivabble.js | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/public/index.html b/public/index.html index d97ccec..28c3216 100644 --- a/public/index.html +++ b/public/index.html @@ -83,6 +83,7 @@ + diff --git a/public/trivabble.js b/public/trivabble.js index 24bf947..6bc774b 100644 --- a/public/trivabble.js +++ b/public/trivabble.js @@ -1719,6 +1719,66 @@ } } + function scoreWords() { + let totalScore = 0; + + const newWords = searchNewWords(); + for (const k of Object.keys(newWords)) { + let wordFactor = 1; + let wordScore = 0; + + const word = newWords[k]; + for (let l = 0; l < word.letters.length; l++) { + const index = ((word.row + (l * word.incRow)) * 15) + word.col + (l * word.incCol); + + /* Letter score */ + const tilePlaceholder = boardCells[index].getElementsByClassName("tile-placeholder")[0]; + const tile = tilePlaceholder.getElementsByClassName("tile")[0]; + const letterScore = tile.lastChild.textContent; + let letterFactor = 1; + + /* Is a freshly played letter? */ + if (currentTilePlayed[index] == word.letters[l]) { + /* Letter factor */ + if (boardCells[index].classList.contains("special-cell-doubleLetter")) { + letterFactor = 2; + } else if (boardCells[index].classList.contains("special-cell-tripleLetter")) { + letterFactor = 3; + } + + /* Word factor */ + if (boardCells[index].classList.contains("special-cell-doubleWord")) { + wordFactor *= 2; + } else if (boardCells[index].classList.contains("special-cell-tripleWord")) { + wordFactor *= 3; + } + + } + + wordScore += letterScore * letterFactor; + } + + totalScore += wordScore * wordFactor; + } + + /* Check for trivabble */ + let number = 0; + for (const i of Object.keys(currentTilePlayed)) { + number++; + } + if (number == 7) { + totalScore += 50; + } + + /* Confirm score */ + myConfirm( + _("Do you want to add score to active player?") + " " + totalScore, + function () { + /* Add score to active player */ + } + ); + } + function triggerLaser(cell) { if (!cell) { return; @@ -2057,6 +2117,7 @@ document.getElementById("clear-rack").onclick = clearRack; document.getElementById("show-rack").onclick = showRack; document.getElementById("check-spelling").onclick = checkSpellingClicked; + document.getElementById("score-words").onclick = scoreWords; helpClear.onclick = clearGame; document.getElementById("btn-settings").onclick = showSettings; -- 2.30.2