From e8d5d396658d074812d5dfd096720c60cd5a9e87 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 13 Oct 2022 23:22:58 +0200 Subject: Added joker handling --- index.html | 4 ++-- index.js | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 6ea4c01..b84f998 100644 --- a/index.html +++ b/index.html @@ -45,7 +45,7 @@ diff --git a/index.js b/index.js index c937904..8e1dacd 100644 --- a/index.js +++ b/index.js @@ -97,6 +97,7 @@ let CW = function(){ } function moveFromBoardToRack(x, y, src) { + CWDATA.board[y][x] = ""; // Find the letter in the placed array let n = undefined; for(n=placed.length-1; n>=0; n--) { @@ -114,7 +115,11 @@ let CW = function(){ i++; } while (i<7); // Move the board letter to the rack - dst.innerHTML = src.innerHTML; + if (src.innerHTML.charAt(21) === "&") { + dst.innerHTML = " "; + } else { + dst.innerHTML = src.innerHTML; + } dst.classList.add("letter"); src.classList.remove("letter"); src.classList.remove("placed"); @@ -126,12 +131,23 @@ let CW = function(){ function moveFromRackToBoard(src, x, y) { let dst = document.getElementById(["s", y, "_", x].join("")); - dst.innerHTML = src.innerHTML; + if (src.innerHTML === " ") { + let letter = prompt("Entrez la lettre à utiliser", "A"); + if (letter === null || !/^[A-Za-z]$/.test(letter)) { + return; + } + letter = letter.toUpperCase(); + dst.innerHTML = [letter, "
 
"].join(""); + CWDATA.board[y][x] = {joker: letter}; + } else { + dst.innerHTML = src.innerHTML; + CWDATA.board[y][x] = dst.innerHTML.charAt(0); + } dst.classList.add("letter"); dst.classList.add("placed"); src.classList.remove("letter"); src.innerHTML = ""; - placed.push({x: cursor.x, y: cursor.y, letter: src.innerHTML}); + placed.push({x: cursor.x, y: cursor.y, letter: dst.innerHTML}); moveCursorForwardIfPossible(); } @@ -145,7 +161,12 @@ let CW = function(){ let elt = document.getElementById(["s", y, "_", x].join("")); if (letter !== "") { elt.className = "letter"; - elt.innerHTML = [letter, "
", letters[letter].points, "
"].join(""); + if (typeof letter === "object") { + elt.innerHTML = [letter.joker, "
 
"].join(""); + letter = "JOKER"; + } else { + elt.innerHTML = [letter, "
", letters[letter].points, "
"].join(""); + } // we also remove the letter from the pool letters[letter].count--; total_remaining_letters--; @@ -154,15 +175,19 @@ let CW = function(){ } } // populate the rack - for (let x=0; x<8; x++) { + for (let x=0; x<7; x++) { let letter = CWDATA.letters[x]; let elt = document.getElementById(["l", x].join("")); if (letter !== undefined) { elt.className = "letter"; - elt.innerHTML = [letter, "
", letters[letter].points, "
"].join(""); // we remove the letter from the pool letters[letter].count--; total_remaining_letters--; + if (letter === "JOKER") { + elt.innerHTML = " "; + } else { + elt.innerHTML = [letter, "
", letters[letter].points, "
"].join(""); + } } elt.onclick = makeRackTileOnClick(x); } -- cgit v1.2.3