From d852596386fde1a6ea5a47092d824e2a0b86f02b Mon Sep 17 00:00:00 2001 From: adrien Date: Fri, 2 May 2025 00:00:59 +0200 Subject: [PATCH] =?UTF-8?q?=C3=A9limination=20des=20doublons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Grille.php | 18 +++++++++++++++--- dico.php | 9 +++++++++ index.php | 15 +++++++-------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/Grille.php b/Grille.php index e5b2a48..1185b29 100644 --- a/Grille.php +++ b/Grille.php @@ -103,18 +103,30 @@ class Grille implements Iterator, ArrayAccess { $this->grille[$y][$x] = $lettre; if ($x == $this->largeur - 1) { - $this->lignes[$y] = explode(" ", $this->get_ligne($y, $this->largeur)); + $mots_ligne = explode(" ", $this->get_ligne($y, $this->largeur)); + foreach ($mots_ligne as $mot_ligne) { + if (in_array($mot_ligne, array_merge(...$this->lignes, ...$this->colonnes))) { + continue 2; + } + if (!isset($this->lignes[$y])) { + $this->lignes[$y] = []; + } + $this->lignes[$y][] = $mot_ligne; + } } else { unset($this->lignes[$y]); } if ($y == $this->hauteur - 1) { $mots_colonne = explode(" ", $this->get_colonne($x, $this->hauteur)); foreach ($mots_colonne as $mot_colonne) { - if (in_array($mot_colonne, array_merge(...$this->lignes))) { + if (in_array($mot_colonne, array_merge(...$this->lignes, ...$this->colonnes))) { continue 2; } + if (!isset($this->lignes[$x])) { + $this->colonnes[$x] = []; + } + $this->colonnes[$x][] = $mot_colonne; } - $this->colonnes[$x] = $mots_colonne; } else { unset($this->colonnes[$x]); } diff --git a/dico.php b/dico.php index b1d8093..10a4374 100644 --- a/dico.php +++ b/dico.php @@ -39,6 +39,15 @@ if (($lecteur = fopen("dico.csv", "r")) !== FALSE) { } fclose($lecteur); } +foreach ($dico as $longueur => $mots) { + foreach ($mots as $mot => $definitions) { + if (count($definitions)) { + $dico[$longueur][$mot] = $definitions[array_rand($definitions)]; + } else { + $dico[$longueur][$mot] = ""; + } + } +} function mots_espaces($longueur, $nb_mots_restants=MAX_MOTS) { diff --git a/index.php b/index.php index 4f53269..f6331ce 100644 --- a/index.php +++ b/index.php @@ -44,22 +44,21 @@ $definitions = [ foreach ($grille->lignes as $y => $mots) { $definitions["lignes"][$y] = []; foreach ($mots as $mot) { - $definitions_lignes = $dico[strlen($mot)][$mot]; - if (count($definitions_lignes)) { - $definitions["lignes"][$y][] = $definitions_lignes[array_rand($definitions_lignes)]; + $definition = $dico[strlen($mot)][$mot]; + if ($dico[strlen($mot)][$mot] != "") { + $definitions["lignes"][$y][] = $definition; } } } foreach ($grille->colonnes as $x => $mots) { - $definitions["colonnes"][$y] = []; + $definitions["colonnes"][$x] = []; foreach ($mots as $mot) { - $definitions_colonnes = $dico[strlen($mot)][$mot]; - if (count($definitions_colonnes)) { - $definitions["colonnes"][$x][] = $definitions_colonnes[array_rand($definitions_colonnes)]; + $definition = $dico[strlen($mot)][$mot]; + if ($dico[strlen($mot)][$mot] != "") { + $definitions["colonnes"][$x][] = $definition; } } } - ?>