From d867e4049915def527c03220ddbe1df7db9b491e Mon Sep 17 00:00:00 2001 From: adrien Date: Fri, 2 May 2025 00:47:00 +0200 Subject: [PATCH] mots_espace renvoie une liste de mots --- Grille.php | 9 ++++----- dico.php | 12 ++++++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Grille.php b/Grille.php index 1185b29..300e818 100644 --- a/Grille.php +++ b/Grille.php @@ -34,7 +34,8 @@ class Grille implements Iterator, ArrayAccess { $this->lettres_suivantes = []; foreach ($hauteur == $largeur ? [$hauteur] : [$hauteur, $largeur] as $longueur) { $this->lettres_suivantes[$longueur] = []; - foreach (mots_espaces($longueur, $hauteur == $largeur ? MAX_MOTS : MAX_MOTS/2) as $mot) { + foreach (mots_espaces($longueur, $hauteur == $largeur ? MAX_MOTS : MAX_MOTS/2) as $mots) { + $mot = implode(" ", $mots); $ref = &$this->lettres_suivantes[$longueur]; for ($i = 0; $i < $longueur; $i++) { $lettre = $mot[$i]; @@ -103,8 +104,7 @@ class Grille implements Iterator, ArrayAccess { $this->grille[$y][$x] = $lettre; if ($x == $this->largeur - 1) { - $mots_ligne = explode(" ", $this->get_ligne($y, $this->largeur)); - foreach ($mots_ligne as $mot_ligne) { + foreach (explode(" ", $this->get_ligne($y, $this->largeur)) as $mot_ligne) { if (in_array($mot_ligne, array_merge(...$this->lignes, ...$this->colonnes))) { continue 2; } @@ -117,8 +117,7 @@ class Grille implements Iterator, ArrayAccess { unset($this->lignes[$y]); } if ($y == $this->hauteur - 1) { - $mots_colonne = explode(" ", $this->get_colonne($x, $this->hauteur)); - foreach ($mots_colonne as $mot_colonne) { + foreach (explode(" ", $this->get_colonne($x, $this->hauteur)) as $mot_colonne) { if (in_array($mot_colonne, array_merge(...$this->lignes, ...$this->colonnes))) { continue 2; } diff --git a/dico.php b/dico.php index 10a4374..4a251ce 100644 --- a/dico.php +++ b/dico.php @@ -54,16 +54,16 @@ function mots_espaces($longueur, $nb_mots_restants=MAX_MOTS) global $dico; foreach ($dico[$longueur] as $mot => $definition) { - yield $mot; + yield [$mot]; if (--$nb_mots_restants <= 0) return; } for ($i = MIN_LETTRES_MOT_1; $longueur - $i - 1 >= MIN_LETTRES_MOT_2; $i++) { - foreach ($dico[$i] as $mot1 => $definition) { - foreach (mots_espaces($longueur - $i - 1, $nb_mots_restants) as $mot2) { - if ($mot1 != $mot2) { - yield "$mot1 $mot2"; + foreach ($dico[$i] as $mot => $definition) { + foreach (mots_espaces($longueur - $i - 1, $nb_mots_restants) as $mots) { + if (!in_array($mot, $mots)) { + yield [$mot, ...$mots]; if (--$nb_mots_restants <= 0) return; - yield "$mot2 $mot1"; + yield [...$mots, $mot]; if (--$nb_mots_restants <= 0) return; } }