summaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
authorJulien Dessaux2022-10-14 00:17:55 +0200
committerJulien Dessaux2022-10-14 00:17:55 +0200
commita1a4a06decb67135480616b2ac47138106baf9d6 (patch)
treecbf605faf6adc56b90d84a807a8f07551f4993aa /index.js
parentAdded joker handling (diff)
downloadjeux-de-mots-a1a4a06decb67135480616b2ac47138106baf9d6.tar.gz
jeux-de-mots-a1a4a06decb67135480616b2ac47138106baf9d6.tar.bz2
jeux-de-mots-a1a4a06decb67135480616b2ac47138106baf9d6.zip
Change placed to a sorted array
Diffstat (limited to '')
-rw-r--r--index.js11
1 files changed, 9 insertions, 2 deletions
diff --git a/index.js b/index.js
index 8e1dacd..605f614 100644
--- a/index.js
+++ b/index.js
@@ -20,7 +20,7 @@ let CW = function(){
K:{count:1, points:10}, W:{count:1, points:10}, X:{count:1, points:10}, Y:{count:1, points:10}, Z:{count:1, points:10},
};
let cursor = undefined;
- let placed = [];
+ let placed = []; // a sorted array of letters to place
function makeBoardTileOnCLick(x, y) {
return function() {
@@ -147,7 +147,14 @@ let CW = function(){
dst.classList.add("placed");
src.classList.remove("letter");
src.innerHTML = "";
+ // insert in the placed sorted array
placed.push({x: cursor.x, y: cursor.y, letter: dst.innerHTML});
+ for (let i=placed.length-1;i>0 && placed[i].y <= placed[i-1].y && placed[i].x <= placed[i-1].x;i--) {
+ let tmp = placed[i];
+ placed[i] = placed[i-1];
+ placed[i-1] = tmp;
+ }
+ // advance the cursor
moveCursorForwardIfPossible();
}
@@ -207,7 +214,7 @@ let CW = function(){
}
}
document.getElementById("remaining_letters").innerHTML = remaining_letters.join("");
- }
+ },
};
}();