hauteur = $hauteur; $this->largeur = $largeur; $this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '')); $this->lignes = []; $this->colonnes = []; $this->lettres_suivantes = tries(max($hauteur, $largeur)); $this->positions = []; for ($y = 0; $y < $hauteur; $y++) { for ($x = 0; $x < $largeur; $x++) $this->positions[] = [$x, $y]; } $this->nb_positions = count($this->positions); mt_srand($id == "" ? null : crc32($id)); $this->grilles = $this->generateur(0); } public function get_ligne($y, $largeur) { $ligne = ""; for ($x = 0; $x < $largeur; $x++) $ligne .= $this->grille[$y][$x]; return $ligne; } public function get_colonne($x, $hauteur) { $colonne = ""; for ($y = 0; $y < $hauteur; $y++) $colonne .= $this->grille[$y][$x]; return $colonne; } public function generateur($i, $lettres_suivantes_ligne = NULL) { if ($i == $this->nb_positions) { yield $this; return; } [$x, $y] = $this->positions[$i]; if ($x) { $lettres_suivantes_ligne = $lettres_suivantes_ligne->noeud[$this->grille[$y][$x-1]]; } else { $lettres_suivantes_ligne = $this->lettres_suivantes[$this->largeur]; } $lettres_suivantes_colonne = $this->lettres_suivantes[$this->hauteur]; for ($y2 = 0; $y2 < $y; $y2++) $lettres_suivantes_colonne = $lettres_suivantes_colonne->noeud[$this->grille[$y2][$x]]; $lettres_communes = array_intersect( array_keys($lettres_suivantes_ligne->noeud), array_keys($lettres_suivantes_colonne->noeud) ); usort($lettres_communes, function ($a, $b) { return mt_rand(-1, 1); }); if (MAX_ESSAIS) $lettres_communes = array_slice($lettres_communes, 0, MAX_ESSAIS); foreach ($lettres_communes as $lettre) { $this->grille[$y][$x] = $lettre; $mots = []; if ($x == $this->largeur - 1) $mots = explode(" ", $this->get_ligne($y, $this->largeur)); else if ($lettre == " ") $mots = explode(" ", $this->get_ligne($y, $x)); else $mots = []; $this->lignes[$y] = array_filter($mots, function($mot) { return strlen($mot) >= 2; }); if (count($this->lignes[$y])) { $mot = array_pop($this->lignes[$y]); if (strlen($mot > 2) && in_array($mot, array_merge(...$this->lignes, ...$this->colonnes))) continue; else $this->lignes[$y][] = $mot; } if ($y == $this->hauteur - 1) { $mots = explode(" ", $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; else $this->colonnes[$x][$rang] = $mot; } } else { $this->colonnes[$x] = []; } if ($i < $this->nb_positions) { yield from $this->generateur($i + 1, $lettres_suivantes_ligne); } else { yield $this; } } } public function hash() { $string = ""; foreach ($this->grille as $ligne) $string .= implode("", $ligne); return hash('sha256', $string); } public function current(): mixed { return $this->grilles->current(); } public function key(): mixed { return $this->grilles->key(); } public function next(): void { $this->grilles->next(); } public function rewind(): void { $this->grilles->rewind(); } public function valid(): bool { return $this->grilles->valid(); } public function offsetExists(mixed $offset): bool { return isset($this->grille[$offset]); } public function offsetGet(mixed $offset): mixed { return $this->grille[$offset]; } public function offsetSet(mixed $offset, mixed $value): void { $this->grille[$offset] = $value; } public function offsetUnset(mixed $offset): void { unset($this->grille[$offset]); } }