Compare commits

...

16 Commits

Author SHA1 Message Date
adc0f040c4 test2 2025-05-23 20:51:23 +02:00
c76b8a2b30 test 2025-05-23 20:49:18 +02:00
909a1cf4c9 dz 2025-05-23 20:46:11 +02:00
667a481ffb bhk 2025-05-23 20:43:55 +02:00
acb6433b4e oihẑgrjopqpjirùjlkpjiùÉFSBHpBY^bBàyn_)NYBÀNYb,È=BBUÇ) 2025-05-23 20:36:11 +02:00
c639ec0759 Actualiser README.md 2025-05-21 10:37:02 +02:00
cb4078e4ce fix 2025-05-14 08:45:39 +02:00
40be9eeee1 serialize 2025-05-11 03:03:43 +02:00
ee76bbfc61 sauvegarder que les définitions 2025-05-11 01:57:43 +02:00
c4e98016b4 chargerle dico qu'une fois 2025-05-10 19:34:39 +02:00
97daba979f sauvegarder les mots intéressants du dico pour pas tout relire 2025-05-10 19:24:03 +02:00
b5930083bb aperçu en png et svg 2025-05-10 18:06:54 +02:00
15b7ecf4ae réorganisation 2025-05-10 14:29:04 +02:00
64b7c08f58 aperçu 2025-05-10 14:10:37 +02:00
cfb0d1fdc9 continuer après la redirection 2025-05-10 11:11:27 +02:00
eeb6889739 __str 2025-05-10 11:08:56 +02:00
11 changed files with 319 additions and 138 deletions

View File

