initial scoring function
authorLaurent Mazet <mazet@softndesign.org>
Mon, 7 Sep 2020 08:31:53 +0000 (10:31 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 7 Sep 2020 08:31:53 +0000 (10:31 +0200)
public/index.html
public/trivabble.js

index d97ccec761cb1b9595bac7a677f4c4e712be5220..28c32163eaf3c82ece97acc9271ca83bde117409 100644 (file)
@@ -83,6 +83,7 @@
             <button id="clear-rack" class="minibutton" data-l10n="text-content">Put back all the tiles of&#10;your rack in the bag</button>
             <button id="show-rack" class="minibutton" data-l10n="text-content">Show my rack to other players</button>
             <button id="check-spelling" class="minibutton" data-l10n="text-content" style="display:none">Check spelling for new words</button>
+            <button id="score-words" class="minibutton" data-l10n="text-content">Score new words</button>
             <p id="info-spell-checking" style="display:none;"><span data-l0n="text-content">Spell checking is based on:</span> <a rel="noopener noreferrer" target="_blank" href="http://aspell.net">Aspell</a></p>
         </div>
     </body>
index 24bf94761cf895f0de7e421825dce7cad3c326b7..6bc774bf2efe0790b604dd1dd439244d91149554 100644 (file)
         }
     }
 
+    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;
         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;