optimize with sort
This commit is contained in:
parent
ad414d0da8
commit
1365ef65dd
@ -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;
|
||||
}
|
||||
|
51
sudoku.js
51
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user