Compare commits

..

59 Commits

Author SHA1 Message Date
13fcdf05fb grand dico 2025-05-08 01:47:58 +02:00
ca39709fbd nombre de mots 2025-05-07 23:54:00 +02:00
ddebd453df interface trie avec des strings 2025-05-07 21:21:48 +02:00
d5a120cd9a omettre les mots plus grands que la grille 2025-05-07 19:38:04 +02:00
ab9e1f08ef Tries everywhere! 2025-05-07 18:34:08 +02:00
b1f3e8b85f omission des lettres isolées 2025-05-07 00:16:55 +02:00
c20d2324e9 FAST & FURIOUS 2025-05-06 17:44:16 +02:00
65bb8be2e3 Revert "Merge branch 'tmp3'"
This reverts commit ed1ad0566f, reversing
changes made to 6ee909914d.
2025-05-06 17:29:51 +02:00
ed1ad0566f Merge branch 'tmp3' 2025-05-06 17:27:47 +02:00
47be3d2e51 FAST & FURIOUS 2025-05-06 16:56:59 +02:00
6ee909914d WIP 2025-05-06 15:55:58 +02:00
4c014bc789 variable inutilisée 2025-05-06 08:47:10 +02:00
b063d39689 Merge branch 'tmp2' 2025-05-06 02:43:01 +02:00
d150e76a9e fix id vide 2025-05-06 02:38:18 +02:00
a6b15fbb87 fix 2025-05-06 02:15:13 +02:00
afd79eb3a2 utiliser session_id comme id 2025-05-06 01:40:53 +02:00
ea4555144c sans cookies 2025-05-05 22:41:55 +02:00
1a35d8e920 sauvegarde 2025-05-05 20:02:56 +02:00
7ae6506539 WIP 2025-05-05 16:57:29 +02:00
f8bd1d35cd TRIE 2025-05-05 02:49:57 +02:00
23cac370a1 conserver la numérotation 2025-05-04 13:57:08 +02:00
dc488aea4d renvoyer un numéro de grille seulement si elle est valide 2025-05-03 14:48:54 +02:00
91db11cc2d permutations 2025-05-03 14:14:56 +02:00
4a19252a94 petites modifs 2025-05-03 12:59:29 +02:00
ca95b76558 ne vérifier les doublons que sur le dernier mot de la ligne 2025-05-03 12:43:11 +02:00
97b9766db3 optimisation 2025-05-03 02:40:38 +02:00
168fc5f7a2 moins de comparaisons 2025-05-03 02:36:46 +02:00
0dd9881f6d correction OFFLINE_URL 2025-05-03 02:25:55 +02:00
32db8f5a6c 2025-05-03 02:21:25 +02:00
9f3a90a04e 2025-05-03 02:09:16 +02:00
73e8d6a857 ça marche 2025-05-03 01:18:33 +02:00
7a245d601a suppression melanger_valeurs 2025-05-02 16:23:39 +02:00
e1eb7685e4 suppression des points quand une seule phrase 2025-05-02 16:23:29 +02:00
10ea7a7f0b fix 2025-05-02 15:15:47 +02:00
63cd243e91 Merge branch 'tmp' 2025-05-02 11:55:23 +02:00
c097be27d8 style 2025-05-02 11:50:08 +02:00
399370624c 7 2025-05-02 04:32:32 +02:00
911da7aef9 ajouts 2025-05-02 04:30:46 +02:00
1987905eb9 annulation de l'autorisation des doublons 2025-05-02 04:08:38 +02:00
2214cf4a05 épurage 2025-05-02 03:55:38 +02:00
5997397908 retour de la limite de mots 2025-05-02 03:34:07 +02:00
b85bf7eb99 corrections 2025-05-02 03:27:01 +02:00
4a1106fcba autoriser autant de doublons que de définitions,
suppression de la limite de mots
2025-05-02 03:06:18 +02:00
d867e40499 mots_espace renvoie une liste de mots 2025-05-02 00:47:00 +02:00
d852596386 élimination des doublons 2025-05-02 00:00:59 +02:00
38344a315c Eviter les doublons lorsqu'il y a plusieurs mots en ligne 2025-05-01 23:25:21 +02:00
b042fcd77a ajout termes informatiques 2025-05-01 22:34:44 +02:00
912b4c410d changement de structure du dictionnaire 2025-05-01 20:58:08 +02:00
fcee18ee04 Nouvelles définitions 2025-05-01 20:13:36 +02:00
1073cd53fc ajout Wikipourri 2025-05-01 19:05:18 +02:00
fa134b8102 suppression du Dictionnaire des idées reçues 2025-05-01 18:52:54 +02:00
6b6edd369b petites modifs 2025-05-01 18:21:44 +02:00
db9b79fb6d correction limite 2025-05-01 17:47:46 +02:00
4874bc0d78 limite nombre de mots 2025-05-01 17:40:35 +02:00
6db7e7ab63 fusion de dico et mot_de_n_lettres, favicon en svg 2025-05-01 17:11:32 +02:00
9546ac4d97 changer le nombre de lignes et de colonnes 2025-05-01 15:16:45 +02:00
351e8387bb publier les mots entiers en ligne et colonne 2025-05-01 14:23:27 +02:00
c8ecb504ef n'afficher que les définitions de la case 2025-05-01 14:06:44 +02:00
92306e2a2a flèches en infobulle 2025-04-30 15:51:16 +02:00
10 changed files with 974471 additions and 4137 deletions

