diff --git a/Grille.php b/Grille.php index ea8efac..1dd0558 100644 --- a/Grille.php +++ b/Grille.php @@ -135,7 +135,6 @@ class Grille implements ArrayAccess if ($x == 0) { $lettres_ligne = $this->dico[$this->largeur]; } - // en colonne $lettres_colonne = $this->dico[$this->hauteur]; for ($y2 = 0; $y2 < $y; $y2++) { @@ -145,9 +144,9 @@ class Grille implements ArrayAccess $lettres_ligne->branches, $lettres_colonne->branches ); - // Sélection des candidats les plus fertiles + // Mélange et sélection des candidats les plus fertiles foreach ($lettres_communes as $lettre => $_) { - $lettres_communes[$lettre] = log(count($lettres_ligne->branches[$lettre])) * count($lettres_colonne->branches[$lettre]) * gaussienne(ECART_TYPE, ECART_TYPE); + $lettres_communes[$lettre] = log(count($lettres_ligne->branches[$lettre])) * count($lettres_colonne->branches[$lettre]) * gaussienne(0, ECART_TYPE); } uksort($lettres_communes, function($a, $b) use ($lettres_communes) { return $lettres_communes[$b] <=> $lettres_communes[$a]; diff --git a/Trie.php b/Trie.php index cfd0406..610bbe0 100644 --- a/Trie.php +++ b/Trie.php @@ -6,17 +6,20 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable public array $branches = []; private $nb_branches = 0; - public function arraySet($cles, $valeur) + public function arraySet($cles, $valeur): int { $cle = $cles[0]; - $this->nb_branches++; $cles = array_slice($cles, 1); if ($cles == []) { $this->branches[$cle] = $valeur; + $this->nb_branches = $valeur instanceof Trie ? count($valeur) : 1; + return $this->nb_branches; } else { if (!isset($this->branches[$cle])) $this->branches[$cle] = new Trie(); - $this->branches[$cle]->arraySet($cles, $valeur); + $nb_branches = $this->branches[$cle]->arraySet($cles, $valeur); + $this->nb_branches += $nb_branches; + return $nb_branches; } } @@ -42,19 +45,22 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable } } - public function arrayUnset($cles) + public function arrayUnset($cles): int { $cle = $cles[0]; $cles = array_slice($cles, 1); if ($cles == []) { + $nb_branches = $this->branches[$cle] instanceof Trie ? count($this->branches[$cle]) : 1; unset($this->branches[$cle]); - $this->nb_branches--; + $this->nb_branches -= $nb_branches; + return $nb_branches; } else { - $this->branches[$cle]->arrayUnset($cles); - $this->nb_branches--; + $nb_branches = $this->branches[$cle]->arrayUnset($cles); + $this->nb_branches -= $nb_branches; if (count($this->branches[$cle]) == 0) { unset($this->branches[$cle]); } + return $nb_branches; } } diff --git a/index.php b/index.php index 6aae3b4..ae58781 100644 --- a/index.php +++ b/index.php @@ -160,7 +160,7 @@ function definition_courante(Array $definitions, int $position) { $title = htmlspecialchars(implode("\n", $title)); ?> " data-iddv="" /> diff --git a/script.js b/script.js index 9acd66e..7a8bece 100644 --- a/script.js +++ b/script.js @@ -1,9 +1,9 @@ async function sha256(text) { - const encoder = new TextEncoder(); - const data = encoder.encode(text); - const hashBuffer = await crypto.subtle.digest("SHA-256", data); - const hashArray = Array.from(new Uint8Array(hashBuffer)); - return hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); + const encoder = new TextEncoder(); + const data = encoder.encode(text); + const hashBuffer = await crypto.subtle.digest("SHA-256", data); + const hashArray = Array.from(new Uint8Array(hashBuffer)); + return hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); } let inputs = Array.from(grilleForm.querySelectorAll(".grille input")); @@ -11,99 +11,99 @@ let largeur = Number(colonnes.value); let nb_cases = inputs.length; let index = 0; for (let input of inputs) { - input.index = index++; - input.x = input.index % largeur; - input.y = Math.floor(input.index / largeur); + input.index = index++; + input.x = input.index % largeur; + input.y = Math.floor(input.index / largeur); - input.onfocus = function (event) { - for (li of document.querySelectorAll( - `.definitions.horizontales > ol > li:nth-child(${input.y + 1})` - )) { - li.classList.add("selectionee"); - for (liEnfant of li.querySelectorAll("li")) { - if (liEnfant.id == input.dataset.iddh) { - liEnfant.classList.add("selectionee"); - } else { - liEnfant.classList.add("non-selectionee"); + input.onfocus = function (event) { + for (li of document.querySelectorAll( + `.definitions.horizontales > ol > li:nth-child(${input.y + 1})` + )) { + li.classList.add("selectionee"); + for (liEnfant of li.querySelectorAll("li")) { + if (liEnfant.id == input.dataset.iddh) { + liEnfant.classList.add("selectionee"); + } else { + liEnfant.classList.add("non-selectionee"); + } + } } - } - } - for (li of document.querySelectorAll( - `.definitions.verticales > ol > li:nth-child(${input.x + 1})` - )) { - li.classList.add("selectionee"); - for (liEnfant of li.querySelectorAll("li")) { - if (liEnfant.id == input.dataset.iddv) { - liEnfant.classList.add("selectionee"); - } else { - liEnfant.classList.add("non-selectionee"); + for (li of document.querySelectorAll( + `.definitions.verticales > ol > li:nth-child(${input.x + 1})` + )) { + li.classList.add("selectionee"); + for (liEnfant of li.querySelectorAll("li")) { + if (liEnfant.id == input.dataset.iddv) { + liEnfant.classList.add("selectionee"); + } else { + liEnfant.classList.add("non-selectionee"); + } + } } - } - } - for (li of document.querySelectorAll( - `.definitions.horizontales > ol > li:not(:nth-child(${input.y + 1})), .definitions.verticales > ol > li:not(:nth-child(${input.x + 1}))` - )) { - li.classList.add("non-selectionee"); - } - - input.select(); - }; - - input.onkeydown = function (event) { - next_input = input; - do { - switch (event.key) { - case "ArrowUp": - next_input = inputs[(next_input.index - largeur + nb_cases) % nb_cases]; - break; - case "ArrowDown": - next_input = inputs[(next_input.index + largeur) % nb_cases]; - break; - case "ArrowLeft": - next_input = inputs[(next_input.index - 1 + nb_cases) % nb_cases]; - break; - case "ArrowRight": - next_input = inputs[(next_input.index + 1) % nb_cases]; - break; - default: - return; - } - } while (next_input.disabled) - event.preventDefault(); - next_input.focus(); - next_input.select(); - }; - - input.oninput = function (event) { - this.value = this.value.toUpperCase(); - if (!input.checkValidity()) { - input.value = ""; - } - if ( - inputs.every((input) => input.value.length == 1) && - grilleForm.checkValidity() - ) { - sha256(inputs.map((input) => input.value).join("")).then((hash) => { - if (hash == solution_hashee.value) { - if (confirm("Bravo !\nUne nouvelle partie ?")) { - grilleForm.submit(); - } + for (li of document.querySelectorAll( + `.definitions.horizontales > ol > li:not(:nth-child(${input.y + 1})), .definitions.verticales > ol > li:not(:nth-child(${input.x + 1}))` + )) { + li.classList.add("non-selectionee"); } - }); - } - }; - input.onblur = function (event) { - for (li of document.querySelectorAll(".definitions li")) { - li.classList.remove("selectionee"); - li.classList.remove("non-selectionee"); + input.select(); + }; + + input.onkeydown = function (event) { + next_input = input; + do { + switch (event.key) { + case "ArrowUp": + next_input = inputs[(next_input.index - largeur + nb_cases) % nb_cases]; + break; + case "ArrowDown": + next_input = inputs[(next_input.index + largeur) % nb_cases]; + break; + case "ArrowLeft": + next_input = inputs[(next_input.index - 1 + nb_cases) % nb_cases]; + break; + case "ArrowRight": + next_input = inputs[(next_input.index + 1) % nb_cases]; + break; + default: + return; + } + } while (next_input.disabled) + event.preventDefault(); + next_input.focus(); + next_input.select(); + }; + + input.oninput = function (event) { + this.value = this.value.toUpperCase(); + if (!input.checkValidity()) { + input.value = ""; + } + if ( + inputs.every((input) => input.value.length == 1) && + grilleForm.checkValidity() + ) { + sha256(inputs.map((input) => input.value).join("")).then((hash) => { + if (hash == solution_hashee.value) { + setTimeout(() => { + if (confirm("Bravo !\nUne nouvelle partie ?")) grilleForm.submit(); + }, 0) + } + }); + } + }; + + input.onblur = function (event) { + for (li of document.querySelectorAll(".definitions li")) { + li.classList.remove("selectionee"); + li.classList.remove("non-selectionee"); + } } - } } for (let input of grilleForm.querySelectorAll(".nouvelle-grille input")) { - input.onfocus = function (event) { - input.name = input.id; - input.select(); - }; + input.onfocus = function (event) { + input.name = input.id; + input.select(); + }; } diff --git a/style.css b/style.css index 94c930a..e0fc85c 100644 --- a/style.css +++ b/style.css @@ -195,17 +195,22 @@ h2 { display: flex; align-items: baseline; position: relative; + height: 1em; } .nouvelle-grille::before { position: absolute; content: ""; - height: 16px; + height: 100%; width: 16px; background: url(favicons/favicon.svg); + background-size: contain; + background-repeat: no-repeat; + background-position: center; margin-right: 0.5em; margin-left: calc(-16px - 0.5em); padding: 0; + bottom: 0; } .nouvelle-grille :first-child {