diff --git a/Grille.php b/Grille.php index 5b91e28..c6b9b72 100644 --- a/Grille.php +++ b/Grille.php @@ -8,127 +8,106 @@ class Grille { public $hauteur; public $largeur; private $grilles; - private $debuts; + private $lettres_suivantes; + private $positions; + private $nb_positions; private $mots_utilises = []; public function __construct($hauteur, $largeur, $id="") { $this->hauteur = $hauteur; $this->largeur = $largeur; - $this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '.')); + $this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '')); if ($id == "") { mt_srand(); } else { mt_srand(crc32($id)); } - $this->debuts = []; + $this->lettres_suivantes = []; foreach ($hauteur == $largeur? [$hauteur]: [$hauteur, $largeur] as $longueur) { - $this->debuts[$longueur] = []; - $nb_mots = 0; + $this->lettres_suivantes[$longueur] = []; foreach(mots_espaces($longueur) as $mot) { for ($i = 0; $i <= $longueur; $i++) { $debut = substr($mot, 0, $i); - if (!isset($this->debuts[$longueur][$debut])) { - $this->debuts[$longueur][$debut] = []; + if (!isset($this->lettres_suivantes[$longueur][$debut])) { + $this->lettres_suivantes[$longueur][$debut] = []; } - $this->debuts[$longueur][$debut][] = $mot; + $this->lettres_suivantes[$longueur][$debut][substr($mot, $i, 1)] = true; } } } mt_srand(); + + $this->positions = []; + for ($y = 0; $y < $hauteur; $y++) { + for ($x = 0; $x < $largeur; $x++) { + $this->positions[] = [$x, $y]; + } + } + $this->nb_positions = count($this->positions); $this->grilles = $this->generateur(); } - public function get_ligne($l, $largeur) { + public function get_ligne($y, $largeur) { $ligne = ""; - for ($c = 0; $c < $largeur; $c++) { - $ligne .= $this->grille[$l][$c]; + for ($x = 0; $x < $largeur; $x++) { + $ligne .= $this->grille[$y][$x]; } return $ligne; } - public function set_ligne($l, $mot) { - for ($i = 0; $i < strlen($mot); $i++) { - $this->grille[$l][$i] = $mot[$i]; - } - } - - public function get_colonne($c, $hauteur) { + public function get_colonne($x, $hauteur) { $colonne = ""; - for ($l = 0; $l < $hauteur; $l++) { - $colonne .= $this->grille[$l][$c]; + for ($y = 0; $y < $hauteur; $y++) { + $colonne .= $this->grille[$y][$x]; } return $colonne; } - - public function set_colonne($c, $mot) { - for ($i = 0; $i < strlen($mot); $i++) { - $this->grille[$i][$c] = $mot[$i]; - } - } - public function generateur() { - yield from $this->trouve_une_ligne(0); - $this->grille = array_fill(0, $this->hauteur, array_fill(0, $this->largeur, ' ')); - } - - private function trouve_une_ligne($l) { - global $mots_de_n_lettres; - $largeur = min($l, $this->largeur); - $hauteur = min($l + 1, $this->hauteur); - foreach ($this->debuts[$this->largeur][$this->get_ligne($l, $largeur)] as $mot_lig) { - $this->set_ligne($l, $mot_lig); - $ok = true; - for ($c = $l; $c < $this->largeur; $c++) { - if (!isset($this->debuts[$this->hauteur][$this->get_colonne($c, $hauteur)])) { - $ok = false; - break; - } - } - if (!$ok) { - continue; - } - $this->mots_utilises[$mot_lig] = true; - if ($l < $this->largeur) { - yield from $this->trouve_une_colonne($l); - } else if ($l + 1 < $this->hauteur) { - yield from $this->trouve_une_ligne($l + 1); - } else { - yield $this; - } - unset($this->mots_utilises[$mot_lig]); + public function generateur($index=0) { + if ($index == $this->nb_positions) { + yield $this; + return; } - } - private function trouve_une_colonne($c) { - global $mots_de_n_lettres; - $hauteur = min($c + 1, $this->hauteur); - $largeur = min($c + 1, $this->largeur); - foreach ($this->debuts[$this->hauteur][$this->get_colonne($c, $hauteur)] as $mot_col) { - if (isset($this->mots_utilises[$mot_col])) { - continue; - } - $this->set_colonne($c, $mot_col); - $ok = true; - for ($l = $c; $l < $this->hauteur; $l++) { - if (!isset($this->debuts[$this->largeur][$this->get_ligne($l, $largeur)])) { - $ok = false; - break; + [$x, $y] = $this->positions[$index]; + + $lettres_possibles = array_intersect_assoc( + $this->lettres_suivantes[$this->largeur][$this->get_ligne($y, $x)], + $this->lettres_suivantes[$this->hauteur][$this->get_colonne($x, $y)] + ); + + foreach ($lettres_possibles as $lettre => $_) { + $this->grille[$y][$x] = $lettre; + + $mot_ligne = NULL; + if ($x == $this->largeur - 1) { + $mot_ligne = $this->get_ligne($y, $x); + if (isset($this->mots_utilises[$mot_ligne])) { + continue; + } else { + $this-> mots_utilises[$mot_ligne] = true; } } - if (!$ok) { - continue; + $mot_colonne = NULL; + if ($y == $this->hauteur - 1) { + $mot_colonne = $this->get_colonne($x, $y); + if (isset($this->mots_utilises[$mot_colonne])) { + continue; + } else { + $this-> mots_utilises[$mot_colonne] = true; + } } - $this->mots_utilises[$mot_col] = true; - if ($c +1 < $this->hauteur) { - yield from $this->trouve_une_ligne($c + 1); - } else if ($c + 1 < $this->largeur) { - yield from $this->trouve_une_colonne($c + 1); - } else { - yield $this; + + yield from $this->generateur($index + 1); + + if ($mot_ligne) { + unset($this-> mots_utilises[$mot_ligne]); + } + if ($mot_colonne) { + unset($this-> mots_utilises[$mot_colonne]); } - unset($this->mots_utilises[$mot_col]); } } diff --git a/Grille0.php b/Grille0.php new file mode 100644 index 0000000..ee7129a --- /dev/null +++ b/Grille0.php @@ -0,0 +1,149 @@ +hauteur = $hauteur; + $this->largeur = $largeur; + $this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '.')); + + if ($id == "") { + mt_srand(); + } else { + mt_srand(crc32($id)); + } + $this->debuts = []; + foreach ($hauteur == $largeur? [$hauteur]: [$hauteur, $largeur] as $longueur) { + $this->debuts[$longueur] = []; + foreach(mots_espaces($longueur) as $mot) { + for ($i = 0; $i <= $longueur; $i++) { + $debut = substr($mot, 0, $i); + if (!isset($this->debuts[$longueur][$debut])) { + $this->debuts[$longueur][$debut] = []; + } + $this->debuts[$longueur][$debut][] = $mot; + } + } + } + mt_srand(); + + $this->grilles = $this->generateur(); + } + + public function get_ligne($y, $largeur) { + $ligne = ""; + for ($x = 0; $x < $largeur; $x++) { + $ligne .= $this->grille[$y][$x]; + } + return $ligne; + } + + public function set_ligne($y, $mot) { + for ($i = 0; $i < strlen($mot); $i++) { + $this->grille[$y][$i] = $mot[$i]; + } + } + + public function get_colonne($x, $hauteur) { + $colonne = ""; + for ($y = 0; $y < $hauteur; $y++) { + $colonne .= $this->grille[$y][$x]; + } + return $colonne; + } + + public function set_colonne($x, $mot) { + for ($i = 0; $i < strlen($mot); $i++) { + $this->grille[$i][$x] = $mot[$i]; + } + } + + public function generateur() { + yield from $this->trouve_une_ligne(0); + $this->grille = array_fill(0, $this->hauteur, array_fill(0, $this->largeur, ' ')); + } + + private function trouve_une_ligne($y) { + global $mots_de_n_lettres; + $largeur = min($y, $this->largeur); + $hauteur = min($y + 1, $this->hauteur); + foreach ($this->debuts[$this->largeur][$this->get_ligne($y, $largeur)] as $mot_lig) { + $this->set_ligne($y, $mot_lig); + $ok = true; + for ($x = $y; $x < $this->largeur; $x++) { + if (!isset($this->debuts[$this->hauteur][$this->get_colonne($x, $hauteur)])) { + $ok = false; + break; + } + } + if (!$ok) { + continue; + } + $this->mots_utilises[$mot_lig] = true; + if ($y < $this->largeur) { + yield from $this->trouve_une_colonne($y); + } else if ($y + 1 < $this->hauteur) { + yield from $this->trouve_une_ligne($y + 1); + } else { + yield $this; + } + unset($this->mots_utilises[$mot_lig]); + } + } + + private function trouve_une_colonne($x) { + global $mots_de_n_lettres; + $hauteur = min($x + 1, $this->hauteur); + $largeur = min($x + 1, $this->largeur); + foreach ($this->debuts[$this->hauteur][$this->get_colonne($x, $hauteur)] as $mot_col) { + if (isset($this->mots_utilises[$mot_col])) { + continue; + } + $this->set_colonne($x, $mot_col); + $ok = true; + for ($y = $x; $y < $this->hauteur; $y++) { + if (!isset($this->debuts[$this->largeur][$this->get_ligne($y, $largeur)])) { + $ok = false; + break; + } + } + if (!$ok) { + continue; + } + $this->mots_utilises[$mot_col] = true; + if ($x +1 < $this->hauteur) { + yield from $this->trouve_une_ligne($x + 1); + } else if ($x + 1 < $this->largeur) { + yield from $this->trouve_une_colonne($x + 1); + } else { + yield $this; + } + unset($this->mots_utilises[$mot_col]); + } + } + + public function current() { + return $this->grilles->current(); + } + + public function valid() { + return $this->grilles->valid(); + } + + public function hash() { + $string = ""; + foreach ($this->grille as $ligne) { + $string .= implode("", $ligne); + } + return hash('sha256', $string); + } +} \ No newline at end of file diff --git a/dico.php b/dico.php index 25ae938..eec5000 100644 --- a/dico.php +++ b/dico.php @@ -2,7 +2,7 @@ const MIN_LETTRES_MOT_1 = 2; -const MIN_LETTRES_MOT_2 = 0; +const MIN_LETTRES_MOT_2 = 1; // const MAX_MOTS = 100000; diff --git a/index.php b/index.php index 36997f0..07ab631 100644 --- a/index.php +++ b/index.php @@ -84,17 +84,17 @@ $grille->current();
- - | = chr($c + 65) ?> | + += chr($x + 65) ?> | |
---|---|---|---|
= $l + 1 ?> | - -"> - grille[$l][$c] == " "): ?> + | = $y + 1 ?> | + +">
+ grille[$y][$x] == " "): ?>
@@ -108,14 +108,14 @@ $grille->current();
Horizontalement
|