diff --git a/Grille.php b/Grille.php index 6b0e7a7..5845dab 100644 --- a/Grille.php +++ b/Grille.php @@ -171,7 +171,6 @@ class Grille implements ArrayAccess $this->grille ) ); - var_dump($_SESSION); } public function load($id) @@ -180,7 +179,6 @@ class Grille implements ArrayAccess session_start(["use_cookies" => false]); if (!isset($_SESSION["$this->largeur,$this->hauteur"])) { - var_dump($_SESSION); return false; } diff --git a/Trie.php b/Trie.php index 8a30a10..42ee6f8 100644 --- a/Trie.php +++ b/Trie.php @@ -5,8 +5,7 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable { public array $branches = []; private $nb_branches = 0; - // ArrayAccess - public function offsetSet($cles, $valeur): void { + public function arraySet($cles, $valeur) { $this->nb_branches++; $cle = $cles[0]; $cles = array_slice($cles, 1); @@ -14,38 +13,38 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable { $this->branches[$cle] = $valeur; } else { if (!isset($this->branches[$cle])) $this->branches[$cle] = new Trie(); - $this->branches[$cle]->offsetSet($cles, $valeur); + $this->branches[$cle]->arraySet($cles, $valeur); } } - public function offsetExists($cles): bool { + public function arrayExists($cles) { $cle = $cles[0]; $cles = array_slice($cles, 1); if ($cles == []) { return isset($this->branches[$cle]); } else { - return isset($this->branches[$cle]) && $this->branches[$cle]->offsetExists($cles); + return isset($this->branches[$cle]) && $this->branches[$cle]->arrayExists($cles); } } - public function &offsetGet($cles): mixed { + public function &arrayGet($cles) { $cle = $cles[0]; $cles = array_slice($cles, 1); if ($cles == []) { return $this->branches[$cle]; } else { - return $this->branches[$cle]->offsetGet($cles); + return $this->branches[$cle]->arrayGet($cles); } } - public function offsetUnset($cles): void { + public function arrayUnset($cles) { $cle = $cles[0]; $cles = array_slice($cles, 1); if ($cles == []) { unset($this->branches[$cle]); $this->nb_branches--; } else { - $this->branches[$cle]->offsetUnset($cles); + $this->branches[$cle]->arrayUnset($cles); $this->nb_branches--; if (count($this->branches[$cle]) == 0) { unset($this->branches[$cle]); @@ -53,11 +52,10 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable { } } - // IteratorAggregate - public function getIterator(): Traversable { + public function arrayIterator() { foreach ($this->branches as $cle => $branche) { if ($branche instanceof Trie) { - foreach($branche as $sous_cles => $feuille) { + foreach($branche->arrayIterator() as $sous_cles => $feuille) { yield array_merge([$cle], $sous_cles) => $feuille; } } else { @@ -66,6 +64,30 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable { } } + // ArrayAccess + public function offsetSet($string, $valeur): void { + $this->arraySet(str_split($string), $valeur); + } + + public function offsetExists($string): bool { + return $this->arrayExists(str_split($string)); + } + + public function &offsetGet($string): mixed { + return $this->arrayGet(str_split($string)); + } + + public function offsetUnset($string): void { + $this->arrayUnset(str_split($string)); + } + + // IteratorAggregate + public function getIterator(): Traversable { + foreach($this->arrayIterator() as $array => $valeur) { + yield implode("", $array) => $valeur; + } + } + // Countable public function count(): int { return $this->nb_branches; diff --git a/dico.php b/dico.php index 83a6a3e..113f14e 100644 --- a/dico.php +++ b/dico.php @@ -31,8 +31,8 @@ function dico($longueur_max) { break; } - $mot = str_split(strtoupper($mot)); - $longueur = count($mot); + $mot = strtoupper($mot); + $longueur = strlen($mot); if (!isset($dico[$longueur])) $dico[$longueur] = new Trie(); if (!isset($dico[$longueur][$mot])) $dico[$longueur][$mot] = []; if (strlen($definition)) $dico[$longueur][$mot][] = $definition; @@ -50,9 +50,9 @@ function mots_espaces($longueur_max) { for ($longueur = 1; $longueur <= $longueur_max; $longueur++) { for ($position_espace = MIN_PREMIER_MOT; $position_espace + MIN_MOTS_SUIVANTS < $longueur; $position_espace++) { $mots_suivants = $dico[$longueur - $position_espace - 1]; - foreach ($dico[$position_espace] as $premier_mot => $definition) { + foreach ($dico[$position_espace]->arrayIterator() as $premier_mot => $definition) { $premier_mot[] = " "; - $dico[$longueur][$premier_mot] = $mots_suivants; + $dico[$longueur]->arraySet($premier_mot, $mots_suivants); } } } diff --git a/index.php b/index.php index f8f6c09..e570c88 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,4 @@ lignes[$y] as $mot) { - $definitions = $grille->dico[strlen($mot)][str_split($mot)]; + $definitions = $grille->dico[strlen($mot)][$mot]; if (count($definitions)) { $definitions_horizontales[$y][] = $definitions[mt_rand(0, count($definitions) - 1)]; } @@ -59,7 +58,7 @@ if ($grille_valide) { for ($x = 0 ; $x < $largeur; $x++) { $definitions_verticales[$x] = []; foreach ($grille->colonnes[$x] as $mot) { - $definitions = $grille->dico[strlen($mot)][str_split($mot)]; + $definitions = $grille->dico[strlen($mot)][$mot]; if (count($definitions)) { $definitions_verticales[$x][] = $definitions[mt_rand(0, count($definitions) - 1)]; }