n'afficher que le mot courant dans l'infobulle

This commit is contained in:
2025-12-04 18:09:01 +01:00
parent 329d207930
commit 4a51ed72eb
2 changed files with 47 additions and 10 deletions

View File

@@ -16,6 +16,26 @@ function gaussienne($moyenne = 0, $ecartType = 1.0): float {
return $z * $ecartType + $moyenne; return $z * $ecartType + $moyenne;
} }
function explode_pos(string $separator, string $string): array {
$mots = [];
$mot = "";
$longueur = strlen($string);
for ($i = 0; $i < $longueur; $i++) {
$lettre = $string[$i];
if ($lettre == $separator) {
$mots[$i] = $mot;
$mot = "";
} else {
$mot .= $lettre;
}
}
$mots[$i] = $mot;
return $mots;
}
class Grille implements ArrayAccess class Grille implements ArrayAccess
{ {
@@ -80,27 +100,27 @@ class Grille implements ArrayAccess
]; ];
foreach($this->lignes as $y => $mots) { foreach($this->lignes as $y => $mots) {
$this->definitions["horizontales"][$y] = []; $this->definitions["horizontales"][$y] = [];
foreach($mots as $mot) { foreach($mots as $fin => $mot) {
$definitions = $this->dico[strlen($mot)][$mot]; $definitions = $this->dico[strlen($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($definitions["nb_mots"])) {
$definition["nb_mots"] = $definitions["nb_mots"]; $definition["nb_mots"] = $definitions["nb_mots"];
} }
$this->definitions["horizontales"][$y][] = $definition; $this->definitions["horizontales"][$y][$fin] = $definition;
} }
} }
} }
foreach($this->colonnes as $x => $mots) { foreach($this->colonnes as $x => $mots) {
$this->definitions["verticales"][$x] = []; $this->definitions["verticales"][$x] = [];
foreach($mots as $mot) { foreach($mots as $fin => $mot) {
$definitions = $this->dico[strlen($mot)][$mot]; $definitions = $this->dico[strlen($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($definitions["nb_mots"])) {
$definition["nb_mots"] = $definitions["nb_mots"]; $definition["nb_mots"] = $definitions["nb_mots"];
} }
$this->definitions["verticales"][$x][] = $definition; $this->definitions["verticales"][$x][$fin] = $definition;
} }
} }
} }
@@ -161,8 +181,8 @@ class Grille implements ArrayAccess
// Omission des doublons // Omission des doublons
$mots = []; $mots = [];
if ($x == $this->largeur - 1) $mots = explode(CASE_NOIRE, $this->get_ligne($y, $this->largeur)); if ($x == $this->largeur - 1) $mots = explode_pos(CASE_NOIRE, $this->get_ligne($y, $this->largeur));
else if ($lettre == CASE_NOIRE) $mots = explode(CASE_NOIRE, $this->get_ligne($y, $x)); else if ($lettre == CASE_NOIRE) $mots = explode_pos(CASE_NOIRE, $this->get_ligne($y, $x));
else $mots = []; else $mots = [];
$this->lignes[$y] = array_filter($mots, function ($mot) { $this->lignes[$y] = array_filter($mots, function ($mot) {
return strlen($mot) >= 2; return strlen($mot) >= 2;
@@ -174,7 +194,7 @@ class Grille implements ArrayAccess
} }
if ($y == $this->hauteur - 1) { if ($y == $this->hauteur - 1) {
$mots = explode(CASE_NOIRE, $this->get_colonne($x, $this->hauteur)); $mots = explode_pos(CASE_NOIRE, $this->get_colonne($x, $this->hauteur));
foreach ($mots as $rang => $mot) { foreach ($mots as $rang => $mot) {
if (strlen($mot) < 2) continue; if (strlen($mot) < 2) continue;
if (strlen($mot > 2) && in_array($mot, array_merge(...$this->lignes, ...$this->colonnes))) continue 2; if (strlen($mot > 2) && in_array($mot, array_merge(...$this->lignes, ...$this->colonnes))) continue 2;

View File

@@ -59,6 +59,13 @@ function formatter_definition($definition) {
} }
return ucfirst($definition[DEFINITION]) . $nb_mots . $auteur; return ucfirst($definition[DEFINITION]) . $nb_mots . $auteur;
} }
function mot_courant($mots, $position) {
foreach ($mots as $fin => $mot) {
if ($position <= $fin) return $mot;
}
return [];
}
?> ?>
<!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#">
@@ -137,8 +144,18 @@ function formatter_definition($definition) {
</td> </td>
<?php else: ?> <?php else: ?>
<td class="case blanche"> <td class="case blanche">
<?php
$title = "";
$definition_horizontale = mot_courant($grille->definitions["horizontales"][$y], $x);
$definition_verticale = mot_courant($grille->definitions["verticales"][$x], $y);
if (isset($definition_horizontale[0])) $title .= "→ " . $definition_horizontale[0];
if (isset($definition_horizontale[1])) $title .= " (" . $definition_horizontale[1] . ")";
if (isset($definition_verticale[0])) $title .= "\n ↓ " . $definition_verticale[0];
if (isset($definition_verticale[1])) $title .= " (" . $definition_verticale[1] . ")";
$title = htmlspecialchars($title);
?>
<input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1" pattern="[A-Z]" <input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1" pattern="[A-Z]"
title="<?= strip_tags("→ " . implode("\n→ ", array_map("formatter_definition", $grille->definitions["horizontales"][$y] ?? [])) . "\n↓ " . implode("\n↓ ", array_map("formatter_definition", $grille->definitions["verticales"][$x] ?? []))) ?>" /> title="<?=$title?>" />
</td> </td>
<?php endif; ?> <?php endif; ?>
<?php endfor; ?> <?php endfor; ?>
@@ -153,7 +170,7 @@ function formatter_definition($definition) {
<li> <li>
<?php if (count($definitions)): ?> <?php if (count($definitions)): ?>
<?php if (count($definitions) == 1): ?> <?php if (count($definitions) == 1): ?>
<?= formatter_definition($definitions[0]) ?> <?= formatter_definition(reset($definitions)) ?>
<?php else: ?> <?php else: ?>
<ol> <ol>
<?php foreach ($definitions as $definition) : ?> <?php foreach ($definitions as $definition) : ?>
@@ -173,7 +190,7 @@ function formatter_definition($definition) {
<li> <li>
<?php if (count($definitions)): ?> <?php if (count($definitions)): ?>
<?php if (count($definitions) == 1): ?> <?php if (count($definitions) == 1): ?>
<?= formatter_definition($definitions[0]) ?> <?= formatter_definition(reset($definitions)) ?>
<?php else: ?> <?php else: ?>
<ol> <ol>
<?php foreach ($definitions as $definition) : ?> <?php foreach ($definitions as $definition) : ?>