From ee76bbfc61da2303c60df33d0660d025492fade8 Mon Sep 17 00:00:00 2001 From: adrien Date: Sun, 11 May 2025 01:57:43 +0200 Subject: [PATCH] =?UTF-8?q?sauvegarder=20que=20les=20d=C3=A9finitions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Grille.php | 65 ++++++++++++++++++++---------------------------- index.php | 72 ++++++++++++++---------------------------------------- 2 files changed, 46 insertions(+), 91 deletions(-) diff --git a/Grille.php b/Grille.php index 6cb5979..6852be0 100644 --- a/Grille.php +++ b/Grille.php @@ -31,7 +31,7 @@ class Grille implements ArrayAccess public $lignes = []; public $colonnes = []; public $valide = false; - private $id; + public $definitions = []; public function __construct($hauteur, $largeur) { @@ -73,6 +73,28 @@ class Grille implements ArrayAccess $grilles->current(); if ($grilles->valid()) { + $this->definitions = [ + "horizontales" => [], + "verticales" => [] + ]; + foreach($this->lignes as $y => $mots) { + $this->definitions["horizontales"][$y] = []; + foreach($mots as $mot) { + $definitions = $this->dico[strlen($mot)][$mot]; + if (count($definitions)) { + $this->definitions["horizontales"][$y][] = $definitions[mt_rand(0, count($definitions) - 1)]; + } + } + } + foreach($this->colonnes as $x => $mots) { + $this->definitions["verticales"][$x] = []; + foreach($mots as $mot) { + $definitions = $this->dico[strlen($mot)][$mot]; + if (count($definitions)) { + $this->definitions["verticales"][$x][] = $definitions[mt_rand(0, count($definitions) - 1)]; + } + } + } $this->save($id); return true; } else { @@ -181,22 +203,8 @@ class Grille implements ArrayAccess session_id("$this->largeur,$this->hauteur,$id"); session_start(["use_cookies" => false]); - $_SESSION["grille"] = (string)$this; - $_SESSION["dico"] = []; - foreach ($this->lignes as $y => $mots) { - foreach($mots as $mot) { - $longueur = strlen($mot); - if (!isset($_SESSION["dico"][$longueur])) $_SESSION["dico"][$longueur] = []; - $_SESSION["dico"][$longueur][$mot] = $this->dico[$longueur][$mot]; - } - } - foreach ($this->colonnes as $y => $mots) { - foreach($mots as $mot) { - $longueur = strlen($mot); - if (!isset($_SESSION["dico"][$longueur])) $_SESSION["dico"][$longueur] = []; - $_SESSION["dico"][$longueur][$mot] = $this->dico[$longueur][$mot]; - } - } + $_SESSION["grille"] = $this->grille; + $_SESSION["definitions"] = $this->definitions; } public function load($id) @@ -208,27 +216,8 @@ class Grille implements ArrayAccess return false; } - foreach (explode(PHP_EOL, $_SESSION["grille"]) as $y => $ligne) { - foreach (str_split($ligne) as $x => $lettre) { - $this->grille[$y][$x] = $lettre; - } - } - - for ($y = 0; $y < $this->hauteur; $y++) { - $mots = explode(CASE_NOIRE, $this->get_ligne($y, $this->largeur)); - $this->lignes[$y] = array_filter($mots, function ($mot) { - return strlen($mot) >= 2; - }); - } - - for ($x = 0; $x < $this->largeur; $x++) { - $mots = explode(CASE_NOIRE, $this->get_colonne($x, $this->hauteur)); - $this->colonnes[$x] = array_filter($mots, function ($mot) { - return strlen($mot) >= 2; - }); - } - - $this->dico = $_SESSION["dico"]; + $this->grille = $_SESSION["grille"]; + $this->definitions = $_SESSION["definitions"]; return true; } diff --git a/index.php b/index.php index 0d9faed..7aaf9f4 100644 --- a/index.php +++ b/index.php @@ -42,54 +42,20 @@ if (!isset($_GET["grille"]) || $_GET["grille"] == "") { $grille_valide = $grille->load($id) || $grille->genere($id); } -mt_srand(crc32($id)); -if ($grille_valide) { - $definitions_horizontales = []; - for ($y = 0; $y < $hauteur; $y++) { - $definitions_horizontales[$y] = []; - foreach ($grille->lignes[$y] as $mot) { - $definitions = $grille->dico[strlen($mot)][$mot]; - if (count($definitions)) { - $definition = $definitions[mt_rand(0, count($definitions) - 1)]; - if (strpos($definition, "#") !== false) { - [$definition, $nb_mots] = explode("#", $definition); - $nb_mots = " ($nb_mots mots)"; - } else { - $nb_mots = ""; - } - if (strpos($definition, "@") !== false) { - [$definition, $auteur] = explode("@", $definition); - $auteur = " $auteur"; - } else { - $auteur = ""; - } - $definitions_horizontales[$y][] = $definition; - } - } +function formatter_definition($definition) { + if (strpos($definition, "#") !== false) { + [$definition, $nb_mots] = explode("#", $definition); + $nb_mots = " ($nb_mots mots)"; + } else { + $nb_mots = ""; } - $definitions_verticales = []; - for ($x = 0 ; $x < $largeur; $x++) { - $definitions_verticales[$x] = []; - foreach ($grille->colonnes[$x] as $mot) { - $definitions = $grille->dico[strlen($mot)][$mot]; - if (count($definitions)) { - $definition = $definitions[mt_rand(0, count($definitions) - 1)]; - if (strpos($definition, "#") !== false) { - [$definition, $nb_mots] = explode("#", $definition); - $nb_mots = " ($nb_mots mots)"; - } else { - $nb_mots = ""; - } - if (strpos($definition, "@") !== false) { - [$definition, $auteur] = explode("@", $definition); - $auteur = " $auteur"; - } else { - $auteur = ""; - } - $definitions_verticales[$x][] = $definition . $nb_mots . $auteur; - } - } + if (strpos($definition, "@") !== false) { + [$definition, $auteur] = explode("@", $definition); + $auteur = " $auteur"; + } else { + $auteur = ""; } + return $definition; } ?> @@ -168,7 +134,7 @@ if ($grille_valide) { " /> + title="definitions["horizontales"][$y])) . "\n↓ " . implode("\n↓ ", array_map("formatter_definition", $grille->definitions["verticales"][$x]))) ?>" /> @@ -179,15 +145,15 @@ if ($grille_valide) {

Horizontalement

    - $definitions): ?> + definitions["horizontales"] as $y => $definitions): ?>
  1. - +
      -
    1. +
    @@ -199,15 +165,15 @@ if ($grille_valide) {

    Verticalement

      - $definitions): ?> + definitions["verticales"] as $x => $definitions): ?>
    1. - +
        -
      1. +