omettre les mots plus grands que la grille
This commit is contained in:
parent
ab9e1f08ef
commit
d5a120cd9a
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
test*.*
|
test*.*
|
||||||
*.py
|
*.py
|
||||||
|
dico2.csv
|
||||||
|
wiktionaryXfr2010.7z
|
||||||
|
16
Grille.php
16
Grille.php
@ -22,7 +22,7 @@ class Grille implements ArrayAccess
|
|||||||
public $grille;
|
public $grille;
|
||||||
public $hauteur;
|
public $hauteur;
|
||||||
public $largeur;
|
public $largeur;
|
||||||
private $lettres_suivantes;
|
public $dico;
|
||||||
private $positions;
|
private $positions;
|
||||||
private $nb_positions;
|
private $nb_positions;
|
||||||
public $lignes = [];
|
public $lignes = [];
|
||||||
@ -43,7 +43,7 @@ class Grille implements ArrayAccess
|
|||||||
}
|
}
|
||||||
$this->nb_positions = count($this->positions);
|
$this->nb_positions = count($this->positions);
|
||||||
|
|
||||||
$this->lettres_suivantes = tries(max($hauteur, $largeur));
|
$this->dico = mots_espaces(max($hauteur, $largeur));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_ligne($y, $largeur)
|
public function get_ligne($y, $largeur)
|
||||||
@ -69,11 +69,11 @@ class Grille implements ArrayAccess
|
|||||||
// Recherche de la prochaine lettre possible sur la case courante
|
// Recherche de la prochaine lettre possible sur la case courante
|
||||||
// en ligne
|
// en ligne
|
||||||
if ($x == 0) {
|
if ($x == 0) {
|
||||||
$lettres_ligne = $this->lettres_suivantes[$this->largeur];
|
$lettres_ligne = $this->dico[$this->largeur];
|
||||||
}
|
}
|
||||||
|
|
||||||
// en colonne
|
// en colonne
|
||||||
$lettres_colonne = $this->lettres_suivantes[$this->hauteur];
|
$lettres_colonne = $this->dico[$this->hauteur];
|
||||||
for ($y2 = 0; $y2 < $y; $y2++) {
|
for ($y2 = 0; $y2 < $y; $y2++) {
|
||||||
$lettres_colonne = $lettres_colonne->branches[$this->grille[$y2][$x]];
|
$lettres_colonne = $lettres_colonne->branches[$this->grille[$y2][$x]];
|
||||||
}
|
}
|
||||||
@ -93,10 +93,10 @@ class Grille implements ArrayAccess
|
|||||||
$this->grille[$y][$x] = $lettre;
|
$this->grille[$y][$x] = $lettre;
|
||||||
|
|
||||||
// Omission des lettres isolées
|
// Omission des lettres isolées
|
||||||
if ($lettre == " " &&
|
if ($lettre == " "
|
||||||
($y - 2 < 0 || $this->grille[$y - 2][$x] == " ") &&
|
&& ($y - 2 < 0 || $this->grille[$y - 2][$x] == " ")
|
||||||
($y - 1 < 0 || $x - 1 < 0 || $this->grille[$y - 1][$x - 1] == " ") &&
|
&& ($y - 1 < 0 || $x - 1 < 0 || $this->grille[$y - 1][$x - 1] == " ")
|
||||||
($y - 1 < 0 || $x + 1 >= $this->largeur || $this->grille[$y - 1][$x + 1] == " ")
|
&& ($y - 1 < 0 || $x + 1 >= $this->largeur || $this->grille[$y - 1][$x + 1] == " ")
|
||||||
) {
|
) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
23
dico.php
23
dico.php
@ -6,11 +6,17 @@ const MIN_PREMIER_MOT = 1;
|
|||||||
const MIN_MOTS_SUIVANTS = 1;
|
const MIN_MOTS_SUIVANTS = 1;
|
||||||
|
|
||||||
|
|
||||||
|
function dico($longueur_max) {
|
||||||
$dico = [[""]];
|
$dico = [[""]];
|
||||||
if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
|
if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
|
||||||
$header = fgetcsv($lecteur, 0, "\t");
|
$header = fgetcsv($lecteur, 0, "\t");
|
||||||
while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) {
|
while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) {
|
||||||
if ($ligne[0] == NULL || substr($ligne[0], 0, 1) == "#") continue;
|
if (
|
||||||
|
$ligne[0] == NULL
|
||||||
|
|| substr($ligne[0], 0, 1) == "#"
|
||||||
|
|| strlen($ligne[0]) > $longueur_max
|
||||||
|
) continue;
|
||||||
|
|
||||||
switch(count($ligne)) {
|
switch(count($ligne)) {
|
||||||
case 1:
|
case 1:
|
||||||
[$mot] = $ligne;
|
[$mot] = $ligne;
|
||||||
@ -24,6 +30,7 @@ if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
|
|||||||
$definition .= " <small><em>$auteur</em></small>";
|
$definition .= " <small><em>$auteur</em></small>";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$mot = str_split(strtoupper($mot));
|
$mot = str_split(strtoupper($mot));
|
||||||
$longueur = count($mot);
|
$longueur = count($mot);
|
||||||
if (!isset($dico[$longueur])) $dico[$longueur] = new Trie();
|
if (!isset($dico[$longueur])) $dico[$longueur] = new Trie();
|
||||||
@ -33,19 +40,21 @@ if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
|
|||||||
fclose($lecteur);
|
fclose($lecteur);
|
||||||
}
|
}
|
||||||
|
|
||||||
function tries($longueur_max) {
|
return $dico;
|
||||||
global $dico;
|
}
|
||||||
|
|
||||||
|
function mots_espaces($longueur_max) {
|
||||||
|
$dico = dico($longueur_max);
|
||||||
|
|
||||||
$_tries = [[]];
|
$_tries = [[]];
|
||||||
for ($longueur = 1; $longueur <= $longueur_max; $longueur++) {
|
for ($longueur = 1; $longueur <= $longueur_max; $longueur++) {
|
||||||
$_tries[$longueur] = $dico[$longueur];
|
|
||||||
for ($position_espace = MIN_PREMIER_MOT; $position_espace + MIN_MOTS_SUIVANTS < $longueur; $position_espace++) {
|
for ($position_espace = MIN_PREMIER_MOT; $position_espace + MIN_MOTS_SUIVANTS < $longueur; $position_espace++) {
|
||||||
$mots_suivants = $_tries[$longueur - $position_espace - 1];
|
$mots_suivants = $dico[$longueur - $position_espace - 1];
|
||||||
foreach ($dico[$position_espace] as $premier_mot => $definition) {
|
foreach ($dico[$position_espace] as $premier_mot => $definition) {
|
||||||
$premier_mot[] = " ";
|
$premier_mot[] = " ";
|
||||||
$_tries[$longueur][$premier_mot] = $mots_suivants;
|
$dico[$longueur][$premier_mot] = $mots_suivants;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $_tries;
|
return $dico;
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ if ($grille_valide) {
|
|||||||
for ($y = 0; $y < $hauteur; $y++) {
|
for ($y = 0; $y < $hauteur; $y++) {
|
||||||
$definitions_horizontales[$y] = [];
|
$definitions_horizontales[$y] = [];
|
||||||
foreach ($grille->lignes[$y] as $mot) {
|
foreach ($grille->lignes[$y] as $mot) {
|
||||||
$definitions = $dico[strlen($mot)][str_split($mot)];
|
$definitions = $grille->dico[strlen($mot)][str_split($mot)];
|
||||||
if (count($definitions)) {
|
if (count($definitions)) {
|
||||||
$definitions_horizontales[$y][] = $definitions[mt_rand(0, count($definitions) - 1)];
|
$definitions_horizontales[$y][] = $definitions[mt_rand(0, count($definitions) - 1)];
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ if ($grille_valide) {
|
|||||||
for ($x = 0 ; $x < $largeur; $x++) {
|
for ($x = 0 ; $x < $largeur; $x++) {
|
||||||
$definitions_verticales[$x] = [];
|
$definitions_verticales[$x] = [];
|
||||||
foreach ($grille->colonnes[$x] as $mot) {
|
foreach ($grille->colonnes[$x] as $mot) {
|
||||||
$definitions = $dico[strlen($mot)][str_split($mot)];
|
$definitions = $grille->dico[strlen($mot)][str_split($mot)];
|
||||||
if (count($definitions)) {
|
if (count($definitions)) {
|
||||||
$definitions_verticales[$x][] = $definitions[mt_rand(0, count($definitions) - 1)];
|
$definitions_verticales[$x][] = $definitions[mt_rand(0, count($definitions) - 1)];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user