diff options
author | Julien Dessaux | 2022-10-16 22:35:37 +0200 |
---|---|---|
committer | Julien Dessaux | 2022-10-16 22:35:37 +0200 |
commit | 928145520c9bd9c205bf98085fb57f09567bfccc (patch) | |
tree | 2723ba2227879e102761f6511fcec44ea67aeb3e | |
parent | Check if placement is valid (diff) | |
download | jeux-de-mots-928145520c9bd9c205bf98085fb57f09567bfccc.tar.gz jeux-de-mots-928145520c9bd9c205bf98085fb57f09567bfccc.tar.bz2 jeux-de-mots-928145520c9bd9c205bf98085fb57f09567bfccc.zip |
Compute words and points from placed letters
Diffstat (limited to '')
-rw-r--r-- | index.html | 6 | ||||
-rw-r--r-- | index.js | 126 |
2 files changed, 126 insertions, 6 deletions
@@ -31,13 +31,13 @@ <tr><td id="l0"><td id="l1"><td id="l2"><td id="l3"><td id="l4"><td id="l5"><td id="l6"></tr> </table> <aside> - <button title="validate your move" onclick="onvalidatemoveclick()" id="validate" disabled>Validate</button> + <button title="validate your move" onclick="onvalidatemoveclick()" id="validate" disabled>0 - Validate</button> <button onclick="onSelectSwapTilesClicked()" id="pass">Pass</button> </aside> </p> <p> - <span id="player_1_name">player one name</span>: <span class="player_name" id="player_1_score">0</span><br> - <span id="player_2_name">player two name</span>: <span class="player_name" id="player_2_score">0</span> + <span id="player_1_name">player one name</span>: <span class="player_name" id="player_1_points">0</span><br> + <span id="player_2_name">player two name</span>: <span class="player_name" id="player_2_points">0</span> </p> <p id="remaining_letters"></p> </main> @@ -128,7 +128,7 @@ let CW = function(){ // Remove the letter from the placed array placed.splice(n, 1); // update the validate button - document.getElementById("validate").disabled = !isValidPlay(); + updateUI(); } function moveFromRackToBoard(src, x, y) { @@ -158,8 +158,8 @@ let CW = function(){ } // advance the cursor moveCursorForwardIfPossible(); - // update the validate button - document.getElementById("validate").disabled = !isValidPlay(); + // update the validate button and pending points + updateUI(); } function isValidPlay() { @@ -235,6 +235,126 @@ let CW = function(){ return connected && i === placed.length; } + function getNewWordsAndScores() { + function getLetterScore(letter, elt, modifiers) { + let multiplier = 1; + if (elt.classList.contains("placed")) { + if (elt.classList.contains("tl")) { + multiplier = 3; + } else if (elt.classList.contains("dl")) { + multiplier = 2; + } + if (elt.classList.contains("tw")) { + modifiers.push("tw"); + } else if (elt.classList.contains("dw")) { + modifiers.push("dw"); + } + } + if (typeof letter === "object") { + return 0; + } + return multiplier * letters[letter].points; + } + function getHorizontalWordFrom(x, y) { + let word = ""; + let points = 0; + let modifiers = []; + // Go to the left + for(let i=x;i>=0 && CWDATA.board[y][i] != "";i--) { + let letter = CWDATA.board[y][i]; + let elt = document.getElementById(["s", y, "_", i].join("")); + let p = getLetterScore(letter, elt, modifiers); + if (typeof letter === "object") { + word = letter.joker + word; + } else { + word = letter + word; + points += p; + } + } + // Go to the right + for(let i=x+1;i<15 && CWDATA.board[y][i] != "";i++) { + let letter = CWDATA.board[y][i]; + let elt = document.getElementById(["s", y, "_", i].join("")); + let p = getLetterScore(letter, elt, modifiers); + if (typeof letter === "object") { + word = word + letter.joker; + } else { + word = word + letter; + points += p; + } + } + // score modifiers + modifiers.forEach(function(m) { if (m === "dw") { points *= 2; } else { points *= 3; } }); + return { word: word, points: points }; + } + function getVerticalWordFrom(x, y) { + let word = ""; + let points = 0; + let modifiers = []; + // Go to the top + for(let i=y;i>=0 && CWDATA.board[i][x] != "";i--) { + let letter = CWDATA.board[i][x]; + let elt = document.getElementById(["s", i, "_", x].join("")); + let p = getLetterScore(letter, elt, modifiers); + if (typeof letter === "object") { + word = letter.joker + word; + } else { + word = letter + word; + points += p; + } + } + // Go to the bottom + for(let i=y+1;i<15 && CWDATA.board[i][x] != "";i++) { + let letter = CWDATA.board[i][x]; + let elt = document.getElementById(["s", i, "_", x].join("")); + let p = getLetterScore(letter, elt, modifiers); + if (typeof letter === "object") { + word = word + letter.joker; + } else { + word = word + letter; + points += p; + } + } + // score modifiers + modifiers.forEach(function(m) { if (m === "dw") { points *= 2; } else { points *= 3; } }); + return { word: word, points: points }; + } + let words = []; + words.push(getHorizontalWordFrom(placed[0].x, placed[0].y)); + words.push(getVerticalWordFrom(placed[0].x, placed[0].y)); + for (let i=1; i<placed.length;i++) { + if (placed[i].x === placed[0].x) { // going down + words.push(getHorizontalWordFrom(placed[i].x, placed[i].y)); + } else { // going right + words.push(getVerticalWordFrom(placed[i].x, placed[i].y)); + } + } + // we strip one letter words + for(let i=words.length-1;i>=0;i--) { + if (words[i].word.length < 2) { + words.splice(i, 1); + } + } + return words; + } + + function updateUI() { + let validate = document.getElementById("validate"); + if (isValidPlay()) { + let words = getNewWordsAndScores(); + console.log(words); + let total = 0; + words.forEach(function(word) { + total += word.points; + }); + validate.innerHTML = [total, " - Validate"].join(""); + validate.disabled = false; + } else { + validate.innerHTML = "0 - Validate"; + validate.disabled = true; + } + } + return { init: function() { // populate the board |