From 4b69fa4803d8d4313299f8ebaeecb482d1a52370 Mon Sep 17 00:00:00 2001
From: adrien <adrien@malingrey.fr>
Date: Fri, 25 Apr 2025 02:11:21 +0200
Subject: [PATCH] ajustement de mots_espaces

---
 Grille.php | 16 ++++++++--------
 dico.csv   |  1 +
 dico.php   | 39 +++++++++++++++++++++++----------------
 index.php  | 26 +++++++++++++-------------
 style.css  |  5 +++++
 5 files changed, 50 insertions(+), 37 deletions(-)

diff --git a/Grille.php b/Grille.php
index 6a45b5a..04dc223 100644
--- a/Grille.php
+++ b/Grille.php
@@ -3,9 +3,6 @@
 include_once "dico.php";
 
 
-const MIN_LETTRES = 1;
-
-
 class Grille {
     public $grille;
     public $hauteur;
@@ -19,15 +16,16 @@ class Grille {
         $this->largeur = $largeur;
         $this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '.'));
 
-        if ($hauteur == $largeur) {
-            $dimensions = [$hauteur];
+        if ($id == "") {
+            mt_srand();
         } else {
-            $dimensions = [$hauteur, $largeur];
+            mt_srand(crc32($id));
         }
         $this->mots_commencant_par = [];
-        foreach ($dimensions as $longueur) {
+        foreach ($hauteur == $largeur? [$hauteur]: [$hauteur, $largeur] as $longueur) {
             $this->mots_commencant_par[$longueur] = [];
-            foreach(mots_espaces($longueur, MIN_LETTRES, crc32($id)) as $mot) {
+            $nb_mots = 0;
+            foreach(mots_espaces($longueur) as $mot) {
                 for ($i = 0; $i <= $longueur; $i++) {
                     $debut = substr($mot, 0, $i);
                     if (!isset($this->mots_commencant_par[$longueur][$debut])) {
@@ -37,6 +35,8 @@ class Grille {
                 }
             }
         }
+        mt_srand();
+        
         $this->grilles = $this->generateur();
         $this->grilles->current();
     }
diff --git a/dico.csv b/dico.csv
index e6a6039..683d182 100644
--- a/dico.csv
+++ b/dico.csv
@@ -1,4 +1,5 @@
 #MOT	Définition
+	
 A	
 ABAT	Tripe ou étripe. (Robert Scipion)
 ABATTIS	Architecte à la retraite
diff --git a/dico.php b/dico.php
index e9dcf08..56ffff2 100644
--- a/dico.php
+++ b/dico.php
@@ -1,5 +1,10 @@
 <?php
 
+
+const MIN_LETTRES = 0;
+const MAX_MOTS = 100000;
+
+
 $dico = [];
 if (($handle = fopen("dico.csv", "r")) !== FALSE) {
     $header = fgetcsv($handle, 0, "\t");
@@ -13,7 +18,7 @@ if (($handle = fopen("dico.csv", "r")) !== FALSE) {
     fclose($handle);
 }
 
-$mots_de_n_lettres = [[]];
+$mots_de_n_lettres = [];
 foreach ($dico as $mot => $definition) {
     $n = strlen($mot);
     if (!isset($mots_de_n_lettres[$n])) {
@@ -22,39 +27,41 @@ foreach ($dico as $mot => $definition) {
     $mots_de_n_lettres[$n][] = $mot;
 }
 
-function fisherYatesShuffle(&$items, $seed)
+function fisherYatesShuffle(&$items)
 {
-    mt_srand($seed);
-    for ($i = count($items) - 1; $i > 0; $i--)
-    {
-        $j = @mt_rand(0, $i);
+    for ($i = count($items) - 1; $i > 0; $i--) {
+        $j = mt_rand(0, $i);
         $tmp = $items[$i];
         $items[$i] = $items[$j];
         $items[$j] = $tmp;
     }
-    mt_srand();
 }
 
-function mots_espaces($max, $min=0, $seed=0) {
+function mots_espaces($longueur) {
     global $mots_de_n_lettres;
     global $dico;
 
-    if ($seed) {
-        fisherYatesShuffle($mots_de_n_lettres[$max], $seed);
-    } else {
-        shuffle($mots_de_n_lettres[$max]);
-    }
-    foreach($mots_de_n_lettres[$max] as $mot) {
+    $nb_mots = 0;
+    fisherYatesShuffle($mots_de_n_lettres[$longueur]);
+    foreach($mots_de_n_lettres[$longueur] as $mot) {
         yield $mot;
+        $nb_mots++;
+        if ($nb_mots > MAX_MOTS) {
+            return;
+        }
     }
-    for ($i = ceil($max / 2); $max - $i -1 >= $min; $i++) {
+    for ($i = 2; $longueur - $i - 1 >= MIN_LETTRES; $i++) {
         foreach ($mots_de_n_lettres[$i] as $mot1) {
-            foreach (mots_espaces($max - $i -1, $min) as $mot2) {
+            foreach (mots_espaces($longueur - $i - 1) as $mot2) {
                 if ($mot1 != $mot2) {
                     $dico["$mot1 $mot2"] = $dico[$mot1] && $dico[$mot2] ? "{$dico[$mot1]}<br/>{$dico[$mot2]}." : $dico[$mot1] . $dico[$mot2];
                     yield "$mot1 $mot2";
                     $dico["$mot2 $mot1"] = $dico[$mot2] && $dico[$mot1] ? "{$dico[$mot2]}<br/>{$dico[$mot1]}." : $dico[$mot2] . $dico[$mot1];
                     yield "$mot2 $mot1";
+                    $nb_mots += 2;
+                    if ($nb_mots > MAX_MOTS) {
+                        break;
+                    }
                 }
             }
         }
diff --git a/index.php b/index.php
index a983c09..b07bd24 100644
--- a/index.php
+++ b/index.php
@@ -1,18 +1,14 @@
 <?php
-
-const HAUTEUR_PAR_DEFAUT = 6;
-const LARGEUR_PAR_DEFAUT = 6;
+ini_set('display_errors', '1');
+ini_set('error_reporting', E_ALL);
 
 
-$id = filter_input(INPUT_GET, 'grille', FILTER_VALIDATE_REGEXP, [
-    "options" => [
-        "regexp" => "/^[a-f0-9]{13}$/"
-    ]
-]);
-if (!$id) {
+if (!isset($_GET["grille"])) {
     $_GET["grille"] = uniqid();
-    header("Location: " . $_SERVER['DOCUMENT_URI'] . "?" . http_build_query($_GET));
+    header("Location: " . dirname($_SERVER['DOCUMENT_URI']) . "?" . http_build_query($_GET));
     exit;
+} else {
+    $id = htmlspecialchars($_GET["grille"]);
 }
 
 
@@ -20,6 +16,10 @@ include_once "dico.php";
 include_once "Grille.php";
 
 
+const HAUTEUR_PAR_DEFAUT = 6;
+const LARGEUR_PAR_DEFAUT = 6;
+
+
 $hauteur = filter_input(INPUT_GET, 'lignes', FILTER_VALIDATE_INT, [
     "options" => [
         "default" => HAUTEUR_PAR_DEFAUT,
@@ -118,9 +118,9 @@ $grille = new Grille($hauteur, $largeur, $id);
                 </ol>
             </div>
         </div>
-
-        <input type="hidden" id="lignes" name="lignes" value="<?= $hauteur ?>" />
-        <input type="hidden" id="colonnes" name="colonnes" value="<?= $largeur ?>" />
+        
+        <input type="hidden" id="lignes" <?php if (isset($_GET["lignes"])): ?>name="lignes" <?php endif ?>value="<?= $hauteur ?>" />
+        <input type="hidden" id="colonnes" <?php if (isset($_GET["colonnes"])): ?>name="colonnes" <?php endif ?>value="<?= $largeur ?>" />
         <input type="hidden" id="solution_hashee" value="<?= $grille->hash() ?>" />
         <button type="submit">Nouvelle grille</button>
     </form>
diff --git a/style.css b/style.css
index 7099531..708d3bf 100644
--- a/style.css
+++ b/style.css
@@ -12,6 +12,10 @@ form {
     justify-content: space-evenly;
 }
 
+h1 {
+    margin: 0;
+}
+
 h1 table {
     margin: auto;
     line-height: 0.8;
@@ -19,6 +23,7 @@ h1 table {
 
 h1 td {
     width: 0.7em;
+    text-align: center;
 }
 
 h1,