diff options
author | Julien Dessaux | 2022-10-14 00:17:55 +0200 |
---|---|---|
committer | Julien Dessaux | 2022-10-14 00:17:55 +0200 |
commit | a1a4a06decb67135480616b2ac47138106baf9d6 (patch) | |
tree | cbf605faf6adc56b90d84a807a8f07551f4993aa | |
parent | Added joker handling (diff) | |
download | jeux-de-mots-a1a4a06decb67135480616b2ac47138106baf9d6.tar.gz jeux-de-mots-a1a4a06decb67135480616b2ac47138106baf9d6.tar.bz2 jeux-de-mots-a1a4a06decb67135480616b2ac47138106baf9d6.zip |
Change placed to a sorted array
-rw-r--r-- | index.js | 11 |
1 files changed, 9 insertions, 2 deletions
@@ -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(""); - } + }, }; }(); |