référenement des labels

This commit is contained in:
2025-12-05 21:22:52 +01:00
parent 459c0e9ed2
commit cd3946aeeb
2 changed files with 207 additions and 184 deletions

View File

@@ -20,19 +20,20 @@ function explode_pos(string $separator, string $string): array {
$mots = []; $mots = [];
$mot = ""; $mot = "";
$longueur = strlen($string); $longueur = strlen($string);
$debut = 0;
for ($i = 0; $i < $longueur; $i++) { for ($i = 0; $i < $longueur; $i++) {
$lettre = $string[$i]; $lettre = $string[$i];
if ($lettre == $separator) { if ($lettre == $separator) {
$mots[$i] = $mot; $mots[] = ["mot" => $mot, "debut" => $debut, "fin" => $i - 1, "longueur" => $i - $debut];
$mot = ""; $mot = "";
$debut = $i + 1;
} else { } else {
$mot .= $lettre; $mot .= $lettre;
} }
} }
$mots[$i] = $mot; $mots[] = ["mot" => $mot, "debut" => $debut, "fin" => $longueur - 1, "longueur" => $longueur - $debut];
return $mots; return $mots;
} }
@@ -101,29 +102,35 @@ class Grille implements ArrayAccess
for ($y = 0; $y < $this->hauteur; $y++) { for ($y = 0; $y < $this->hauteur; $y++) {
$mots = explode_pos(CASE_NOIRE, $this->get_ligne($y, $this->largeur)); $mots = explode_pos(CASE_NOIRE, $this->get_ligne($y, $this->largeur));
$this->definitions["horizontales"][$y] = []; $this->definitions["horizontales"][$y] = [];
foreach($mots as $fin => $mot) { foreach($mots as $mot) {
$definitions = $this->dico[strlen($mot)][$mot]; $definitions = $this->dico[$mot["longueur"]][$mot["mot"]];
if (count($definitions)) { if (count($definitions)) {
$definition = $definitions[mt_rand(0, count($definitions) - 1)]; $definition = $definitions[mt_rand(0, count($definitions) - 1)];
if (isset($definitions["nb_mots"])) { if (isset($definition[DEFINITION])) $mot["definition"] = $definition[DEFINITION];
$definition["nb_mots"] = $definitions["nb_mots"]; if (isset($definition[AUTEUR])) $mot["auteur"] = $definition[AUTEUR];
}
$this->definitions["horizontales"][$y][$fin] = $definition;
} }
if (isset($definitions["nb_mots"])) {
$mot["nb_mots"] = $definitions["nb_mots"];
}
unset($mot["mot"]);
$this->definitions["horizontales"][$y][] = $mot;
} }
} }
for ($x = 0; $x < $this->largeur; $x++) { for ($x = 0; $x < $this->largeur; $x++) {
$mots = explode_pos(CASE_NOIRE, $this->get_colonne($x, $this->hauteur)); $mots = explode_pos(CASE_NOIRE, $this->get_colonne($x, $this->hauteur));
$this->definitions["verticales"][$x] = []; $this->definitions["verticales"][$x] = [];
foreach($mots as $fin => $mot) { foreach($mots as $mot) {
$definitions = $this->dico[strlen($mot)][$mot]; $definitions = $this->dico[$mot["longueur"]][$mot["mot"]];
if (count($definitions)) { if (count($definitions)) {
$definition = $definitions[mt_rand(0, count($definitions) - 1)]; $definition = $definitions[mt_rand(0, count($definitions) - 1)];
if (isset($definitions["nb_mots"])) { if (isset($definition[DEFINITION])) $mot["definition"] = $definition[DEFINITION];
$definition["nb_mots"] = $definitions["nb_mots"]; if (isset($definition[AUTEUR])) $mot["auteur"] = $definition[AUTEUR];
}
$this->definitions["verticales"][$x][$fin] = $definition;
} }
if (isset($definitions["nb_mots"])) {
$mot["nb_mots"] = $definitions["nb_mots"];
}
unset($mot["mot"]);
$this->definitions["verticales"][$x][] = $mot;
} }
} }
$this->save($id); $this->save($id);

354
index.php
View File