@ -25,28 +25,19 @@ class Grille implements ArrayAccess
public $grille;
public $hauteur;
public $largeur;
public $dico;
private $dico;
private $positions;
private $nb_positions;
public $lignes = [];
public $colonnes = [];
private $lignes = [];
private $colonnes = [];
public $valide = false;
private $id;
public $definitions = [];
public function __construct($hauteur, $largeur)
{
$this->hauteur = $hauteur;
$this->largeur = $largeur;
$this->grille = array_fill(0, $hauteur, array_fill(0, $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);
$this->dico = mots_espaces(max($hauteur, $largeur));
}
public function get_ligne($y, $largeur)
@ -65,6 +56,56 @@ class Grille implements ArrayAccess
return $colonne;
}
public function genere($id)
{
mt_srand(crc32($id));
if (!isset($this->dico)) {
$this->dico = mots_espaces(max($this->hauteur, $this->largeur));
}
if (!isset($this->positions)) {
$this->positions = [];
for ($y = 0; $y < $this->hauteur; $y++) {
for ($x = 0; $x < $this->largeur; $x++)
$this->positions[] = [$x, $y];
}
$this->nb_positions = count($this->positions);
}
$grilles = $this->gen_grilles();
$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 {
return false;
}
}
public function gen_grilles($i = 0, $lettres_ligne = NULL)
{
[$x, $y] = $this->positions[$i];
@ -137,21 +178,6 @@ class Grille implements ArrayAccess
}
}
public function genere($id)
{
mt_srand(crc32($id));
$grilles = $this->gen_grilles();
$grilles->current();
if ($grilles->valid()) {
$this->save($id);
return true;
} else {
return false;
}
}
public function hash()
{
$string = "";
@ -160,13 +186,9 @@ class Grille implements ArrayAccess
return hash('sha256', $string);
}
public function save($id)
{
session_id($id);
session_start(["use_cookies" => false]);
$_SESSION["$this->largeur,$this->hauteur"] = implode(
"",
public function __toString() {
return implode(
PHP_EOL,
array_map(
function ($ligne) {
return implode("", $ligne);
@ -176,34 +198,40 @@ class Grille implements ArrayAccess
);
}
public function load($id)
public function __serialize(): array {
return [
"grille" => $this->grille,
"definitions" => $this->definitions
];
}
public function __unserialize(array $data): void {
$this->grille = $data["grille"];
$this->definitions = $data["definitions"];
}
public function save($id)
{
session_id($id);
if (session_status() === PHP_SESSION_ACTIVE) {
session_write_close();
}
session_id("$this->largeur,$this->hauteur,$id");
session_start(["use_cookies" => false]);
if (!isset($_SESSION["$this->largeur,$this->hauteur"])) {
$_SESSION = serialize($this);
}
public function load($id)
{
session_id("$this->largeur,$this->hauteur,$id");
session_start(["use_cookies" => false]);
if (!isset($_SESSION["grille"])) {
return false;
}
foreach (str_split($_SESSION["$this->largeur,$this->hauteur"], $this->largeur) 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;
});
}
unserialize($_SESSION);
return true;
}

View File

@ -1,3 +1,5 @@
# mots-croises
# Mots croisés
Générateur de grille de mots croisés en PHP
![screenshot](https://git.malingrey.fr/adrien/mots-croises/raw/branch/main/thumbnail.png)

View File

@ -6,8 +6,8 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable {
private $nb_branches = 0;
public function arraySet($cles, $valeur) {
$this->nb_branches++;
$cle = $cles[0];
$this->nb_branches++;
$cles = array_slice($cles, 1);
if ($cles == []) {
$this->branches[$cle] = $valeur;

84
apercu.png.php Normal file
View File

@ -0,0 +1,84 @@
<?php
$largeur = isset($_GET['largeur']) ? (int)$_GET['largeur'] : 200;
$hauteur = isset($_GET['hauteur']) ? (int)$_GET['hauteur'] : 200;
$lignes = isset($_GET['lignes']) ? (int)$_GET['lignes'] : 8;
$colonnes = isset($_GET['colonnes']) ? (int)$_GET['colonnes'] : 8;
$image = imagecreatetruecolor($largeur, $hauteur);
imagesavealpha($image, true);
$blanc = imagecolorallocate($image, 255, 255, 255);
$noir = imagecolorallocate($image, 0, 0, 0);
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
// Calculer la taille et la position des cases
$min_dimension = min($largeur, $hauteur);
if ($min_dimension <= 16) {
$bordure_exterieure = 0;
} else if ($min_dimension < 32) {
$bordure_exterieure = 1;
} else if ($min_dimension <= 96) {
$bordure_exterieure = 2;
} else if ($min_dimension <= 600) {
$bordure_exterieure = 3;
} else {
$bordure_exterieure = 6;
}
$cote = (int)min(($largeur - 2 * $bordure_exterieure) / $colonnes, ($hauteur - 2 * $bordure_exterieure) / $lignes);
if ($cote < 3) {
$bordure_interieure = 0;
} else if ($min_dimension < 600) {
$bordure_interieure = 1;
} else {
$bordure_interieure = 2;
}
$haut = (int)(($hauteur - $lignes * $cote - 2 * $bordure_exterieure) / 2) + (int)$bordure_exterieure;
$gauche = (int)(($largeur - $colonnes * $cote - 2 * $bordure_exterieure) / 2) + $bordure_exterieure;
$bas = $haut + $lignes * $cote - $bordure_interieure;
$droite = $gauche + $colonnes * $cote - $bordure_interieure;
// Remplir l'image avec un fond transparent
imagefill($image, 0, 0, $transparent);
// Dessiner les bordures extérieures (3 pixels d'épaisseur)
$marge1 = ceil($bordure_exterieure / 2);
$marge2 = floor($bordure_exterieure / 2);
imagesetthickness($image, $bordure_exterieure);
imagerectangle($image, $gauche - $marge1, $haut - $marge1, $droite + $marge2 - 1, $bas + $marge2 - 1, $noir);
imagefilledrectangle($image, $gauche, $haut, $droite - 1, $bas - 1, $blanc);
// Dessiner les lignes et colonnes internes (1 pixel d'épaisseur)
if ($bordure_interieure >= 1) {
imagesetthickness($image, $bordure_interieure);
for ($x = $gauche + $cote - ceil($bordure_interieure / 2); $x < $droite; $x += $cote) {
imageline($image, $x, $haut, $x, $bas, $noir); // Lignes verticales
}
for ($y = $haut + $cote - ceil($bordure_interieure / 2); $y < $bas; $y += $cote) {
imageline($image, $gauche, $y, $droite, $y, $noir); // Lignes horizontales
}
}
// Noicir les cases
if (isset($_GET["grille"])) {
include_once "Grille.php";
$grille = new Grille($lignes, $colonnes);
$id = htmlspecialchars($_GET["grille"]);
$grille->load($id) || $grille->genere($id);
for ($y = 0; $y < $lignes; $y++) {
for ($x = 0; $x < $colonnes; $x++) {
if ($grille[$y][$x] == CASE_NOIRE) {
imagefilledrectangle($image, $gauche + $x * $cote, $haut + $y * $cote, $gauche + ($x + 1) * $cote - 1, $haut + ($y + 1) * $cote - 1, $noir);
}
}
}
}
// Envoyer l'image au navigateur
header('Content-Type: image/png');
imagepng($image);
// Libérer la mémoire
imagedestroy($image);
?>

90
apercu.svg.php Normal file
View File

@ -0,0 +1,90 @@
<?php
$lignes = isset($_GET['lignes']) ? (int)$_GET['lignes'] : 8;
$colonnes = isset($_GET['colonnes']) ? (int)$_GET['colonnes'] : 8;
$bordure = 2;
$marge = $bordure / 2;
$cote = 20;
// Dimensions du SVG
$width = $colonnes * $cote; // Largeur proportionnelle au nombre de colonnes
$height = $lignes * $cote; // Hauteur proportionnelle au nombre de lignes
$rectRadius = 20; // Rayon des coins arrondis du rectangle
// Création du document XML
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
// Élément SVG principal
$svg = $doc->createElement('svg');
$svg->setAttribute('xmlns', 'http://www.w3.org/2000/svg');
$svg->setAttribute('viewBox', -$marge . " " . -$marge . " " . ($width + $bordure) . " " . ($height + $bordure));
$svg->setAttribute('width', $width + $bordure);
$svg->setAttribute('height', $height + $bordure);
$doc->appendChild($svg);
// Rectangle arrondi
$rect = $doc->createElement('rect');
$rect->setAttribute('x', 0);
$rect->setAttribute('y', 0);
$rect->setAttribute('width', $width);
$rect->setAttribute('height', $height);
$rect->setAttribute('rx', $rectRadius);
$rect->setAttribute('ry', $rectRadius);
$rect->setAttribute('fill', 'white');
$rect->setAttribute('stroke', 'black');
$rect->setAttribute('stroke-width', $bordure);
$svg->appendChild($rect);
// Lignes verticales
for ($i = 1; $i < $colonnes; $i++) {
$x = $i * $cote;
$line = $doc->createElement('line');
$line->setAttribute('x1', $x);
$line->setAttribute('y1', $marge);
$line->setAttribute('x2', $x);
$line->setAttribute('y2', $height - $marge);
$line->setAttribute('stroke', '#000');
$line->setAttribute('stroke-width', 1);
$svg->appendChild($line);
}
// Lignes horizontales
for ($i = 1; $i < $lignes; $i++) {
$y = $i * $cote;
$line = $doc->createElement('line');
$line->setAttribute('x1', $marge);
$line->setAttribute('y1', $y);
$line->setAttribute('x2', $width - $marge);
$line->setAttribute('y2', $y);
$line->setAttribute('stroke', '#000');
$line->setAttribute('stroke-width', 1);
$svg->appendChild($line);
}
// Noicir les cases
if (isset($_GET["grille"])) {
include_once "Grille.php";
$grille = new Grille($lignes, $colonnes);
$id = htmlspecialchars($_GET["grille"]);
$grille->load($id) || $grille->genere($id);
for ($y = 0; $y < $lignes; $y++) {
for ($x = 0; $x < $colonnes; $x++) {
if ($grille[$y][$x] == CASE_NOIRE) {
$rect = $doc->createElement('rect');
$rect->setAttribute('x', $x * $cote);
$rect->setAttribute('y', $y * $cote);
$rect->setAttribute('width', $cote);
$rect->setAttribute('height', $cote);
$rect->setAttribute('fill', 'black');
$svg->appendChild($rect);
}
}
}
}
header('Content-Type: image/svg+xml');
echo $doc->saveXML();

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

View File

@ -21,8 +21,8 @@
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#505050"
inkscape:zoom="8.3124997"
inkscape:cx="28.57143"
inkscape:cy="30.977445"
inkscape:cx="32"
inkscape:cy="32"
inkscape:window-width="1536"
inkscape:window-height="793"
inkscape:window-x="0"
@ -32,19 +32,19 @@
<path
fill="#ffffff"
stroke="#000000"
stroke-width="2.03093"
stroke-width="2"
stroke-miterlimit="10"
d="m 62.968825,7.9076 v 48.183453 a 6.8777723,6.877772 0 0 1 -6.876426,6.87777 H 7.9089443 a 6.8777723,6.877772 0 0 1 -6.87777,-6.876424 V 7.9076 A 6.8777723,6.877772 0 0 1 7.9075973,1.0311743 H 56.092399 A 6.8777723,6.877772 0 0 1 62.968825,7.9076 Z M 46.910816,17.089183 H 31.999999 v 14.910816 h 14.910817 z"
d="m 63,8 v 48 a 7,7 0 0 1 -7,7 H 8 a 7,7 0 0 1 -7,-7 V 9 A 7,7 0 0 1 8,1 H 56 A 7,7 0 0 1 63,8 Z M 47,17 H 32 v 15 h 15 z"
id="path1" />
<path
d="M 46.910816,17.089183 H 31.999999 v 14.910816 h 14.910817 z"
d="M 47,17 H 32 v 15 h 15 z"
id="path2"
style="stroke-width:1.34646" />
style="stroke-width:1" />
<path
fill="none"
stroke="#000000"
stroke-width="2.06235"
stroke-width="2"
stroke-miterlimit="10"
d="M 46.910816,62.968823 V 31.999999 m 0,-14.910815 V 1.0311743 M 31.999999,62.968823 V 31.999999 m 0,-14.910815 V 1.0311743 m -16.058011,0 V 62.968823 M 46.910816,31.999999 h 16.058009 m -30.968826,0 H 1.0311743 M 62.968825,46.910815 H 1.0311743 M 62.968825,15.941991 H 1.0311743"
d="M 47,63 V 32 m 0,-15 V 1 M 32,63 V 32 m 0,-15 V 1 m -16,0 V 63 M 47,32 h 16 m -31,0 H 1 M 63,47 H 1 M 63,16 H 1"
id="path3" />
</svg>

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

104
index.php
View File

@ -28,14 +28,34 @@ $largeur = filter_input(INPUT_GET, 'colonnes', FILTER_VALIDATE_INT, [
$grille = new Grille($hauteur, $largeur);
$basedir = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].dirname($_SERVER["DOCUMENT_URI"]);
if (!isset($_GET["grille"]) || $_GET["grille"] == "") {
do {
$id = uniqid();
} while (!$grille->genere($id));
$grille_valide = $grille->genere($id);
} while (!$grille_valide);
$_GET["grille"] = $id;
header("Location: $basedir/?" . http_build_query($_GET));
exit;
} else {
$id = htmlspecialchars($_GET["grille"]);
$grille_valide = $grille->load($id) || $grille->genere($id);
}
function formatter_definition($definition) {
if (strpos($definition, "#") !== false) {
[$definition, $nb_mots] = explode("#", $definition);
$nb_mots = " <small>($nb_mots mots)</small>";
} else {
$nb_mots = "";
}
if (strpos($definition, "@") !== false) {
[$definition, $auteur] = explode("@", $definition);
$auteur = " <small><em>$auteur</em></small>";
} else {
$auteur = "";
}
return $definition;
}
?>
<!DOCTYPE HTML>
@ -47,77 +67,21 @@ if (!isset($_GET["grille"]) || $_GET["grille"] == "") {
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="favicon.ico" />
<link rel="icon" type="image/svg+xml" href="favicons/favicon.svg">
<link rel="icon" type="image/png" href="favicons/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/svg+xml" href="apercu.svg.php?grille=<?=$id?>&lignes=<?=$hauteur?>&colonnes=<?=$largeur?>">
<link rel="icon" type="image/png" sizes="96x96" href="apercu.png.php?grille=<?=$id?>&lignes=<?=$hauteur?>&colonnes=<?=$largeur?>&largeur=96&hauteur=96" />
<link rel="apple-touch-icon" sizes="180x180" href="favicons/apple-touch-icon.png" />
<meta name="apple-mobile-web-app-title" content="🄼🄾🅃🅂 🄲🅁🄾🄸🅂🄴🅂" />
<link rel="manifest" href="site.webmanifest" />
<meta property="og:title" content="🄼🄾🅃🅂▣🄲🅁🄾🄸🅂🄴🅂"/>
<meta property="og:type" content="game"/>
<meta property="og:url" content="<?=$basedir?>"/>
<meta property="og:image" content="<?=$basedir?>/thumbnail.png"/>
<meta property="og:image:width" content="192"/>
<meta property="og:image:height" content="192"/>
<meta property="og:image" content="<?=$basedir?>/apercu.png.php?grille=<?=$id?>&lignes=<?=$hauteur?>&colonnes=<?=$largeur?>&largeur=1200&hauteur=630"/>
<meta property="og:image:width" content="1200"/>
<meta property="og:image:height" content="630"/>
<meta property="og:locale" content="fr_FR"/>
<meta property="og:site_name" content="<?=$_SERVER["HTTP_HOST"]?>"/>
</head>
<?php
$id = htmlspecialchars($_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 = " <small>($nb_mots mots)</small>";
} else {
$nb_mots = "";
}
if (strpos($definition, "@") !== false) {
[$definition, $auteur] = explode("@", $definition);
$auteur = " <small><em>$auteur</em></small>";
} else {
$auteur = "";
}
$definitions_horizontales[$y][] = $definition;
}
}
}
$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 = " <small>($nb_mots mots)</small>";
} else {
$nb_mots = "";
}
if (strpos($definition, "@") !== false) {
[$definition, $auteur] = explode("@", $definition);
$auteur = " <small><em>$auteur</em></small>";
} else {
$auteur = "";
}
$definitions_verticales[$x][] = $definition . $nb_mots . $auteur;
}
}
}
}
?>
<body>
<form id="grilleForm" method="get" location=".">
<h1 class="large width">
@ -170,7 +134,7 @@ if ($grille_valide) {
<?php else: ?>
<td class="case blanche">
<input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1" pattern="[A-Z]" placeholder="<?= $grille[$y][$x] ?>"
title="<?= strip_tags("→ " . implode("\n→ ", $definitions_horizontales[$y]) . "\n↓ " . implode("\n↓ ", $definitions_verticales[$x])) ?>" />
title="<?= strip_tags("→ " . implode("\n→ ", array_map("formatter_definition", $grille->definitions["horizontales"][$y] ?? [])) . "\n↓ " . implode("\n↓ ", array_map("formatter_definition", $grille->definitions["verticales"][$x] ?? []))) ?>" />
</td>
<?php endif; ?>
<?php endfor; ?>
@ -181,15 +145,15 @@ if ($grille_valide) {
<div class="definitions horizontales">
<h2>Horizontalement</h2>
<ol type="1">
<?php foreach ($definitions_horizontales as $y => $definitions): ?>
<?php foreach ($grille->definitions["horizontales"] as $y => $definitions): ?>
<li>
<?php if (count($definitions)): ?>
<?php if (count($definitions) == 1): ?>
<?= $definitions[0] ?>
<?= formatter_definition($definitions[0]) ?>
<?php else: ?>
<ol>
<?php foreach ($definitions as $definition) : ?>
<li><?= $definition ?></li>
<li><?= formatter_definition($definition) ?></li>
<?php endforeach ?>
</ol>
<?php endif ?>
@ -201,15 +165,15 @@ if ($grille_valide) {
<div class="definitions verticales">
<h2>Verticalement</h2>
<ol type="A">
<?php foreach ($definitions_verticales as $x => $definitions): ?>
<?php foreach ($grille->definitions["verticales"] as $x => $definitions): ?>
<li>
<?php if (count($definitions)): ?>
<?php if (count($definitions) == 1): ?>
<?= $definitions[0] ?>
<?= formatter_definition($definitions[0]) ?>
<?php else: ?>
<ol>
<?php foreach ($definitions as $definition) : ?>
<li><?= $definition ?></li>
<li><?= formatter_definition($definition) ?></li>
<?php endforeach ?>
</ol>
<?php endif ?>

View File

@ -108,6 +108,7 @@ h2 {
}
.grille input[disabled] {
color: black;
background-color: black;
}

12
vercel.json Normal file
View File

@ -0,0 +1,12 @@
{
"functions": {
"api/*.php": {
"runtime": "vercel-php@0.7.3"
}
},
"routes": [
{ "src": "/(.*)", "dest": "/index.php" }
]
}