Compare commits

..

5 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
9 changed files with 2956 additions and 4218 deletions

View File

@@ -2,13 +2,16 @@
include_once "dico.php"; include_once "dico.php";
const ECART_TYPE = 5.0; const ECART_TYPE_ALEA = 5;
$randmax = mt_getrandmax() + 1; $randmax = mt_getrandmax() + 1;
function gaussienne($moyenne = 0, $ecartType = 1.0): float { function gaussienne($moyenne = 0, $ecartType = 1.0): float {
global $randmax; global $randmax;
$u = 0;
$v = 0;
$u = (mt_rand() + 1) / $randmax; $u = (mt_rand() + 1) / $randmax;
$v = (mt_rand() + 1) / $randmax; $v = (mt_rand() + 1) / $randmax;
@@ -16,26 +19,6 @@ 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
{ {
@@ -99,30 +82,20 @@ class Grille implements ArrayAccess
"verticales" => [] "verticales" => []
]; ];
foreach($this->lignes as $y => $mots) { foreach($this->lignes as $y => $mots) {
$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[strlen($mot)][$mot];
if (count($definitions)) { if (count($definitions)) {
$definition = $definitions[mt_rand(0, count($definitions) - 1)]; $this->definitions["horizontales"][$y][] = $definitions[mt_rand(0, count($definitions) - 1)];
if (isset($definitions["nb_mots"])) {
$definition["nb_mots"] = $definitions["nb_mots"];
}
$this->definitions["horizontales"][$y][$fin] = $definition;
} }
} }
} }
foreach($this->colonnes as $x => $mots) { foreach($this->colonnes as $x => $mots) {
$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[strlen($mot)][$mot];
if (count($definitions)) { if (count($definitions)) {
$definition = $definitions[mt_rand(0, count($definitions) - 1)]; $this->definitions["verticales"][$x][] = $definitions[mt_rand(0, count($definitions) - 1)];
if (isset($definitions["nb_mots"])) {
$definition["nb_mots"] = $definitions["nb_mots"];
}
$this->definitions["verticales"][$x][$fin] = $definition;
} }
} }
} }
@@ -152,35 +125,25 @@ class Grille implements ArrayAccess
$lettres_ligne->branches, $lettres_ligne->branches,
$lettres_colonne->branches $lettres_colonne->branches
); );
// Sélection des candidats les plus fertiles
foreach ($lettres_communes as $lettre => $_) { foreach ($lettres_communes as $lettre => $_) {
$lettres_communes[$lettre] = log(count($lettres_ligne->branches[$lettre])) * count($lettres_colonne->branches[$lettre]) * gaussienne(ECART_TYPE, ECART_TYPE); $lettres_communes[$lettre] = count($lettres_ligne->branches[$lettre]) * count($lettres_colonne->branches[$lettre]) * gaussienne(1, ECART_TYPE_ALEA);
} }
uksort($lettres_communes, function($a, $b) use ($lettres_communes) { uksort($lettres_communes, function($a, $b) use ($lettres_communes) {
return $lettres_communes[$b] <=> $lettres_communes[$a]; return $lettres_communes[$b] <=> $lettres_communes[$a];
}); });
uksort($lettres_communes, function($a, $b) { $lettres_communes = array_slice($lettres_communes, 0, 3);
return ($a == CASE_NOIRE)? 1 : -1;
});
$lettres_communes = array_slice($lettres_communes, 0, 2);
foreach ($lettres_communes as $lettre => $_) { foreach ($lettres_communes as $lettre => $_) {
$this->grille[$y][$x] = $lettre; $this->grille[$y][$x] = $lettre;
// Omission des lettres isolées // Omission des lettres isolées
if ($lettre == CASE_NOIRE) { if ($lettre == CASE_NOIRE
if (($y < 2 || $this->grille[$y - 2][$x] == CASE_NOIRE) && ($y - 2 < 0 || $this->grille[$y - 2][$x] == CASE_NOIRE)
&& ($y < 1 || $x == 0 || $this->grille[$y - 1][$x - 1] == CASE_NOIRE) && ($y - 1 < 0 || $x - 1 < 0 || $this->grille[$y - 1][$x - 1] == CASE_NOIRE)
&& ($y < 1 || $x + 1 >= $this->largeur || $this->grille[$y - 1][$x + 1] == CASE_NOIRE) && ($y - 1 < 0 || $x + 1 >= $this->largeur || $this->grille[$y - 1][$x + 1] == CASE_NOIRE)
) continue; ) {
if ($y == $this->hauteur - 1 continue;
&& ($x < 2 || $this[$y][$x - 2] == CASE_NOIRE) }
&& ($x < 1 || $this[$y - 1][$x - 1] == CASE_NOIRE)
) continue;
} else if ($x == $this->largeur - 1 && $y == $this->hauteur - 1
&& $this[$y][$x - 1] == CASE_NOIRE
&& $this[$y-1][$x] == CASE_NOIRE
) continue;
// Omission des doublons // Omission des doublons
$mots = []; $mots = [];

View File

@@ -1,27 +1,23 @@
<?php <?php
class Trie implements ArrayAccess, IteratorAggregate, Countable class Trie implements ArrayAccess, IteratorAggregate, Countable {
{
public array $branches = []; public array $branches = [];
private $nb_branches = 0; private $nb_branches = 0;
public function arraySet($cles, $valeur) public function arraySet($cles, $valeur) {
{
$cle = $cles[0]; $cle = $cles[0];
$this->nb_branches++; $this->nb_branches++;
$cles = array_slice($cles, 1); $cles = array_slice($cles, 1);
if ($cles == []) { if ($cles == []) {
$this->branches[$cle] = $valeur; $this->branches[$cle] = $valeur;
} else { } else {
if (!isset($this->branches[$cle])) if (!isset($this->branches[$cle])) $this->branches[$cle] = new Trie();
$this->branches[$cle] = new Trie();
$this->branches[$cle]->arraySet($cles, $valeur); $this->branches[$cle]->arraySet($cles, $valeur);
} }
} }
public function arrayExists($cles) public function arrayExists($cles) {
{
$cle = $cles[0]; $cle = $cles[0];
$cles = array_slice($cles, 1); $cles = array_slice($cles, 1);
if ($cles == []) { if ($cles == []) {
@@ -31,8 +27,7 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable
} }
} }
public function &arrayGet($cles) public function &arrayGet($cles) {
{
$cle = $cles[0]; $cle = $cles[0];
$cles = array_slice($cles, 1); $cles = array_slice($cles, 1);
if ($cles == []) { if ($cles == []) {
@@ -42,8 +37,7 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable
} }
} }
public function arrayUnset($cles) public function arrayUnset($cles) {
{
$cle = $cles[0]; $cle = $cles[0];
$cles = array_slice($cles, 1); $cles = array_slice($cles, 1);
if ($cles == []) { if ($cles == []) {
@@ -58,45 +52,10 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable
} }
} }
// ArrayAccess public function arrayIterator() {
public function offsetSet($array, $valeur): void
{
if (is_string($array)) {
$array = str_split($array);
}
$this->arraySet($array, $valeur);
}
public function offsetExists($array): bool
{
if (is_string($array)) {
$array = str_split($array);
}
return $this->arrayExists($array);
}
public function &offsetGet($array): mixed
{
if (is_string($array)) {
$array = str_split($array);
}
return $this->arrayGet($array);
}
public function offsetUnset($array): void
{
if (is_string($array)) {
$array = str_split($array);
}
$this->arrayUnset($array);
}
// IteratorAggregate
public function getIterator(): Traversable
{
foreach ($this->branches as $cle => $branche) { foreach ($this->branches as $cle => $branche) {
if ($branche instanceof Trie) { if ($branche instanceof Trie) {
foreach ($branche->getIterator() as $sous_cles => $feuille) { foreach($branche->arrayIterator() as $sous_cles => $feuille) {
yield array_merge([$cle], $sous_cles) => $feuille; yield array_merge([$cle], $sous_cles) => $feuille;
} }
} else { } else {
@@ -105,9 +64,32 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable
} }
} }
// ArrayAccess
public function offsetSet($string, $valeur): void {
$this->arraySet(str_split($string), $valeur);
}
public function offsetExists($string): bool {
return $this->arrayExists(str_split($string));
}
public function &offsetGet($string): mixed {
return $this->arrayGet(str_split($string));
}
public function offsetUnset($string): void {
$this->arrayUnset(str_split($string));
}
// IteratorAggregate
public function getIterator(): Traversable {
foreach($this->arrayIterator() as $array => $valeur) {
yield implode("", $array) => $valeur;
}
}
// Countable // Countable
public function count(): int public function count(): int {
{
return $this->nb_branches; return $this->nb_branches;
} }
} }

2818
dico.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,21 +1,18 @@
<?php <?php
include_once "Trie.php"; include_once "Trie.php";
const CASE_NOIRE = " "; const CASE_NOIRE = " ";
const DEFINITION = 0;
const AUTEUR = 1;
function dico($longueur_max) { function dico($longueur_max) {
$transliterator = Transliterator::createFromRules(':: Any-Latin; :: Latin-ASCII; :: NFD; :: [:Nonspacing Mark:] Remove; :: Upper(); :: NFC;', Transliterator::FORWARD); $transliterator = Transliterator::createFromRules(':: Any-Latin; :: Latin-ASCII; :: NFD; :: [:Nonspacing Mark:] Remove; :: Upper(); :: NFC;', Transliterator::FORWARD);
$dico = []; $dico = [[new Trie()]];
for ($longueur = 0; $longueur <= $longueur_max; $longueur++) { for ($longueur = 0; $longueur <= $longueur_max; $longueur++) {
$dico[] = new Trie(); $dico[] = new Trie();
} }
if (($lecteur = fopen("dico.tsv", "r")) !== FALSE) { if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
$entete = fgetcsv($lecteur, 0, "\t"); $entete = fgetcsv($lecteur, 0, "\t");
while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) { while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) {
if ( if (
@@ -25,7 +22,7 @@ function dico($longueur_max) {
) continue; ) continue;
$mot = $ligne[0]; $mot = $ligne[0];
$definition = array_slice($ligne, 1); $definitions = array_slice($ligne, 1);
$mot = str_replace("-", CASE_NOIRE, $mot); $mot = str_replace("-", CASE_NOIRE, $mot);
$mot = $transliterator->transliterate($mot); $mot = $transliterator->transliterate($mot);
@@ -33,16 +30,12 @@ function dico($longueur_max) {
$mots = explode(CASE_NOIRE, $mot); $mots = explode(CASE_NOIRE, $mot);
$nb_mots = count($mots); $nb_mots = count($mots);
$mot = implode("", $mots); $mot = implode("", $mots);
} else { foreach($definitions as $i => $definition) {
$nb_mots = 1; $definitions[$i] = "$definition#$nb_mots";
}
} }
if (array_key_exists($mot, $dico)) { $dico[strlen($mot)][$mot] = $definitions;
$dico[strlen($mot)][$mot][] = $definition;
} else {
$dico[strlen($mot)][$mot] = [$definition];
if ($nb_mots > 1) $dico[strlen($mot)][$mot]["nb_mots"] = $nb_mots;
}
} }
fclose($lecteur); fclose($lecteur);
} }
@@ -50,14 +43,14 @@ function dico($longueur_max) {
return $dico; return $dico;
} }
function mots_espaces($longueur_max, $longueur_min=1) { function mots_espaces($longueur_max) {
$dico = dico($longueur_max); $dico = dico($longueur_max);
for ($longueur = $longueur_max; $longueur >= $longueur_min + 1; $longueur--) { for ($longueur = 1; $longueur <= $longueur_max; $longueur++) {
for ($position_espace = $longueur - $longueur_min - 1; $position_espace >= $longueur_min; $position_espace--) { for ($position_espace = 1; $position_espace + 1 < $longueur; $position_espace++) {
$mots_suivants = $dico[$longueur - $position_espace - 1]; $mots_suivants = $dico[$longueur - $position_espace - 1];
foreach ($dico[$position_espace] as $premier_mot => $definition) { foreach ($dico[$position_espace]->arrayIterator() as $premier_mot => $definition) {
$premier_mot[] = CASE_NOIRE; $premier_mot[] = CASE_NOIRE;
$dico[$longueur][$premier_mot] = $mots_suivants; $dico[$longueur]->arraySet($premier_mot, $mots_suivants);
} }
} }
} }

3995
dico.tsv

File diff suppressed because it is too large Load Diff

View File

@@ -1,13 +1,11 @@
<?php <?php
include_once "Grille.php"; include_once "Grille.php";
const HAUTEUR_DEFAUT = 7; const HAUTEUR_DEFAUT = 8;
const HAUTEUR_MIN = 2; const HAUTEUR_MIN = 2;
const HAUTEUR_MAX = 10; const HAUTEUR_MAX = 10;
const LARGEUR_DEFAUT = 7; const LARGEUR_DEFAUT = 8;
const LARGEUR_MIN = 2; const LARGEUR_MIN = 2;
const LARGEUR_MAX = 10; const LARGEUR_MAX = 10;
@@ -45,26 +43,19 @@ if (!isset($_GET["grille"]) || $_GET["grille"] == "") {
} }
function formatter_definition($definition) { function formatter_definition($definition) {
if (isset($definition["nb_mots"]) && $definition["nb_mots"] > 1){ if (strpos($definition, "#") !== false) {
$nb_mots = $definition["nb_mots"]; [$definition, $nb_mots] = explode("#", $definition);
$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 (strpos($definition, "@") !== false) {
$auteur = $definition[AUTEUR]; [$definition, $auteur] = explode("@", $definition);
$auteur = " <small><em>$auteur</em></small>"; $auteur = " <small><em>$auteur</em></small>";
} else { } else {
$auteur = ""; $auteur = "";
} }
return ucfirst($definition[DEFINITION]) . $nb_mots . $auteur; return $definition;
}
function mot_courant($mots, $position) {
foreach ($mots as $fin => $mot) {
if ($position <= $fin) return $mot;
}
return [];
} }
?> ?>
<!DOCTYPE HTML> <!DOCTYPE HTML>
@@ -94,7 +85,6 @@ function mot_courant($mots, $position) {
<body> <body>
<form id="grilleForm" method="get" location="."> <form id="grilleForm" method="get" location=".">
<h1 class="large width"> <h1 class="large width">
<a href=".">
<table> <table>
<tbody> <tbody>
<tr> <tr>
@@ -120,9 +110,8 @@ function mot_courant($mots, $position) {
</tr> </tr>
</tbody> </tbody>
</table> </table>
</a>
</h1> </h1>
<h1 class="small width"><a href=".">Mots■croisés</a></h1> <h1 class="small width">Mots■croisés</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">
@@ -144,18 +133,8 @@ function mot_courant($mots, $position) {
</td> </td>
<?php else: ?> <?php else: ?>
<td class="case blanche"> <td class="case blanche">
<?php <input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1" pattern="[A-Z]" placeholder="<?= $grille[$y][$x] ?>"
$title = []; title="<?= strip_tags("→ " . implode("\n→ ", array_map("formatter_definition", $grille->definitions["horizontales"][$y] ?? [])) . "\n↓ " . implode("\n↓ ", array_map("formatter_definition", $grille->definitions["verticales"][$x] ?? []))) ?>" />
$definition_horizontale = mot_courant($grille->definitions["horizontales"][$y], $x);
$definition_verticale = mot_courant($grille->definitions["verticales"][$x], $y);
if (isset($definition_horizontale[0])) $title[0] = "→ " . $definition_horizontale[0];
if (isset($definition_horizontale[1])) $title[0] .= " (" . $definition_horizontale[1] . ")";
if (isset($definition_verticale[0])) $title[1] = "↓ " . $definition_verticale[0];
if (isset($definition_verticale[1])) $title[1] .= " (" . $definition_verticale[1] . ")";
$title = htmlspecialchars(implode("\n", $title));
?>
<input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1" pattern="[A-Z]"
title="<?=$title?>" />
</td> </td>
<?php endif; ?> <?php endif; ?>
<?php endfor; ?> <?php endfor; ?>
@@ -170,13 +149,11 @@ function mot_courant($mots, $position) {
<li> <li>
<?php if (count($definitions)): ?> <?php if (count($definitions)): ?>
<?php if (count($definitions) == 1): ?> <?php if (count($definitions) == 1): ?>
<?= formatter_definition(reset($definitions)) ?> <?= formatter_definition($definitions[0]) ?>
<?php else: ?> <?php else: ?>
<ol> <ol>
<?php foreach ($definitions as $definition) : ?> <?php foreach ($definitions as $definition) : ?>
<?php if (isset($definition[0])): ?>
<li><?= formatter_definition($definition) ?></li> <li><?= formatter_definition($definition) ?></li>
<?php endif ?>
<?php endforeach ?> <?php endforeach ?>
</ol> </ol>
<?php endif ?> <?php endif ?>
@@ -192,13 +169,11 @@ function mot_courant($mots, $position) {
<li> <li>
<?php if (count($definitions)): ?> <?php if (count($definitions)): ?>
<?php if (count($definitions) == 1): ?> <?php if (count($definitions) == 1): ?>
<?= formatter_definition(reset($definitions)) ?> <?= formatter_definition($definitions[0]) ?>
<?php else: ?> <?php else: ?>
<ol> <ol>
<?php foreach ($definitions as $definition) : ?> <?php foreach ($definitions as $definition) : ?>
<?php if (isset($definition[0])): ?>
<li><?= formatter_definition($definition) ?></li> <li><?= formatter_definition($definition) ?></li>
<?php endif ?>
<?php endforeach ?> <?php endforeach ?>
</ol> </ol>
<?php endif ?> <?php endif ?>

View File

@@ -35,28 +35,26 @@ for (let input of inputs) {
}; };
input.onkeydown = function (event) { input.onkeydown = function (event) {
next_input = input; next_input = null;
do {
switch (event.key) { switch (event.key) {
case "ArrowUp": case "ArrowUp":
next_input = inputs[(next_input.index - largeur + nb_cases) % nb_cases]; next_input = inputs[(input.index - largeur + nb_cases) % nb_cases];
break; break;
case "ArrowDown": case "ArrowDown":
next_input = inputs[(next_input.index + largeur) % nb_cases]; next_input = inputs[(input.index + largeur) % nb_cases];
break; break;
case "ArrowLeft": case "ArrowLeft":
next_input = inputs[(next_input.index - 1 + nb_cases) % nb_cases]; next_input = inputs[(input.index - 1 + nb_cases) % nb_cases];
break; break;
case "ArrowRight": case "ArrowRight":
next_input = inputs[(next_input.index + 1) % nb_cases]; next_input = inputs[(input.index + 1) % nb_cases];
break; break;
default:
return;
} }
} while (next_input.disabled) if (next_input) {
event.preventDefault();
next_input.focus(); next_input.focus();
next_input.select(); next_input.select();
event.preventDefault();
}
}; };
input.oninput = function (event) { input.oninput = function (event) {

View File

@@ -18,12 +18,6 @@ h1 {
letter-spacing: 0.2em; letter-spacing: 0.2em;
} }
h1 a {
width: 100%;
text-decoration: none;
color: inherit;
}
h1.large.width { h1.large.width {
display: inherit; display: inherit;
padding-top: 1rem; padding-top: 1rem;
@@ -205,7 +199,6 @@ h2 {
text-decoration: underline; text-decoration: underline;
} }
h1 a:hover,
.nouvelle-grille button:hover { .nouvelle-grille button:hover {
color: #2a6496; color: #2a6496;
} }
@@ -322,7 +315,6 @@ h1 a:hover,
background-color: #edeeee; background-color: #edeeee;
} }
h1 a:hover,
.nouvelle-grille button:hover { .nouvelle-grille button:hover {
color: #479fec; color: #479fec;
} }

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" }
]
}