Eviter les doublons lorsqu'il y a plusieurs mots en ligne

This commit is contained in:
Adrien MALINGREY 2025-05-01 23:25:21 +02:00
parent b042fcd77a
commit 38344a315c
4 changed files with 73 additions and 72 deletions

View File

@ -103,17 +103,18 @@ class Grille implements Iterator, ArrayAccess {
$this->grille[$y][$x] = $lettre;
if ($x == $this->largeur - 1) {
$this->lignes[$y] = $this->get_ligne($y, $this->largeur);
$this->lignes[$y] = explode(" ", $this->get_ligne($y, $this->largeur));
} else {
unset($this->lignes[$y]);
}
if ($y == $this->hauteur - 1) {
$colonne = $this->get_colonne($x, $this->hauteur);
if (in_array($this->get_colonne($x, $y), $this->lignes)) {
continue;
} else {
$this->colonnes[$x] = $colonne;
$mots_colonne = explode(" ", $this->get_colonne($x, $this->hauteur));
foreach ($mots_colonne as $mot_colonne) {
if (in_array($mot_colonne, array_merge(...$this->lignes))) {
continue 2;
}
}
$this->colonnes[$x] = $mots_colonne;
} else {
unset($this->colonnes[$x]);
}

View File

@ -9,7 +9,7 @@ $dico = [[]];
if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($lecteur, 0, "\t");
while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) {
if ($ligne[0] != NULL && substr($ligne[0], 0, 1) == "#") {
if ($ligne[0] == NULL || substr($ligne[0], 0, 1) == "#") {
continue;
}
switch(count($ligne)) {
@ -33,8 +33,10 @@ if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
if (!isset($dico[$longueur][$mot])) {
$dico[$longueur][$mot] = [];
}
if (strlen($definition)) {
$dico[$longueur][$mot][] = $definition;
}
}
fclose($lecteur);
}

View File

@ -43,21 +43,19 @@ $definitions = [
];
foreach ($grille->lignes as $y => $mots) {
$definitions["lignes"][$y] = [];
foreach (explode(" ", $mots) as $mot) {
foreach ($mots as $mot) {
$definitions_lignes = $dico[strlen($mot)][$mot];
$definition = $definitions_lignes[array_rand($definitions_lignes)];
if (strlen($definition)) {
$definitions["lignes"][$y][] = $definition;
if (count($definitions_lignes)) {
$definitions["lignes"][$y][] = $definitions_lignes[array_rand($definitions_lignes)];
}
}
}
foreach ($grille->colonnes as $x => $mots) {
$definitions["colonnes"][$y] = [];
foreach (explode(" ", $mots) as $mot) {
foreach ($mots as $mot) {
$definitions_colonnes = $dico[strlen($mot)][$mot];
$definition = $definitions_colonnes[array_rand($definitions_colonnes)];
if (strlen($definition)) {
$definitions["colonnes"][$x][] = $definition;
if (count($definitions_colonnes)) {
$definitions["colonnes"][$x][] = $definitions_colonnes[array_rand($definitions_colonnes)];
}
}
}