n'afficher que le mot courant dans l'infobulle
This commit is contained in:
34
Grille.php
34
Grille.php
@@ -16,6 +16,26 @@ function gaussienne($moyenne = 0, $ecartType = 1.0): float {
|
||||
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
|
||||
{
|
||||
@@ -80,27 +100,27 @@ class Grille implements ArrayAccess
|
||||
];
|
||||
foreach($this->lignes as $y => $mots) {
|
||||
$this->definitions["horizontales"][$y] = [];
|
||||
foreach($mots as $mot) {
|
||||
foreach($mots as $fin => $mot) {
|
||||
$definitions = $this->dico[strlen($mot)][$mot];
|
||||
if (count($definitions)) {
|
||||
$definition = $definitions[mt_rand(0, count($definitions) - 1)];
|
||||
if (isset($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) {
|
||||
$this->definitions["verticales"][$x] = [];
|
||||
foreach($mots as $mot) {
|
||||
foreach($mots as $fin => $mot) {
|
||||
$definitions = $this->dico[strlen($mot)][$mot];
|
||||
if (count($definitions)) {
|
||||
$definition = $definitions[mt_rand(0, count($definitions) - 1)];
|
||||
if (isset($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
|
||||
$mots = [];
|
||||
if ($x == $this->largeur - 1) $mots = explode(CASE_NOIRE, $this->get_ligne($y, $this->largeur));
|
||||
else if ($lettre == CASE_NOIRE) $mots = explode(CASE_NOIRE, $this->get_ligne($y, $x));
|
||||
if ($x == $this->largeur - 1) $mots = explode_pos(CASE_NOIRE, $this->get_ligne($y, $this->largeur));
|
||||
else if ($lettre == CASE_NOIRE) $mots = explode_pos(CASE_NOIRE, $this->get_ligne($y, $x));
|
||||
else $mots = [];
|
||||
$this->lignes[$y] = array_filter($mots, function ($mot) {
|
||||
return strlen($mot) >= 2;
|
||||
@@ -174,7 +194,7 @@ class Grille implements ArrayAccess
|
||||
}
|
||||
|
||||
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) {
|
||||
if (strlen($mot) < 2) continue;
|
||||
if (strlen($mot > 2) && in_array($mot, array_merge(...$this->lignes, ...$this->colonnes))) continue 2;
|
||||
|
||||
23
index.php
23
index.php
@@ -59,6 +59,13 @@ function formatter_definition($definition) {
|
||||
}
|
||||
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>
|
||||
<html lang="fr-FR" dir="ltr" prefix="og: https://ogp.me/ns#">
|
||||
@@ -137,8 +144,18 @@ function formatter_definition($definition) {
|
||||
</td>
|
||||
<?php else: ?>
|
||||
<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]"
|
||||
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>
|
||||
<?php endif; ?>
|
||||
<?php endfor; ?>
|
||||
@@ -153,7 +170,7 @@ function formatter_definition($definition) {
|
||||
<li>
|
||||
<?php if (count($definitions)): ?>
|
||||
<?php if (count($definitions) == 1): ?>
|
||||
<?= formatter_definition($definitions[0]) ?>
|
||||
<?= formatter_definition(reset($definitions)) ?>
|
||||
<?php else: ?>
|
||||
<ol>
|
||||
<?php foreach ($definitions as $definition) : ?>
|
||||
@@ -173,7 +190,7 @@ function formatter_definition($definition) {
|
||||
<li>
|
||||
<?php if (count($definitions)): ?>
|
||||
<?php if (count($definitions) == 1): ?>
|
||||
<?= formatter_definition($definitions[0]) ?>
|
||||
<?= formatter_definition(reset($definitions)) ?>
|
||||
<?php else: ?>
|
||||
<ol>
|
||||
<?php foreach ($definitions as $definition) : ?>
|
||||
|
||||
Reference in New Issue
Block a user