3
.gitignore vendored
View File

@ -1,2 +1,5 @@
__pycache__/
test*.*
*.py
dico2.csv
wiktionaryXfr2010.7z

View File

@ -1,169 +1,228 @@
<?php
include_once "dico.php";
function melanger_cles($tableau)
{
uksort($tableau, function ($a, $b) {
return mt_rand(-1, 1);
});
return $tableau;
$randmax = mt_getrandmax() + 1;
function gaussienne($moyenne = 0, $ecartType = 1.0): float {
global $randmax;
$u = 0;
$v = 0;
$u = (mt_rand() + 1) / $randmax;
$v = (mt_rand() + 1) / $randmax;
$z = sqrt(-2.0 * log($u)) * cos(2.0 * M_PI * $v);
return $z * $ecartType + $moyenne;
}
class Grille implements Iterator, ArrayAccess {
class Grille implements ArrayAccess
{
public $grille;
public $hauteur;
public $largeur;
private $grilles;
private $lettres_suivantes;
public $dico;
private $positions;
private $nb_positions;
private $mots_utilises;
public $lignes = [];
public $colonnes = [];
public $valide = false;
private $id;
public function __construct($hauteur, $largeur, $id = "")
public function __construct($hauteur, $largeur)
{
$this->hauteur = $hauteur;
$this->largeur = $largeur;
$this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, ''));
$this->lettres_suivantes = [];
foreach ($hauteur == $largeur ? [$hauteur] : [$hauteur, $largeur] as $longueur) {
$this->lettres_suivantes[$longueur] = [];
foreach (mots_espaces($longueur) as $mot) {
$ref = &$this->lettres_suivantes[$longueur];
for ($i = 0; $i < $longueur; $i++) {
$lettre = $mot[$i];
if (!isset($ref[$lettre])) {
$ref[$lettre] = [];
}
$ref = &$ref[$lettre];
}
$ref = [];
}
}
$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++) {
for ($x = 0; $x < $largeur; $x++)
$this->positions[] = [$x, $y];
}
}
$this->nb_positions = count($this->positions);
mt_srand($id == "" ? null : crc32($id));
$this->mots_utilises = [];
$this->grilles = $this->generateur();
$this->dico = mots_espaces(max($hauteur, $largeur));
}
public function get_ligne($y, $largeur)
{
$ligne = "";
for ($x = 0; $x < $largeur; $x++) {
for ($x = 0; $x < $largeur; $x++)
$ligne .= $this->grille[$y][$x];
}
return $ligne;
}
public function get_colonne($x, $hauteur)
{
$colonne = "";
for ($y = 0; $y < $hauteur; $y++) {
for ($y = 0; $y < $hauteur; $y++)
$colonne .= $this->grille[$y][$x];
}
return $colonne;
}
public function generateur($i = 0)
public function gen_grilles($i = 0, $lettres_ligne = NULL)
{
if ($i == $this->nb_positions) {
yield $this;
return;
}
[$x, $y] = $this->positions[$i];
$lettres_suivantes_ligne = $this->lettres_suivantes[$this->largeur];
for ($x2 = 0; $x2 < $x; $x2++) {
$lettres_suivantes_ligne = $lettres_suivantes_ligne[$this->grille[$y][$x2]];
// Recherche de la prochaine lettre possible sur la case courante
// en ligne
if ($x == 0) {
$lettres_ligne = $this->dico[$this->largeur];
}
$lettres_suivantes_colonne = $this->lettres_suivantes[$this->hauteur];
// en colonne
$lettres_colonne = $this->dico[$this->hauteur];
for ($y2 = 0; $y2 < $y; $y2++) {
$lettres_suivantes_colonne = $lettres_suivantes_colonne[$this->grille[$y2][$x]];
$lettres_colonne = $lettres_colonne->branches[$this->grille[$y2][$x]];
}
$lettres_communes = melanger_cles(array_intersect_key(
$lettres_suivantes_ligne,
$lettres_suivantes_colonne
));
$lettres_communes = array_intersect_key(
$lettres_ligne->branches,
$lettres_colonne->branches
);
foreach ($lettres_communes as $lettre => $_) {
$lettres_communes[$lettre] = count($lettres_ligne->branches[$lettre]) * count($lettres_colonne->branches[$lettre]) * gaussienne(1, 5);
}
uksort($lettres_communes, function($a, $b) use ($lettres_communes) {
return $lettres_communes[$b] <=> $lettres_communes[$a];
});
$lettres_communes = array_slice($lettres_communes, 0, 3);
foreach ($lettres_communes as $lettre => $_) {
$this->grille[$y][$x] = $lettre;
if ($x == $this->largeur - 1) {
$this->mots_utilises[$y] = $this->get_ligne($y, $x);
} else {
unset($this->mots_utilises[$y]);
}
if ($y == $this->hauteur - 1) {
if (in_array($this->get_colonne($x, $y), $this->mots_utilises)) {
continue;
}
// Omission des lettres isolées
if ($lettre == " "
&& ($y - 2 < 0 || $this->grille[$y - 2][$x] == " ")
&& ($y - 1 < 0 || $x - 1 < 0 || $this->grille[$y - 1][$x - 1] == " ")
&& ($y - 1 < 0 || $x + 1 >= $this->largeur || $this->grille[$y - 1][$x + 1] == " ")
) {
continue;
}
if ($i < $this->nb_positions) {
yield from $this->generateur($i + 1);
// Omission des doublons
$mots = [];
if ($x == $this->largeur - 1) $mots = explode(" ", $this->get_ligne($y, $this->largeur));
else if ($lettre == " ") $mots = explode(" ", $this->get_ligne($y, $x));
else $mots = [];
$this->lignes[$y] = array_filter($mots, function ($mot) {
return strlen($mot) >= 2;
});
if (count($this->lignes[$y])) {
$mot = array_pop($this->lignes[$y]);
if (strlen($mot > 2) && in_array($mot, array_merge(...$this->lignes, ...$this->colonnes))) continue;
else $this->lignes[$y][] = $mot;
}
if ($y == $this->hauteur - 1) {
$mots = explode(" ", $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;
else $this->colonnes[$x][$rang] = $mot;
}
} else {
$this->colonnes[$x] = [];
}
if ($i < $this->nb_positions - 1) {
yield from $this->gen_grilles($i + 1, $lettres_ligne->branches[$lettre]);
} else {
yield $this;
}
}
}
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 = "";
foreach ($this->grille as $ligne) {
foreach ($this->grille as $ligne)
$string .= implode("", $ligne);
}
return hash('sha256', $string);
}
public function current(): mixed
public function save($id)
{
return $this->grilles->current();
session_id($id);
session_start(["use_cookies" => false]);
$_SESSION["$this->largeur,$this->hauteur"] = implode(
"",
array_map(
function ($ligne) {
return implode("", $ligne);
},
$this->grille
)
);
}
public function key(): mixed {
return $this->grilles->key();
}
public function next(): void {
$this->grilles->next();
}
public function rewind(): void {
$this->grilles->rewind();
}
public function valid(): bool
public function load($id)
{
return $this->grilles->valid();
session_id($id);
session_start(["use_cookies" => false]);
if (!isset($_SESSION["$this->largeur,$this->hauteur"])) {
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(" ", $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(" ", $this->get_colonne($x, $this->hauteur));
$this->colonnes[$x] = array_filter($mots, function ($mot) {
return strlen($mot) >= 2;
});
}
return true;
}
public function offsetExists(mixed $offset): bool {
public function offsetExists(mixed $offset): bool
{
return isset($this->grille[$offset]);
}
public function offsetGet(mixed $offset): mixed {
public function offsetGet(mixed $offset): mixed
{
return $this->grille[$offset];
}
public function offsetSet(mixed $offset, mixed $value): void {
public function offsetSet(mixed $offset, mixed $value): void
{
$this->grille[$offset] = $value;
}
public function offsetUnset(mixed $offset): void {
public function offsetUnset(mixed $offset): void
{
unset($this->grille[$offset]);
}
}

95
Trie.php Normal file
View File

@ -0,0 +1,95 @@
<?php
class Trie implements ArrayAccess, IteratorAggregate, Countable {
public array $branches = [];
private $nb_branches = 0;
public function arraySet($cles, $valeur) {
$this->nb_branches++;
$cle = $cles[0];
$cles = array_slice($cles, 1);
if ($cles == []) {
$this->branches[$cle] = $valeur;
} else {
if (!isset($this->branches[$cle])) $this->branches[$cle] = new Trie();
$this->branches[$cle]->arraySet($cles, $valeur);
}
}
public function arrayExists($cles) {
$cle = $cles[0];
$cles = array_slice($cles, 1);
if ($cles == []) {
return isset($this->branches[$cle]);
} else {
return isset($this->branches[$cle]) && $this->branches[$cle]->arrayExists($cles);
}
}
public function &arrayGet($cles) {
$cle = $cles[0];
$cles = array_slice($cles, 1);
if ($cles == []) {
return $this->branches[$cle];
} else {
return $this->branches[$cle]->arrayGet($cles);
}
}
public function arrayUnset($cles) {
$cle = $cles[0];
$cles = array_slice($cles, 1);
if ($cles == []) {
unset($this->branches[$cle]);
$this->nb_branches--;
} else {
$this->branches[$cle]->arrayUnset($cles);
$this->nb_branches--;
if (count($this->branches[$cle]) == 0) {
unset($this->branches[$cle]);
}
}
}
public function arrayIterator() {
foreach ($this->branches as $cle => $branche) {
if ($branche instanceof Trie) {
foreach($branche->arrayIterator() as $sous_cles => $feuille) {
yield array_merge([$cle], $sous_cles) => $feuille;
}
} else {
yield [$cle] => $branche;
}
}
}
// 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
public function count(): int {
return $this->nb_branches;
}
}

977281
dico.csv

File diff suppressed because one or more lines are too long

103
dico.php
View File

@ -1,61 +1,66 @@
<?php
include_once "Trie.php";
const MIN_LETTRES_MOT_1 = 2;
const MIN_LETTRES_MOT_2 = 1;
const MIN_PREMIER_MOT = 1;
const MIN_MOTS_SUIVANTS = 1;
$dico = [];
if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($lecteur, 0, "\t");
while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) {
if (substr($ligne[0], 0, 1) != "#" && count($ligne) >= 3) {
[$mot, $definition, $auteur] = $ligne;
$mot = strtoupper($mot);
if ($auteur) {
$definition .= " <small><em>$auteur</em></small>";
}
$nb_espaces = substr_count($mot, ' ');
if ($nb_espaces > 0) {
$definition .= " <small>(" . ($nb_espaces + 1) . " mots)</small>";
}
if (strlen($definition)) {
$dico[$mot] = [$definition];
} else {
$dico[$mot] = [];
}
}
$nb_mots = 0;
function dico($longueur_max) {
global $nb_mots;
$transliterator = Transliterator::createFromRules(':: Any-Latin; :: Latin-ASCII; :: NFD; :: [:Nonspacing Mark:] Remove; :: Upper(); :: NFC;', Transliterator::FORWARD);
$dico = [[""]];
for ($longueur = 0; $longueur <= $longueur_max; $longueur++) {
$dico[] = new Trie();
}
fclose($lecteur);
}
if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
$entete = fgetcsv($lecteur, 0, "\t");
while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) {
if (
$ligne[0] == NULL
|| substr($ligne[0], 0, 1) == "#"
|| strlen($ligne[0]) > $longueur_max
) continue;
$mots_de_n_lettres = [];
foreach ($dico as $mot => $definition) {
$n = strlen($mot);
if (!isset($mots_de_n_lettres[$n])) {
$mots_de_n_lettres[$n] = [];
}
$mots_de_n_lettres[$n][] = $mot;
}
$mot = $ligne[0];
$definitions = array_slice($ligne, 1);
$mot = str_replace("-", " ", $mot);
function mots_espaces($longueur)
{
global $mots_de_n_lettres;
global $dico;
$nb_mots = 0;
foreach ($mots_de_n_lettres[$longueur] as $mot) {
yield $mot;
}
for ($i = MIN_LETTRES_MOT_1; $longueur - $i - 1 >= MIN_LETTRES_MOT_2; $i++) {
foreach ($mots_de_n_lettres[$i] as $mot1) {
foreach (mots_espaces($longueur - $i - 1) as $mot2) {
if ($mot1 != $mot2) {
$dico["$mot1 $mot2"] = array_merge($dico[$mot1], $dico[$mot2]);
yield "$mot1 $mot2";
$dico["$mot2 $mot1"] = array_merge($dico[$mot2], $dico[$mot1]);
yield "$mot2 $mot1";
$mot = $transliterator->transliterate($mot);
if (strpos($mot, " ") !== false) {
$mots = explode(" ", $mot);
$nb_mots = count($mots);
$mot = implode("", $mots);
foreach($definitions as $i => $definition) {
$definitions[$i] .= " ($nb_mots mots)";
}
}
$longueur = strlen($mot);
$dico[$longueur][$mot] = $definitions;
}
fclose($lecteur);
}
return $dico;
}
function mots_espaces($longueur_max) {
global $nb_mots;
$dico = dico($longueur_max);
for ($longueur = 1; $longueur <= $longueur_max; $longueur++) {
for ($position_espace = MIN_PREMIER_MOT; $position_espace + MIN_MOTS_SUIVANTS < $longueur; $position_espace++) {
$mots_suivants = $dico[$longueur - $position_espace - 1];
foreach ($dico[$position_espace]->arrayIterator() as $premier_mot => $definition) {
$premier_mot[] = " ";
$dico[$longueur]->arraySet($premier_mot, $mots_suivants);
}
}
}
return $dico;
}

50
favicon.svg Normal file
View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
viewBox="0 0 64 64"
version="1.1"
id="svg3"
sodipodi:docname="favicon.svg"
inkscape:version="1.4.1 (93de688d07, 2025-03-30)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs
id="defs3" />
<sodipodi:namedview
id="namedview3"
pagecolor="#505050"
bordercolor="#eeeeee"
borderopacity="1"
inkscape:showpageshadow="0"
inkscape:pageopacity="0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#505050"
inkscape:zoom="8.3124997"
inkscape:cx="28.57143"
inkscape:cy="30.977445"
inkscape:window-width="1536"
inkscape:window-height="793"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="svg3" />
<path
fill="#ffffff"
stroke="#000000"
stroke-width="2.03093"
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"
id="path1" />
<path
d="M 46.910816,17.089183 H 31.999999 v 14.910816 h 14.910817 z"
id="path2"
style="stroke-width:1.34646" />
<path
fill="none"
stroke="#000000"
stroke-width="2.06235"
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"
id="path3" />
</svg>

After

Width:  |  Height:  |  Size: 1.9 KiB

181
index.php
View File

@ -1,62 +1,79 @@
<?php
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
if (!isset($_GET["grille"])) {
$_GET["grille"] = uniqid();
header("Location: " . dirname($_SERVER['DOCUMENT_URI']) . "?" . http_build_query($_GET));
exit;
} else {
$id = htmlspecialchars($_GET["grille"]);
}
include_once "dico.php";
include_once "Grille.php";
const HAUTEUR_PAR_DEFAUT = 6;
const LARGEUR_PAR_DEFAUT = 6;
const HAUTEUR_DEFAUT = 8;
const HAUTEUR_MIN = 2;
const HAUTEUR_MAX = 10;
const LARGEUR_DEFAUT = 8;
const LARGEUR_MIN = 2;
const LARGEUR_MAX = 10;
$hauteur = filter_input(INPUT_GET, 'l', FILTER_VALIDATE_INT, [
$hauteur = filter_input(INPUT_GET, 'lignes', FILTER_VALIDATE_INT, [
"options" => [
"default" => HAUTEUR_PAR_DEFAUT,
"min_range" => 2,
"max_range" => 30
]
]);
$largeur = filter_input(INPUT_GET, 'c', FILTER_VALIDATE_INT, [
"options" => [
"default" => LARGEUR_PAR_DEFAUT,
"min_range" => 2,
"max_range" => 30
"default" => HAUTEUR_DEFAUT,
"min_range" => HAUTEUR_MIN,
"max_range" => HAUTEUR_MAX
]
]);
$grille = new Grille($hauteur, $largeur, $id);
$grille->current();
$definitions = [
"horizontales" => [],
"verticales" => []
];
for ($y = 0; $y < $hauteur; $y++) {
$definitions["horizontales"][$y] = $dico[$grille->get_ligne($y, $largeur)];
}
for ($x = 0; $x < $largeur; $x++) {
$definitions["verticales"][$x] = $dico[$grille->get_colonne($x, $hauteur)];
$largeur = filter_input(INPUT_GET, 'colonnes', FILTER_VALIDATE_INT, [
"options" => [
"default" => LARGEUR_DEFAUT,
"min_range" => LARGEUR_MIN,
"max_range" => LARGEUR_MAX
]
]);
$grille = new Grille($hauteur, $largeur);
if (!isset($_GET["grille"]) || $_GET["grille"] == "") {
do {
$id = uniqid();
} while (!$grille->genere($id));
$_GET["grille"] = $id;
header("Location: " . dirname($_SERVER['DOCUMENT_URI']) . "?" . http_build_query($_GET));
exit;
}
$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)) {
$definitions_horizontales[$y][] = $definitions[mt_rand(0, count($definitions) - 1)];
}
}
}
$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)) {
$definitions_verticales[$x][] = $definitions[mt_rand(0, count($definitions) - 1)];
}
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Mots croisés</title>
<title>MOTS■CROISES</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" href="favicon.ico">
<link rel="icon" href="favicon.svg">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
@ -89,9 +106,9 @@ for ($x = 0; $x < $largeur; $x++) {
</tbody>
</table>
</h1>
<h1 class="small width">Mots croisés</h1>
<h1 class="small width">Motscroisés</h1>
<div class="grille-et-definitions">
<?php if ($grille->valid()): ?>
<?php if ($grille_valide): ?>
<div class="grille">
<table>
<tr>
@ -105,14 +122,16 @@ for ($x = 0; $x < $largeur; $x++) {
<tr>
<th><?= $y + 1 ?></th>
<?php for ($x = 0; $x < $largeur; $x++): ?>
<td class="case <?= $grille[$y][$x] == " " ? "noire" : "blanche" ?>">
<?php if ($grille[$y][$x] == " "): ?>
<input type="text" maxlength="1" size="1" value=" " disabled />
<?php else: ?>
<input type="text" maxlength="1" size="1" pattern="[A-Z]" placeholder="<?= $grille[$y][$x] ?>"
title="<?= "Horizontalement\n" . strip_tags(implode("\n", $definitions["horizontales"][$y])) . "\nVerticalement\n" . strip_tags(implode("\n", $definitions["verticales"][$x])) ?>" />
<?php endif; ?>
</td>
<?php if ($grille[$y][$x] == " "): ?>
<td class="case noire">
<input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1" value=" " disabled />
</td>
<?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])) ?>" />
</td>
<?php endif; ?>
<?php endfor; ?>
</tr>
<?php endfor; ?>
@ -120,52 +139,62 @@ for ($x = 0; $x < $largeur; $x++) {
</div>
<div class="definitions horizontales">
<h2>Horizontalement</h2>
<ol>
<?php for ($y = 0; $y < $hauteur; $y++): ?>
<ol type="1">
<?php foreach ($definitions_horizontales as $y => $definitions): ?>
<li>
<?php if (count($definitions["horizontales"][$y]) == 1): ?>
<?= $definitions["horizontales"][$y][0] ?>
<?php else: ?>
<ol>
<?php foreach ($definitions["horizontales"][$y] as $definition) : ?>
<li><?= $definition ?></li>
<?php endforeach ?>
</ol>
<?php if (count($definitions)): ?>
<?php if (count($definitions) == 1): ?>
<?= $definitions[0] ?>
<?php else: ?>
<ol>
<?php foreach ($definitions as $definition) : ?>
<li><?= $definition ?></li>
<?php endforeach ?>
</ol>
<?php endif ?>
<?php endif ?>
</li>
<?php endfor; ?>
<?php endforeach; ?>
</ol>
</div>
<div class="definitions verticales">
<h2>Verticalement</h2>
<ol type="A">
<?php for ($x = 0; $x < $largeur; $x++): ?>
<?php foreach ($definitions_verticales as $x => $definitions): ?>
<li>
<?php if (count($definitions["horizontales"][$x]) == 1): ?>
<?= $definitions["horizontales"][$x][0] ?>
<?php else: ?>
<ol>
<?php foreach ($definitions["horizontales"][$x] as $definition) : ?>
<li><?= $definition ?></li>
<?php endforeach ?>
</ol>
<?php if (count($definitions)): ?>
<?php if (count($definitions) == 1): ?>
<?= $definitions[0] ?>
<?php else: ?>
<ol>
<?php foreach ($definitions as $definition) : ?>
<li><?= $definition ?></li>
<?php endforeach ?>
</ol>
<?php endif ?>
<?php endif ?>
</li>
<?php endfor; ?>
<?php endforeach; ?>
</ol>
</div>
<?php else: ?>
<?php else: http_response_code(500); ?>
<h3 class="erreur">Erreur de génération de la grille</h3>
<?php endif ?>
</div>
<input type="hidden" id="lignes" <?php if (isset($_GET["lignes"])): ?>name="lignes" <?php endif ?>value="<?= $hauteur ?>" />
<input type="hidden" id="colonnes" <?php if (isset($_GET["colonnes"])): ?>name="colonnes" <?php endif ?>value="<?= $largeur ?>" />
<input type="hidden" id="solution_hashee" value="<?= $grille->hash() ?>" />
<button type="submit"><img src="favicon.ico">Nouvelle grille</button>
<div class="nouvelle-grille">
<img src="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>
<script>navigator?.serviceWorker.register('service-worker.js')</script>
</body>
</html>

139
script.js
View File

@ -1,59 +1,102 @@
async function sha256(text) {
const encoder = new TextEncoder();
const data = encoder.encode(text);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
const encoder = new TextEncoder();
const data = encoder.encode(text);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
}
const inputs = Array.from(grilleForm.querySelectorAll('input[type="text"]'));
let inputs = grilleForm.querySelectorAll(".grille input");
let largeur = Number(colonnes.value);
let nb_cases = inputs.length;
let index = 0;
for (let input of inputs) {
input.index = index++;
input.x = input.index % largeur;
input.y = Math.floor(input.index / largeur);
inputs.forEach(input => {
input.index = index++;
input.onfocus = function (event) {
for (li of document.querySelectorAll(
`.definitions.horizontales > ol > li:nth-child(${
input.y + 1
}), .definitions.verticales > ol > li:nth-child(${input.x + 1})`
)) {
li.classList.add("selectionee");
}
for (li of document.querySelectorAll(
`.definitions.horizontales > ol > li:not(:nth-child(${
input.y + 1
})), .definitions.verticales > ol > li:not(:nth-child(${input.x + 1}))`
)) {
li.classList.add("non-selectionee");
}
input.onfocus = function (event) {
input.select();
};
input.select();
};
input.onkeydown = function (event) {
next_input = null;
switch (event.key) {
case 'ArrowUp':
next_input = inputs[(input.index - largeur + nb_cases) % nb_cases];
break;
case 'ArrowDown':
next_input = inputs[(input.index + largeur) % nb_cases];
break;
case 'ArrowLeft':
next_input = inputs[(input.index - 1 + nb_cases) % nb_cases];
break;
case 'ArrowRight':
next_input = inputs[(input.index + 1) % nb_cases];
break;
}
if (next_input) {
next_input.focus();
next_input.select();
event.preventDefault();
}
};
input.onkeydown = function (event) {
next_input = null;
switch (event.key) {
case "ArrowUp":
next_input = inputs[(input.index - largeur + nb_cases) % nb_cases];
break;
case "ArrowDown":
next_input = inputs[(input.index + largeur) % nb_cases];
break;
case "ArrowLeft":
next_input = inputs[(input.index - 1 + nb_cases) % nb_cases];
break;
case "ArrowRight":
next_input = inputs[(input.index + 1) % nb_cases];
break;
}
if (next_input) {
next_input.focus();
next_input.select();
event.preventDefault();
}
};
input.oninput = function (event) {
this.value = this.value.toUpperCase();
if (!input.checkValidity()) {
input.value = '';
input.oninput = function (event) {
this.value = this.value.toUpperCase();
if (!input.checkValidity()) {
input.value = "";
}
if (
inputs.every((input) => input.value.length == 1) &&
grilleForm.checkValidity()
) {
sha256(inputs.map((input) => input.value).join("")).then((hash) => {
if (hash == solution_hashee.value) {
if (confirm("Bravo !\nUne nouvelle partie ?")) {
grilleForm.submit();
}
}
if (inputs.every(input => input.value.length == 1) && grilleForm.checkValidity()) {
sha256(inputs.map(input => input.value).join('')).then(hash => {
if (hash == solution_hashee.value) {
if (confirm('Bravo ! \nUne nouvelle partie ?')) {
grilleForm.submit();
}
}
});
}
};
});
});
}
};
input.onblur = function (event) {
for (li of document.querySelectorAll(
`.definitions.horizontales > ol > li:nth-child(${
input.y + 1
}), .definitions.verticales > ol > li:nth-child(${input.x + 1})`
)) {
li.classList.remove("selectionee");
}
for (li of document.querySelectorAll(
`.definitions.horizontales > ol > li:not(:nth-child(${
input.y + 1
})), .definitions.verticales > ol > li:not(:nth-child(${input.x + 1}))`
)) {
li.classList.remove("non-selectionee");
}
};
}
for (let input of grilleForm.querySelectorAll(".nouvelle-grille input")) {
input.onfocus = function (event) {
input.name = input.id;
input.select();
};
}

86
service-worker.js Normal file
View File

@ -0,0 +1,86 @@
/*
Copyright 2015, 2019, 2020 Google LLC. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Incrementing OFFLINE_VERSION will kick off the install event and force
// previously cached resources to be updated from the network.
const OFFLINE_VERSION = 1;
const CACHE_NAME = "offline";
// Customize this with a different URL if needed.
const OFFLINE_URL = "index.php";
self.addEventListener("install", (event) => {
event.waitUntil(
(async () => {
const cache = await caches.open(CACHE_NAME);
// Setting {cache: 'reload'} in the new request will ensure that the
// response isn't fulfilled from the HTTP cache; i.e., it will be from
// the network.
await cache.add(new Request(OFFLINE_URL, { cache: "reload" }));
})()
);
// Force the waiting service worker to become the active service worker.
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
(async () => {
// Enable navigation preload if it's supported.
// See https://developers.google.com/web/updates/2017/02/navigation-preload
if ("navigationPreload" in self.registration) {
await self.registration.navigationPreload.enable();
}
})()
);
// Tell the active service worker to take control of the page immediately.
self.clients.claim();
});
self.addEventListener("fetch", (event) => {
// We only want to call event.respondWith() if this is a navigation request
// for an HTML page.
if (event.request.mode === "navigate") {
event.respondWith(
(async () => {
try {
// First, try to use the navigation preload response if it's supported.
const preloadResponse = await event.preloadResponse;
if (preloadResponse) {
return preloadResponse;
}
// Always try the network first.
const networkResponse = await fetch(event.request);
return networkResponse;
} catch (error) {
// catch is only triggered if an exception is thrown, which is likely
// due to a network error.
// If fetch() returns a valid HTTP response with a response code in
// the 4xx or 5xx range, the catch() will NOT be called.
console.log("Fetch failed; returning offline page instead.", error);
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(OFFLINE_URL);
return cachedResponse;
}
})()
);
}
// If our if() condition is false, then this fetch handler won't intercept the
// request. If there are any other fetch handlers registered, they will get a
// chance to call event.respondWith(). If no fetch handlers call
// event.respondWith(), the request will be handled by the browser as if there
// were no service worker involvement.
});

359
style.css
View File

@ -1,283 +1,324 @@
body {
margin: 0;
padding: 2rem 1rem;
background-color: white;
font-family: Times, 'Times New Roman', Georgia, serif;
margin: 0;
padding: 0;
background-color: white;
font-family: Times, "Times New Roman", Georgia, serif;
}
form {
display: flex;
flex-flow: column;
min-height: calc(100vh - 4rem);
display: flex;
padding: 1rem;
min-height: calc(100vh - 2rem);
flex-flow: column;
justify-content: space-between;
}
h1 {
margin: 0;
letter-spacing: 0.2em;
margin: 0;
letter-spacing: 0.2em;
}
h1.large.width {
display: inherit;
display: inherit;
padding-top: 1rem;
}
h1.small.width {
display: none;
display: none;
}
h1 table {
margin: auto;
line-height: 0.8;
margin: auto;
line-height: 0.8;
}
h1 td {
width: 0.7em;
text-align: center;
width: 0.7em;
text-align: center;
}
h1,
h2 {
font-variant-caps: petite-caps;
text-align: center;
font-variant-caps: petite-caps;
text-align: center;
}
.grille-et-definitions {
display: flex;
flex-flow: row;
justify-content: space-evenly;
flex-wrap: wrap;
height: max-content;
flex-grow: 1;
align-items: center;
display: flex;
flex-flow: row;
justify-content: space-evenly;
flex-wrap: wrap;
height: max-content;
gap: 1em;
}
.grille {
display: flex;
}
.grille table {
border-collapse: collapse;
margin: 0 auto;
height: fit-content;
border-collapse: collapse;
margin: auto;
height: fit-content;
}
.grille th,
.grille td {
width: 2rem;
height: 2rem;
text-align: center;
width: 2rem;
height: 2rem;
text-align: center;
vertical-align: middle;
}
.grille td {
width: 2rem;
height: 2rem;
border: 1px solid black;
padding: 2px;
background-color: white;
width: 2rem;
height: 2rem;
border: 1px solid black;
padding: 2px;
background-color: white;
}
.grille tr:nth-of-type(2) td {
border-top-width: 3px;
border-top-width: 3px;
}
.grille tr:last-of-type td {
border-bottom-width: 3px;
border-bottom-width: 3px;
}
.grille td:first-of-type {
border-left-width: 3px;
border-left-width: 3px;
}
.grille td:last-child {
border-right-width: 3px;
border-right-width: 3px;
}
.grille .case.noire {
background-color: black;
}
.grille .case.noire {
background-color: black;
background-color: black;
}
.grille input {
width: 100%;
height: 100%;
border: none;
padding: 0;
text-align: center;
font-size: 1.2em;
font-family: 'Comic Sans MS', 'Comic Sans', sans;
color: darkblue;
background-color: white;
width: 100%;
height: 100%;
border: none;
padding: 0;
text-align: center;
font-size: 1.2em;
font-family: "Comic Sans MS", "Comic Sans", sans;
color: darkblue;
background-color: white;
}
.grille input[disabled] {
background-color: black;
background-color: black;
}
.grille input::placeholder {
color: transparent;
color: transparent;
}
.definitions {
width: 30%;
width: 30%;
}
.definitions.horizontales {
order: -1;
order: -1;
}
.definitions ol {
padding-left: 2em;
padding-left: 2em;
}
.definitions > div > ol > li::marker {
font-weight: bold;
font-weight: bold;
}
.definitions li ol {
padding-left: 0em;
list-style: parenthese;
padding-left: 0em;
list-style: parenthese;
}
@counter-style parenthese {
system: extends decimal;
suffix: ") ";
system: extends decimal;
suffix: ") ";
}
.definitions li li {
margin-left: 0.8em;
counter-increment: count;
margin-left: 0.8em;
counter-increment: count;
}
.definitions li li::marker {
font-size: small;
font-size: small;
}
.definitions em {
text-wrap: nowrap;
text-wrap: nowrap;
}
.definitions.case ol.horizontales {
list-style-type: "→ ";
}
.definitions.case ol.verticales {
list-style-type: "↓ ";
}
.definitions li {
transition: opacity 0.3s;
}
.definitions li.non-selectionee {
opacity: 30%;
}
.erreur {
text-align: center;
text-align: center;
}
button[type='submit'] {
width: fit-content;
margin: 0 auto;
.nouvelle-grille img {
margin: 0 0.5em;
}
button[type='submit'] {
width: 100%;
margin: auto;
border: none;
background: none;
font-family: inherit;
font-size: 1em;
display: flex;
align-items: center;
gap: 0.5em;
justify-content: center;
.nouvelle-grille {
margin: 1em auto 0 auto;
display: flex;
align-items: baseline;
}
button[type="submit"]:hover {
cursor: pointer;
color: #2a6496;
text-decoration: underline;
.nouvelle-grille button,
.nouvelle-grille input {
border: none;
background: none;
color: inherit;
font-family: inherit;
font-size: 1em;
cursor: pointer;
}
button[type="submit"]:active {
color: darkorchid;
.nouvelle-grille button {
text-decoration: underline;
}
.nouvelle-grille button:hover {
color: #2a6496;
}
.nouvelle-grille button:active {
color: darkorchid;
}
.nouvelle-grille input {
-moz-appearance: textfield;
appearance: textfield;
margin: 0;
padding: 0;
text-align: center;
text-decoration: underline dotted;
width: 1em;
}
.nouvelle-grille input::-webkit-inner-spin-button,
.nouvelle-grille input::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
@media (max-width: 1024px) {
body {
padding: 1.5rem 1rem;
}
h1.large.width {
display: none;
}
h1.large.width {
display: none;
}
h1.small.width {
display: initial;
}
h1.small.width {
display: initial;
}
h1 table {
line-height: 0.7;
}
h1 table {
line-height: 0.7;
}
.grille {
width: 100%;
margin: auto;
}
form {
min-height: calc(100vh - 2rem);
}
.definitions {
width: 45%;
}
.grille {
width: 100%;
margin: auto;
}
.definitions {
width: 45%;
}
.definitions.horizontales {
order: inherit
}
.definitions.horizontales {
order: inherit;
}
}
@media (max-width: 640px) {
* {
box-sizing: border-box;
}
* {
box-sizing: border-box;
}
body {
width: auto;
margin: 0;
padding: 1rem 0;
}
body {
width: auto;
margin: 0;
padding: 1rem 0;
}
h1.large.width {
display: none;
}
h1.large.width {
display: none;
}
h1.small.width {
display: none;
}
h1.small.width {
display: none;
}
.grille td {
width: 2.5rem;
height: 2.5rem;
}
.grille td {
width: 2.5rem;
height: 2.5rem;
}
.definitions {
display: flex;
flex-flow: column;
}
h2 {
font-size: 1.2em;
margin: 1em 0 0.5em 0;
}
.definitions.horizontales,
.definitions.verticales {
width: 100%;
}
.definitions.horizontales,
.definitions.verticales {
width: 100%;
}
.definitions > ol {
margin: 0;
}
.definitions li.non-selectionee {
visibility: hidden;
height: 0;
margin: 0;
padding: 0;
}
}
@media (max-device-width:768px) and (orientation: landscape) {
@media (max-device-width: 768px) and (orientation: landscape) {
html {
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
}
@media (prefers-color-scheme: dark) {
body,
button {
background-color: #02081a;
color: #c6c6c6;
}
body,
button {
background-color: #02081a;
color: #c6c6c6;
}
.grille td,
.grille input {
background-color: #edeeee;
}
.grille td,
.grille input {
background-color: #edeeee;
}
button[type="submit"]:hover {
color: #479fec;
}
.nouvelle-grille button:hover {
color: #479fec;
}
button[type="submit"]:active {
color: orchid;
}
.nouvelle-grille button:active {
color: orchid;
}
}