diff --git a/Grille.php b/Grille.php index f4d3883..9bba935 100644 --- a/Grille.php +++ b/Grille.php @@ -20,19 +20,20 @@ function explode_pos(string $separator, string $string): array { $mots = []; $mot = ""; $longueur = strlen($string); + $debut = 0; for ($i = 0; $i < $longueur; $i++) { $lettre = $string[$i]; if ($lettre == $separator) { - $mots[$i] = $mot; + $mots[] = ["mot" => $mot, "debut" => $debut, "fin" => $i - 1, "longueur" => $i - $debut]; $mot = ""; + $debut = $i + 1; } else { $mot .= $lettre; } } - $mots[$i] = $mot; - + $mots[] = ["mot" => $mot, "debut" => $debut, "fin" => $longueur - 1, "longueur" => $longueur - $debut]; return $mots; } @@ -101,29 +102,35 @@ class Grille implements ArrayAccess for ($y = 0; $y < $this->hauteur; $y++) { $mots = explode_pos(CASE_NOIRE, $this->get_ligne($y, $this->largeur)); $this->definitions["horizontales"][$y] = []; - foreach($mots as $fin => $mot) { - $definitions = $this->dico[strlen($mot)][$mot]; + foreach($mots as $mot) { + $definitions = $this->dico[$mot["longueur"]][$mot["mot"]]; if (count($definitions)) { $definition = $definitions[mt_rand(0, count($definitions) - 1)]; - if (isset($definitions["nb_mots"])) { - $definition["nb_mots"] = $definitions["nb_mots"]; - } - $this->definitions["horizontales"][$y][$fin] = $definition; + if (isset($definition[DEFINITION])) $mot["definition"] = $definition[DEFINITION]; + if (isset($definition[AUTEUR])) $mot["auteur"] = $definition[AUTEUR]; } + if (isset($definitions["nb_mots"])) { + $mot["nb_mots"] = $definitions["nb_mots"]; + } + unset($mot["mot"]); + $this->definitions["horizontales"][$y][] = $mot; } } for ($x = 0; $x < $this->largeur; $x++) { $mots = explode_pos(CASE_NOIRE, $this->get_colonne($x, $this->hauteur)); $this->definitions["verticales"][$x] = []; - foreach($mots as $fin => $mot) { - $definitions = $this->dico[strlen($mot)][$mot]; + foreach($mots as $mot) { + $definitions = $this->dico[$mot["longueur"]][$mot["mot"]]; if (count($definitions)) { $definition = $definitions[mt_rand(0, count($definitions) - 1)]; - if (isset($definitions["nb_mots"])) { - $definition["nb_mots"] = $definitions["nb_mots"]; - } - $this->definitions["verticales"][$x][$fin] = $definition; + if (isset($definition[DEFINITION])) $mot["definition"] = $definition[DEFINITION]; + if (isset($definition[AUTEUR])) $mot["auteur"] = $definition[AUTEUR]; } + if (isset($definitions["nb_mots"])) { + $mot["nb_mots"] = $definitions["nb_mots"]; + } + unset($mot["mot"]); + $this->definitions["verticales"][$x][] = $mot; } } $this->save($id); diff --git a/index.php b/index.php index 5785e1a..6f8f073 100644 --- a/index.php +++ b/index.php @@ -1,13 +1,16 @@ 1){ + if (isset($definition["nb_mots"]) && $definition["nb_mots"] > 1) { $nb_mots = $definition["nb_mots"]; $nb_mots = " ($nb_mots mots)"; } else { $nb_mots = ""; } - if (array_key_exists(AUTEUR, $definition)) { - $auteur = $definition[AUTEUR]; + if (isset($definition["auteur"])) { + $auteur = $definition["auteur"]; $auteur = " $auteur"; } else { $auteur = ""; } - return ucfirst($definition[DEFINITION]) . $nb_mots . $auteur; + return ucfirst($definition["definition"]) . $nb_mots . $auteur; } -function mot_courant($mots, $position) { - foreach ($mots as $fin => $mot) { - if ($position <= $fin) return $mot; +function definition_courante($definitions, $position) { + foreach ($definitions as $id => $definition) { + if ($position <= $definition["fin"]) + return [$id, $definition]; } return []; } - -function debuts($definitions) { - $debut = 0; - $retour = []; - foreach ($definitions as $fin => $definition) { - if ($fin - $debut > 1) { - $retour[$debut] = $definition; - } - $debut = $fin + 1; - } - return $retour; -} ?> -
- -