From 1365ef65dd6e8c3c20c2b5b8269cbca317415766 Mon Sep 17 00:00:00 2001 From: adrien Date: Sat, 25 Nov 2023 19:55:59 +0100 Subject: [PATCH] optimize with sort --- style.css | 4 ++++ sudoku.js | 51 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/style.css b/style.css index 03d75ef..2bb0047 100644 --- a/style.css +++ b/style.css @@ -36,6 +36,10 @@ table input:not([disabled]) { background: transparent !important; } +table input[disabled] { + background-position: center !important; +} + tr:nth-child(3n+1) td input { border-top-width: 3px !important; } diff --git a/sudoku.js b/sudoku.js index 3aae958..e392768 100755 --- a/sudoku.js +++ b/sudoku.js @@ -176,13 +176,43 @@ function oninput() { function checkBox(box) { checkCandidates(box.andNeighbourhood) - checkDuplicates(box) + + if (box.value) { + for (let [areaName, neighbours] of Object.entries({ + région: regions[box.regionId], + ligne: rows[box.rowId], + colonne: columns[box.columnId], + })) + for (neighbour of neighbours) + if (box != neighbour && box.value == neighbour.value) { + for (neighbour of [box, neighbour]) { + neighbour.setCustomValidity(`Il y a un autre ${box.value} dans cette ${areaName}.`) + neighbour.classList.add("is-invalid") + } + } + } + checkSuccess() } function checkBoxes() { checkCandidates(boxes) - boxes.forEach(checkDuplicates) + + for (let [areaName, areas] of Object.entries({ + région: regions, + ligne: rows, + colonne: columns, + })) + for (area of areas) + area.sort((box, neighbour) => { + if (box.value && box.value == neighbour.value) { + for (neighbour of [box, neighbour]) { + neighbour.setCustomValidity(`Il y a un autre ${box.value} dans cette ${areaName}.`) + neighbour.classList.add("is-invalid") + } + } + }) + checkSuccess() } @@ -198,23 +228,6 @@ function checkCandidates(area) { }) } -function checkDuplicates(box) { - if (box.value) { - for (let [area, neighbours] of Object.entries({ - région: regions[box.regionId], - ligne: rows[box.rowId], - colonne: columns[box.columnId], - })) - for (neighbour of neighbours) - if (box != neighbour && box.value == neighbour.value) { - for (neighbour of [box, neighbour]) { - neighbour.setCustomValidity(`Il y a un autre ${box.value} dans cette ${area}.`) - neighbour.classList.add("is-invalid") - } - } - } -} - function checkSuccess() { if (sudokuForm.checkValidity()) { // Correct grid if (boxes.filter(box => box.value == "").length == 0) {