diff --git a/Grille.php b/Grille.php index 4559c7e..6324fe4 100644 --- a/Grille.php +++ b/Grille.php @@ -16,6 +16,26 @@ function gaussienne($moyenne = 0, $ecartType = 1.0): float { return $z * $ecartType + $moyenne; } +function explode_pos(string $separator, string $string): array { + $mots = []; + $mot = ""; + $longueur = strlen($string); + + for ($i = 0; $i < $longueur; $i++) { + $lettre = $string[$i]; + if ($lettre == $separator) { + $mots[$i] = $mot; + $mot = ""; + } else { + $mot .= $lettre; + } + } + + $mots[$i] = $mot; + + return $mots; +} + class Grille implements ArrayAccess { @@ -80,27 +100,27 @@ class Grille implements ArrayAccess ]; foreach($this->lignes as $y => $mots) { $this->definitions["horizontales"][$y] = []; - foreach($mots as $mot) { + foreach($mots as $fin => $mot) { $definitions = $this->dico[strlen($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][] = $definition; + $this->definitions["horizontales"][$y][$fin] = $definition; } } } foreach($this->colonnes as $x => $mots) { $this->definitions["verticales"][$x] = []; - foreach($mots as $mot) { + foreach($mots as $fin => $mot) { $definitions = $this->dico[strlen($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][] = $definition; + $this->definitions["verticales"][$x][$fin] = $definition; } } } @@ -161,8 +181,8 @@ class Grille implements ArrayAccess // Omission des doublons $mots = []; - if ($x == $this->largeur - 1) $mots = explode(CASE_NOIRE, $this->get_ligne($y, $this->largeur)); - else if ($lettre == CASE_NOIRE) $mots = explode(CASE_NOIRE, $this->get_ligne($y, $x)); + if ($x == $this->largeur - 1) $mots = explode_pos(CASE_NOIRE, $this->get_ligne($y, $this->largeur)); + else if ($lettre == CASE_NOIRE) $mots = explode_pos(CASE_NOIRE, $this->get_ligne($y, $x)); else $mots = []; $this->lignes[$y] = array_filter($mots, function ($mot) { return strlen($mot) >= 2; @@ -174,7 +194,7 @@ class Grille implements ArrayAccess } if ($y == $this->hauteur - 1) { - $mots = explode(CASE_NOIRE, $this->get_colonne($x, $this->hauteur)); + $mots = explode_pos(CASE_NOIRE, $this->get_colonne($x, $this->hauteur)); foreach ($mots as $rang => $mot) { if (strlen($mot) < 2) continue; if (strlen($mot > 2) && in_array($mot, array_merge(...$this->lignes, ...$this->colonnes))) continue 2; diff --git a/index.php b/index.php index 4341dcf..eadb9f2 100644 --- a/index.php +++ b/index.php @@ -59,6 +59,13 @@ function formatter_definition($definition) { } return ucfirst($definition[DEFINITION]) . $nb_mots . $auteur; } + +function mot_courant($mots, $position) { + foreach ($mots as $fin => $mot) { + if ($position <= $fin) return $mot; + } + return []; +} ?> @@ -137,8 +144,18 @@ function formatter_definition($definition) { + definitions["horizontales"][$y], $x); + $definition_verticale = mot_courant($grille->definitions["verticales"][$x], $y); + if (isset($definition_horizontale[0])) $title .= "→ " . $definition_horizontale[0]; + if (isset($definition_horizontale[1])) $title .= " (" . $definition_horizontale[1] . ")"; + if (isset($definition_verticale[0])) $title .= "\n ↓ " . $definition_verticale[0]; + if (isset($definition_verticale[1])) $title .= " (" . $definition_verticale[1] . ")"; + $title = htmlspecialchars($title); + ?> definitions["horizontales"][$y] ?? [])) . "\n↓ " . implode("\n↓ ", array_map("formatter_definition", $grille->definitions["verticales"][$x] ?? []))) ?>" /> + title="" /> @@ -153,7 +170,7 @@ function formatter_definition($definition) {
  • - +
      @@ -173,7 +190,7 @@ function formatter_definition($definition) {
    1. - +