From ca95b76558e894d4b7d28a9731203fd60d358b5a Mon Sep 17 00:00:00 2001 From: adrien Date: Sat, 3 May 2025 12:43:11 +0200 Subject: [PATCH] =?UTF-8?q?ne=20v=C3=A9rifier=20les=20doublons=20que=20sur?= =?UTF-8?q?=20le=20dernier=20mot=20de=20la=20ligne?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Grille.php | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Grille.php b/Grille.php index 17b9649..4e50039 100644 --- a/Grille.php +++ b/Grille.php @@ -98,22 +98,29 @@ class Grille implements Iterator, ArrayAccess { foreach ($lettres_communes as $lettre => $_) { $this->grille[$y][$x] = $lettre; - if ($x == 0) $this->lignes[$y] = []; - if ($x == $this->largeur - 1) $this->lignes[$y] = explode(" ", $this->get_ligne($y, $this->largeur)); - else if ($lettre == " ") $this->lignes[$y] = explode(" ", $this->get_ligne($y, $x)); - if (count($this->lignes[$y])) { - $mot = array_pop($this->lignes[$y]); - if (strlen($mot) >= 1 && in_array($mot, array_merge(...$this->lignes, ...$this->colonnes))) continue; - $this->lignes[$y][] = $mot; + $mots = []; + if ($x == $this->largeur - 1) $mots = explode(" ", $this->get_ligne($y, $this->largeur)); + else if ($lettre == " ") $mots = explode(" ", $this->get_ligne($y, $x)); + $mots = array_filter($mots, function($mot) { + return strlen($mot) > 1; + }); + if (count($mots) >= 1) { + $dernier_mot = array_pop($mots); + $this->lignes[$y] = $mots; + if (in_array($dernier_mot, array_merge(...$this->lignes, ...$this->colonnes))) continue; + else $this->lignes[$y][] = $dernier_mot; } - if ($y == 0) $this->colonnes[$x] = []; - if ($y == $this->hauteur - 1) $this->colonnes[$x] = explode(" ", $this->get_colonne($x, $this->hauteur)); - else if ($lettre == " ") $this->colonnes[$x] = explode(" ", $this->get_colonne($x, $y)); - if (count($this->colonnes[$x])) { - $mot = array_pop($this->colonnes[$x]); - if (strlen($mot) >= 1 && in_array($mot, array_merge(...$this->lignes, ...$this->colonnes))) continue; - $this->colonnes[$x][] = $mot; + + if ($y == $this->hauteur - 1) { + $mots = explode(" ", $this->get_colonne($x, $this->hauteur)); + foreach ($mots as $rang => $mot) { + if (strlen($mot) <= 1) continue; + if (in_array($mot, array_merge(...$this->lignes, ...$this->colonnes))) continue 2; + else $this->colonnes[$x][$rang] = $mot; + } + } else { + $this->colonnes[$x] = []; } if ($i < $this->nb_positions) {