@@ -1,13 +1,16 @@
<?php <?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
include_once "Grille.php"; include_once "Grille.php";
const HAUTEUR_DEFAUT = 7; const HAUTEUR_DEFAUT = 7;
const HAUTEUR_MIN = 2; const HAUTEUR_MIN = 2;
const HAUTEUR_MAX = 10; const HAUTEUR_MAX = 10;
const LARGEUR_DEFAUT = 7; const LARGEUR_DEFAUT = 7;
const LARGEUR_MIN = 2; const LARGEUR_MIN = 2;
const LARGEUR_MAX = 10; const LARGEUR_MAX = 10;
$hauteur = filter_input(INPUT_GET, 'lignes', FILTER_VALIDATE_INT, [ $hauteur = filter_input(INPUT_GET, 'lignes', FILTER_VALIDATE_INT, [
@@ -27,7 +30,7 @@ $largeur = filter_input(INPUT_GET, 'colonnes', FILTER_VALIDATE_INT, [
]); ]);
$grille = new Grille($hauteur, $largeur); $grille = new Grille($hauteur, $largeur);
$basedir = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].dirname($_SERVER["DOCUMENT_URI"]); $basedir = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . dirname($_SERVER["DOCUMENT_URI"]);
if (!isset($_GET["grille"]) || $_GET["grille"] == "") { if (!isset($_GET["grille"]) || $_GET["grille"] == "") {
do { do {
@@ -43,209 +46,222 @@ if (!isset($_GET["grille"]) || $_GET["grille"] == "") {
} }
function formatter_definition($definition) { function formatter_definition($definition) {
if (isset($definition["nb_mots"]) && $definition["nb_mots"] > 1){ if (isset($definition["nb_mots"]) && $definition["nb_mots"] > 1) {
$nb_mots = $definition["nb_mots"]; $nb_mots = $definition["nb_mots"];
$nb_mots = " <small>($nb_mots mots)</small>"; $nb_mots = " <small>($nb_mots mots)</small>";
} else { } else {
$nb_mots = ""; $nb_mots = "";
} }
if (array_key_exists(AUTEUR, $definition)) { if (isset($definition["auteur"])) {
$auteur = $definition[AUTEUR]; $auteur = $definition["auteur"];
$auteur = " <small><em>$auteur</em></small>"; $auteur = " <small><em>$auteur</em></small>";
} else { } else {
$auteur = ""; $auteur = "";
} }
return ucfirst($definition[DEFINITION]) . $nb_mots . $auteur; return ucfirst($definition["definition"]) . $nb_mots . $auteur;
} }
function mot_courant($mots, $position) { function definition_courante($definitions, $position) {
foreach ($mots as $fin => $mot) { foreach ($definitions as $id => $definition) {
if ($position <= $fin) return $mot; if ($position <= $definition["fin"])
return [$id, $definition];
} }
return []; return [];
} }
function debuts($definitions) {
$debut = 0;
$retour = [];
foreach ($definitions as $fin => $definition) {
if ($fin - $debut > 1) {
$retour[$debut] = $definition;
}
$debut = $fin + 1;
}
return $retour;
}
?> ?>
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html lang="fr-FR" dir="ltr" prefix="og: https://ogp.me/ns#"> <html lang="fr-FR" dir="ltr" prefix="og: https://ogp.me/ns#">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>🄼🄾🅃🅂▣🄲🅁🄾🄸🅂🄴🅂</title> <title>🄼🄾🅃🅂▣🄲🅁🄾🄸🅂🄴🅂</title>
<link rel="stylesheet" href="style.css"> <link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="favicon.ico" /> <link rel="shortcut icon" href="favicon.ico" />
<link rel="icon" type="image/svg+xml" href="apercu.svg.php?grille=<?=$id?>&lignes=<?=$hauteur?>&colonnes=<?=$largeur?>"> <link rel="icon" type="image/svg+xml"
<link rel="icon" type="image/png" sizes="96x96" href="apercu.png.php?grille=<?=$id?>&lignes=<?=$hauteur?>&colonnes=<?=$largeur?>&largeur=96&hauteur=96" /> href="apercu.svg.php?grille=<?= $id ?>&lignes=<?= $hauteur ?>&colonnes=<?= $largeur ?>">
<link rel="apple-touch-icon" sizes="180x180" href="favicons/apple-touch-icon.png" /> <link rel="icon" type="image/png" sizes="96x96"
<meta name="apple-mobile-web-app-title" content="🄼🄾🅃🅂 🄲🅁🄾🄸🅂🄴🅂" /> href="apercu.png.php?grille=<?= $id ?>&lignes=<?= $hauteur ?>&colonnes=<?= $largeur ?>&largeur=96&hauteur=96" />
<link rel="manifest" href="site.webmanifest" /> <link rel="apple-touch-icon" sizes="180x180" href="favicons/apple-touch-icon.png" />
<meta property="og:title" content="🄼🄾🅃🅂🄲🅁🄾🄸🅂🄴🅂"/> <meta name="apple-mobile-web-app-title" content="🄼🄾🅃🅂 🄲🅁🄾🄸🅂🄴🅂" />
<meta property="og:type" content="game"/> <link rel="manifest" href="site.webmanifest" />
<meta property="og:url" content="<?=$basedir?>"/> <meta property="og:title" content="🄼🄾🅃🅂▣🄲🅁🄾🄸🅂🄴🅂" />
<meta property="og:image" content="<?=$basedir?>/apercu.png.php?grille=<?=$id?>&lignes=<?=$hauteur?>&colonnes=<?=$largeur?>&largeur=1200&hauteur=630"/> <meta property="og:type" content="game" />
<meta property="og:image:width" content="1200"/> <meta property="og:url" content="<?= $basedir ?>" />
<meta property="og:image:height" content="630"/> <meta property="og:image"
<meta property="og:locale" content="fr_FR"/> content="<?= $basedir ?>/apercu.png.php?grille=<?= $id ?>&lignes=<?= $hauteur ?>&colonnes=<?= $largeur ?>&largeur=1200&hauteur=630" />
<meta property="og:site_name" content="<?=$_SERVER["HTTP_HOST"]?>"/> <meta property="og:image:width" content="1200" />
</head> <meta property="og:image:height" content="630" />
<meta property="og:locale" content="fr_FR" />
<meta property="og:site_name" content="<?= $_SERVER["HTTP_HOST"] ?>" />
</head>
<body> <body>
<form id="grilleForm" method="get" location="."> <form id="grilleForm" method="get" location=".">
<h1 class="large width"> <h1 class="large width">
<a href="."> <a href=".">
<table> <table>
<tbody> <tbody>
<tr> <tr>
<td colspan="2"></td> <td colspan="2"></td>
<td>M</td> <td>M</td>
</tr> </tr>
<tr> <tr>
<td>c</td> <td>c</td>
<td>r</td> <td>r</td>
<td>o</td> <td>o</td>
<td>i</td> <td>i</td>
<td>s</td> <td>s</td>
<td>é</td> <td>é</td>
<td>s</td> <td>s</td>
</tr> </tr>
<tr> <tr>
<td colspan="2"></td> <td colspan="2"></td>
<td>t</td> <td>t</td>
</tr> </tr>
<tr> <tr>
<td colspan="2"></td> <td colspan="2"></td>
<td>s</td> <td>s</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
</a> </a>
</h1> </h1>
<h1 class="small width"><a href=".">Mots■croisés</a></h1> <h1 class="small width"><a href=".">Mots■croisés</a></h1>
<div class="grille-et-definitions"> <div class="grille-et-definitions">
<?php if ($grille_valide): ?> <?php if ($grille_valide): ?>
<div class="grille"> <div class="grille">
<table> <table>
<tr> <tr>
<th></th> <th></th>
<?php for ($x = 0; $x < $largeur; $x++): ?> <?php for ($x = 0; $x < $largeur; $x++): ?>
<th><?= chr($x + 65) ?></th> <th><?= chr($x + 65) ?></th>
<?php endfor; ?> <?php endfor; ?>
<th></th> <th></th>
</tr> </tr>
<?php for ($y = 0; $y < $hauteur; $y++): ?> <?php for ($y = 0; $y < $hauteur; $y++): ?>
<tr> <tr>
<th><?= $y + 1 ?></th> <th><?= $y + 1 ?></th>
<?php for ($x = 0; $x < $largeur; $x++): ?> <?php for ($x = 0; $x < $largeur; $x++): ?>
<?php if ($grille[$y][$x] == CASE_NOIRE): ?> <?php if ($grille[$y][$x] == CASE_NOIRE): ?>
<td class="case noire"> <td class="case noire">
<input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1" value="<?= CASE_NOIRE ?>" disabled /> <input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1"
</td> value="<?= CASE_NOIRE ?>" disabled />
<?php else: ?> </td>
<td class="case blanche"> <?php else: ?>
<?php <td class="case blanche">
$title = []; <?php
$definition_horizontale = mot_courant($grille->definitions["horizontales"][$y], $x); $title = [];
$definition_verticale = mot_courant($grille->definitions["verticales"][$x], $y); [$iddh, $definition_horizontale] = definition_courante($grille->definitions["horizontales"][$y], $x);
if (isset($definition_horizontale[0])) $title[0] = "→ " . $definition_horizontale[0]; [$iddv, $definition_verticale] = definition_courante($grille->definitions["verticales"][$x], $y);
if (isset($definition_horizontale[1])) $title[0] .= " (" . $definition_horizontale[1] . ")"; if (isset($definition_horizontale["definition"]))
if (isset($definition_verticale[0])) $title[1] = " " . $definition_verticale[0]; $title[0] = " " . $definition_horizontale["definition"];
if (isset($definition_verticale[1])) $title[1] .= " (" . $definition_verticale[1] . ")"; if (isset($definition_horizontale["nb_mots"]))
$title = htmlspecialchars(implode("\n", $title)); $title[0] .= " (" . $definition_horizontale["nb_mots"] . ")";
?> if (isset($definition_horizontale["auteur"]))
<input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1" pattern="[A-Z]" $title[0] .= " (" . $definition_horizontale["auteur"] . ")";
title="<?=$title?>" /> if (isset($definition_verticale["definition"]))
</td> $title[1] = "↓ " . $definition_verticale["definition"];
<?php endif; ?> if (isset($definition_verticale["nb_mots"]))
<?php endfor; ?> $title[0] .= " (" . $definition_verticale["nb_mots"] . ")";
</tr> if (isset($definition_verticale["auteur"]))
<?php endfor; ?> $title[1] .= " (" . $definition_verticale["auteur"] . ")";
$title = htmlspecialchars(implode("\n", $title));
?>
<input id="case-<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1"
pattern="[A-Z]" title="<?= $title ?>"
data-iddh="<?= isset($definition_horizontale["definition"])? "dh$y.$iddh" : "" ?>"
data-iddv="<?= isset($definition_verticale["definition"])? "dh$x.$iddv" : "" ?>"
/>
</td>
<?php endif; ?>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table> </table>
</div> </div>
<div class="definitions horizontales"> <div class="definitions horizontales">
<h2>Horizontalement</h2> <h2>Horizontalement</h2>
<ol type="1"> <ol type="1">
<?php <?php foreach ($grille->definitions["horizontales"] as $y => $definitions):
foreach ($grille->definitions["horizontales"] as $y => $definitions): $definitions = array_filter($definitions, function($definition) {
$definitions = debuts($definitions); return isset($definition["definition"]);
?> });
<li> ?>
<?php if (count($definitions)): ?> <li>
<?php if (count($definitions) == 1): ?> <?php if (count($definitions)): ?>
<label for="A<?=$y + 1?>"> <?php if (count($definitions) == 1): ?>
<?= formatter_definition(reset($definitions)) ?> <?php foreach ($definitions as $id => $definition): ?>
</label> <label id="<?= "dh$y.$id" ?>" for="case-1<?= $y + 1 ?>">
<?php else: ?> <?= formatter_definition($definition) ?>
<ol> </label>
<?php foreach ($definitions as $debut => $definition) : ?> <?php endforeach ?>
<label for="<?=chr($debut + 0x41)?><?=$y + 1?>"> <?php else: ?>
<li><?= formatter_definition($definition) ?></li> <ol>
</label> <?php foreach ($definitions as $id => $definition): ?>
<?php endforeach ?> <label id="<?= "dh$y.$id" ?>" for="case-<?= chr($definition["debut"] + 0x41) ?><?= $y + 1 ?>">
</ol> <li><?= formatter_definition($definition) ?></li>
<?php endif ?> </label>
<?php endif ?> <?php endforeach ?>
</li> </ol>
<?php endforeach; ?> <?php endif ?>
<?php endif ?>
</li>
<?php endforeach ?>
</ol> </ol>
</div> </div>
<div class="definitions verticales"> <div class="definitions verticales">
<h2>Verticalement</h2> <h2>Verticalement</h2>
<ol type="A"> <ol type="A">
<?php <?php foreach ($grille->definitions["verticales"] as $x => $definitions):
foreach ($grille->definitions["verticales"] as $x => $definitions): $definitions = array_filter($definitions, function($definition) {
$definitions = debuts($definitions); return isset($definition["definition"]);
?> });
<li> ?>
<?php if (count($definitions)): ?> <li>
<?php if (count($definitions) == 1): ?> <?php if (count($definitions)): ?>
<label for="<?=chr($x + 0x41)?>1"> <?php if (count($definitions) == 1): ?>
<?= formatter_definition(reset($definitions)) ?> <?php foreach ($definitions as $id => $definition): ?>
</label> <label id="<?= "dv$x.$id" ?>" for="case-<?= chr($x + 0x41) ?>1">
<?php else: ?> <?= formatter_definition($definition) ?>
<ol> </label>
<?php foreach ($definitions as $debut => $definition) : ?> <?php endforeach ?>
<label for="<?=chr($x + 0x41)?><?=$debut + 1?>"> <?php else: ?>
<li><?= formatter_definition($definition) ?></li> <ol>
</label> <?php foreach ($definitions as $id => $definition): ?>
<?php endforeach ?> <label id="<?= "dv$x.$id" ?>" for="case-<?= chr($x + 0x41) ?><?= $definition["debut"] + 1 ?>">
</ol> <li><?= formatter_definition($definition) ?></li>
<?php endif ?> </label>
<?php endif ?> <?php endforeach ?>
</li> </ol>
<?php endforeach; ?> <?php endif ?>
<?php endif ?>
</li>
<?php endforeach ?>
</ol> </ol>
</div> </div>
<input type="hidden" id="solution_hashee" value="<?= $grille->hash() ?>" /> <input type="hidden" id="solution_hashee" value="<?= $grille->hash() ?>" />
<?php else: http_response_code(500); ?> <?php else:
http_response_code(500); ?>
<h3 class="erreur">Erreur de génération de la grille</h3> <h3 class="erreur">Erreur de génération de la grille</h3>
<?php endif ?> <?php endif ?>
</div> </div>
<div class="nouvelle-grille">
<img src="favicons/favicon.svg" width="16" height="16">
<button type="submit">Nouvelle grille</button>
de
<input type="number" id="lignes"<?= isset($_GET["lignes"])? ' name="lignes"': "" ?> value="<?= $hauteur ?>" min="<?=HAUTEUR_MIN?>" max="<?=HAUTEUR_MAX?>"/>
lignes et
<input type="number" id="colonnes"<?= isset($_GET["colonnes"])? ' name="colonnes"': "" ?> value="<?= $largeur ?>" min="<?=LARGEUR_MIN?>" max="<?=LARGEUR_MAX?>"/>
colonnes
</div>
</form>
<script src="script.js"></script> <div class="nouvelle-grille">
<script>navigator?.serviceWorker.register('service-worker.js')</script> <img src="favicons/favicon.svg" width="16" height="16">
</body> <button type="submit">Nouvelle grille</button>
de
<input type="number" id="lignes" <?= isset($_GET["lignes"]) ? ' name="lignes"' : "" ?>
value="<?= $hauteur ?>" min="<?= HAUTEUR_MIN ?>" max="<?= HAUTEUR_MAX ?>" />
lignes et
<input type="number" id="colonnes" <?= isset($_GET["colonnes"]) ? ' name="colonnes"' : "" ?>
value="<?= $largeur ?>" min="<?= LARGEUR_MIN ?>" max="<?= LARGEUR_MAX ?>" />
colonnes
</div>
</form>
<script src="script.js"></script>
<script>navigator?.serviceWorker.register('service-worker.js')</script>
</body>
</html> </html>