From a1a4a06decb67135480616b2ac47138106baf9d6 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 14 Oct 2022 00:17:55 +0200 Subject: Change placed to a sorted array --- index.js | 11 +++++++++-- 1 file 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(""); - } + }, }; }(); -- cgit v1.2.3