refactor
This commit is contained in:
parent
57d06c3b53
commit
ad414d0da8
65
sudoku.js
65
sudoku.js
@ -36,8 +36,6 @@ window.onload = function() {
|
|||||||
box.onclick = onclick
|
box.onclick = onclick
|
||||||
box.onmouseenter = onmouseenter
|
box.onmouseenter = onmouseenter
|
||||||
box.onmouseleave = onmouseleave
|
box.onmouseleave = onmouseleave
|
||||||
box.previousValue = ""
|
|
||||||
box.previousPlaceholder = ""
|
|
||||||
}
|
}
|
||||||
box.oncontextmenu = oncontextmenu
|
box.oncontextmenu = oncontextmenu
|
||||||
box.rowId = rowId
|
box.rowId = rowId
|
||||||
@ -85,6 +83,8 @@ window.onload = function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.onpopstate = (event) => loadGame(event.state)
|
||||||
|
|
||||||
function loadGame(state) {
|
function loadGame(state) {
|
||||||
if (state) {
|
if (state) {
|
||||||
boxes.forEach((box, i) => {
|
boxes.forEach((box, i) => {
|
||||||
@ -111,8 +111,6 @@ function loadGame(state) {
|
|||||||
highlight()
|
highlight()
|
||||||
}
|
}
|
||||||
|
|
||||||
window.onpopstate = (event) => loadGame(event.state)
|
|
||||||
|
|
||||||
function searchCandidatesOf(box) {
|
function searchCandidatesOf(box) {
|
||||||
box.candidates = new Set(VALUES)
|
box.candidates = new Set(VALUES)
|
||||||
box.neighbourhood.forEach(neighbour => box.candidates.delete(neighbour.value))
|
box.neighbourhood.forEach(neighbour => box.candidates.delete(neighbour.value))
|
||||||
@ -177,16 +175,30 @@ function oninput() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function checkBox(box) {
|
function checkBox(box) {
|
||||||
box.andNeighbourhood.forEach(neighbour => {
|
checkCandidates(box.andNeighbourhood)
|
||||||
neighbour.setCustomValidity("")
|
checkDuplicates(box)
|
||||||
neighbour.classList.remove("is-invalid")
|
checkSuccess()
|
||||||
searchCandidatesOf(neighbour)
|
}
|
||||||
if (neighbour.candidates.size == 0) {
|
|
||||||
neighbour.setCustomValidity("Aucun chiffre possible !")
|
function checkBoxes() {
|
||||||
neighbour.classList.add("is-invalid")
|
checkCandidates(boxes)
|
||||||
|
boxes.forEach(checkDuplicates)
|
||||||
|
checkSuccess()
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkCandidates(area) {
|
||||||
|
area.forEach(box => {
|
||||||
|
box.setCustomValidity("")
|
||||||
|
box.classList.remove("is-invalid")
|
||||||
|
searchCandidatesOf(box)
|
||||||
|
if (box.candidates.size == 0) {
|
||||||
|
box.setCustomValidity("Aucun chiffre possible !")
|
||||||
|
box.classList.add("is-invalid")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkDuplicates(box) {
|
||||||
if (box.value) {
|
if (box.value) {
|
||||||
for (let [area, neighbours] of Object.entries({
|
for (let [area, neighbours] of Object.entries({
|
||||||
région: regions[box.regionId],
|
région: regions[box.regionId],
|
||||||
@ -201,37 +213,6 @@ function checkBox(box) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
checkSuccess()
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkBoxes() {
|
|
||||||
boxes.forEach(box => {
|
|
||||||
box.setCustomValidity("")
|
|
||||||
box.classList.remove("is-invalid")
|
|
||||||
searchCandidatesOf(box)
|
|
||||||
if (box.candidates.size == 0) {
|
|
||||||
box.setCustomValidity("Aucun chiffre possible !")
|
|
||||||
box.classList.add("is-invalid")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
for (let [areaName, areas] of Object.entries({
|
|
||||||
région: regions,
|
|
||||||
ligne: rows,
|
|
||||||
colonne: columns,
|
|
||||||
}))
|
|
||||||
for (area of areas)
|
|
||||||
for (box1 of area)
|
|
||||||
for (box2 of area)
|
|
||||||
if (box1 != box2 && box1.value && box1.value == box2.value) {
|
|
||||||
for (box of [box1, box2]) {
|
|
||||||
box.setCustomValidity(`Il y a un autre ${box.value} dans cette ${areaName}.`)
|
|
||||||
box.classList.add("is-invalid")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
checkSuccess()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkSuccess() {
|
function checkSuccess() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user