Compare commits
115 Commits
6da441e9df
...
grand-dico
Author | SHA1 | Date | |
---|---|---|---|
13fcdf05fb | |||
ca39709fbd | |||
ddebd453df | |||
d5a120cd9a | |||
ab9e1f08ef | |||
b1f3e8b85f | |||
c20d2324e9 | |||
65bb8be2e3 | |||
ed1ad0566f | |||
47be3d2e51 | |||
6ee909914d | |||
4c014bc789 | |||
b063d39689 | |||
d150e76a9e | |||
a6b15fbb87 | |||
afd79eb3a2 | |||
ea4555144c | |||
1a35d8e920 | |||
7ae6506539 | |||
f8bd1d35cd | |||
23cac370a1 | |||
dc488aea4d | |||
91db11cc2d | |||
4a19252a94 | |||
ca95b76558 | |||
97b9766db3 | |||
168fc5f7a2 | |||
0dd9881f6d | |||
32db8f5a6c | |||
9f3a90a04e | |||
73e8d6a857 | |||
7a245d601a | |||
e1eb7685e4 | |||
10ea7a7f0b | |||
63cd243e91 | |||
c097be27d8 | |||
399370624c | |||
911da7aef9 | |||
1987905eb9 | |||
2214cf4a05 | |||
5997397908 | |||
b85bf7eb99 | |||
4a1106fcba | |||
d867e40499 | |||
d852596386 | |||
38344a315c | |||
b042fcd77a | |||
912b4c410d | |||
fcee18ee04 | |||
1073cd53fc | |||
fa134b8102 | |||
6b6edd369b | |||
db9b79fb6d | |||
4874bc0d78 | |||
6db7e7ab63 | |||
9546ac4d97 | |||
351e8387bb | |||
c8ecb504ef | |||
92306e2a2a | |||
fcf7977e1a | |||
64e4113aa7 | |||
590e9ab3ec | |||
0276726063 | |||
7adeb187bd | |||
20c1bc090d | |||
851edebb4b | |||
18cce82633 | |||
b703729e66 | |||
cb8b42af67 | |||
7aea2d9f99 | |||
88d95e42ad | |||
1416b8bd54 | |||
2445bfcf54 | |||
50872aef31 | |||
4fb2e70915 | |||
44b6fd7e8e | |||
f1ceae33f4 | |||
584f1a81a0 | |||
bcfe6da555 | |||
dbe228c6c2 | |||
856825a5e4 | |||
b3ad38815f | |||
e04b6f0b91 | |||
92dcf21297 | |||
ac778222e1 | |||
cfa03c1927 | |||
7d346da288 | |||
eae502702e | |||
34db5a47a7 | |||
c2d9947131 | |||
958116a83c | |||
33dd403de1 | |||
967d5de46c | |||
dd8dabd39c | |||
4b69fa4803 | |||
64bc91d4a0 | |||
65e31d71c6 | |||
e234dd0ff8 | |||
2aba951d3b | |||
d1d189a0db | |||
6249729fcc | |||
3834e579a6 | |||
f0b9052211 | |||
ea141bfde6 | |||
b9eb5b725f | |||
ef49f35e2a | |||
1689b54570 | |||
c4ab6cb09d | |||
7a29d4d12d | |||
0c6cd43dcd | |||
c5e13c277e | |||
341ac43d86 | |||
d730b9a26a | |||
5537607011 | |||
cb4c6c91ed |
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
__pycache__/
|
||||
test*.*
|
||||
*.py
|
||||
dico2.csv
|
||||
wiktionaryXfr2010.7z
|
295
Grille.php
295
Grille.php
@ -1,133 +1,228 @@
|
||||
<?php
|
||||
|
||||
include_once "dico.php";
|
||||
|
||||
|
||||
const MIN_LETTRES = 1;
|
||||
$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 {
|
||||
class Grille implements ArrayAccess
|
||||
{
|
||||
public $grille;
|
||||
public $hauteur;
|
||||
public $largeur;
|
||||
private $grilles;
|
||||
private $mots_commencant_par;
|
||||
private $mots_utilises = [];
|
||||
public $dico;
|
||||
private $positions;
|
||||
private $nb_positions;
|
||||
public $lignes = [];
|
||||
public $colonnes = [];
|
||||
public $valide = false;
|
||||
private $id;
|
||||
|
||||
public function __construct($hauteur, $largeur) {
|
||||
$this->hauteur = $hauteur;
|
||||
$this->largeur = $largeur;
|
||||
$this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '.'));
|
||||
public function __construct($hauteur, $largeur)
|
||||
{
|
||||
$this->hauteur = $hauteur;
|
||||
$this->largeur = $largeur;
|
||||
$this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, ''));
|
||||
|
||||
if ($hauteur == $largeur) {
|
||||
$dimensions = [$hauteur];
|
||||
} else {
|
||||
$dimensions = [$hauteur, $largeur];
|
||||
$this->positions = [];
|
||||
for ($y = 0; $y < $hauteur; $y++) {
|
||||
for ($x = 0; $x < $largeur; $x++)
|
||||
$this->positions[] = [$x, $y];
|
||||
}
|
||||
$this->mots_commencant_par = [];
|
||||
foreach ($dimensions as $dimension) {
|
||||
$this->mots_commencant_par[$dimension] = [];
|
||||
foreach(mots_espaces($dimension, MIN_LETTRES) as $mot) {
|
||||
for ($i = 0; $i <= $dimension; $i++) {
|
||||
$debut = substr($mot, 0, $i);
|
||||
if (!isset($this->mots_commencant_par[$dimension][$debut])) {
|
||||
$this->mots_commencant_par[$dimension][$debut] = [];
|
||||
}
|
||||
$this->mots_commencant_par[$dimension][$debut][] = $mot;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->grilles = $this->generateur();
|
||||
$this->grilles->current();
|
||||
$this->nb_positions = count($this->positions);
|
||||
|
||||
$this->dico = mots_espaces(max($hauteur, $largeur));
|
||||
}
|
||||
|
||||
public function get_ligne($l, $max = 100) {
|
||||
public function get_ligne($y, $largeur)
|
||||
{
|
||||
$ligne = "";
|
||||
$min = min($this->largeur, $max);
|
||||
for ($i = 0; $i < $min; $i++) {
|
||||
$ligne .= $this->grille[$l][$i];
|
||||
}
|
||||
for ($x = 0; $x < $largeur; $x++)
|
||||
$ligne .= $this->grille[$y][$x];
|
||||
return $ligne;
|
||||
}
|
||||
|
||||
public function set_ligne($l, $mot) {
|
||||
for ($i = 0; $i < strlen($mot); $i++) {
|
||||
$this->grille[$l][$i] = $mot[$i];
|
||||
}
|
||||
}
|
||||
|
||||
public function get_colonne($c, $max = 100) {
|
||||
public function get_colonne($x, $hauteur)
|
||||
{
|
||||
$colonne = "";
|
||||
$min = min($this->hauteur, $max);
|
||||
for ($i = 0; $i < $min; $i++) {
|
||||
$colonne .= $this->grille[$i][$c];
|
||||
}
|
||||
for ($y = 0; $y < $hauteur; $y++)
|
||||
$colonne .= $this->grille[$y][$x];
|
||||
return $colonne;
|
||||
}
|
||||
|
||||
public function set_colonne($c, $mot) {
|
||||
for ($i = 0; $i < strlen($mot); $i++) {
|
||||
$this->grille[$i][$c] = $mot[$i];
|
||||
}
|
||||
}
|
||||
|
||||
public function generateur() {
|
||||
yield from $this->trouve_une_ligne(0);
|
||||
$this->grille = array_fill(0, $this->hauteur, array_fill(0, $this->largeur, ' '));
|
||||
}
|
||||
public function gen_grilles($i = 0, $lettres_ligne = NULL)
|
||||
{
|
||||
[$x, $y] = $this->positions[$i];
|
||||
|
||||
private function trouve_une_ligne($l) {
|
||||
global $mots_de_n_lettres;
|
||||
foreach ($this->mots_commencant_par[$this->largeur][$this->get_ligne($l, $l)] as $mot_lig) {
|
||||
$this->set_ligne($l, $mot_lig);
|
||||
$ok = true;
|
||||
for ($c = $l; $c < $this->largeur; $c++) {
|
||||
if (!isset($this->mots_commencant_par[$this->hauteur][$this->get_colonne($c, $l+1)])) {
|
||||
$ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$ok) {
|
||||
// Recherche de la prochaine lettre possible sur la case courante
|
||||
// en ligne
|
||||
if ($x == 0) {
|
||||
$lettres_ligne = $this->dico[$this->largeur];
|
||||
}
|
||||
|
||||
// en colonne
|
||||
$lettres_colonne = $this->dico[$this->hauteur];
|
||||
for ($y2 = 0; $y2 < $y; $y2++) {
|
||||
$lettres_colonne = $lettres_colonne->branches[$this->grille[$y2][$x]];
|
||||
}
|
||||
$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;
|
||||
|
||||
// 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;
|
||||
}
|
||||
$this->mots_utilises[$mot_lig] = true;
|
||||
if ($l < $this->largeur) {
|
||||
yield from $this->trouve_une_colonne($l);
|
||||
} else if ($l + 1 < $this->hauteur) {
|
||||
yield from $this->trouve_une_ligne($l + 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;
|
||||
}
|
||||
unset($this->mots_utilises[$mot_lig]);
|
||||
}
|
||||
}
|
||||
|
||||
private function trouve_une_colonne($c) {
|
||||
global $mots_de_n_lettres;
|
||||
foreach ($this->mots_commencant_par[$this->hauteur][$this->get_colonne($c, $c + 1)] as $mot_col) {
|
||||
if (isset($this->mots_utilises[$mot_col])) {
|
||||
continue;
|
||||
}
|
||||
$this->set_colonne($c, $mot_col);
|
||||
$ok = true;
|
||||
for ($l = $c; $l < $this->hauteur; $l++) {
|
||||
if (!isset($this->mots_commencant_par[$this->largeur][$this->get_ligne($l, $c+1)])) {
|
||||
$ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$ok) {
|
||||
continue;
|
||||
}
|
||||
$this->mots_utilises[$mot_col] = true;
|
||||
if ($c +1 < $this->hauteur) {
|
||||
yield from $this->trouve_une_ligne($c + 1);
|
||||
} else if ($c + 1 < $this->largeur) {
|
||||
yield from $this->trouve_une_colonne($c + 1);
|
||||
} else {
|
||||
yield $this;
|
||||
}
|
||||
unset($this->mots_utilises[$mot_col]);
|
||||
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)
|
||||
$string .= implode("", $ligne);
|
||||
return hash('sha256', $string);
|
||||
}
|
||||
|
||||
public function save($id)
|
||||
{
|
||||
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 load($id)
|
||||
{
|
||||
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
|
||||
{
|
||||
return isset($this->grille[$offset]);
|
||||
}
|
||||
|
||||
public function offsetGet(mixed $offset): mixed
|
||||
{
|
||||
return $this->grille[$offset];
|
||||
}
|
||||
|
||||
public function offsetSet(mixed $offset, mixed $value): void
|
||||
{
|
||||
$this->grille[$offset] = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset(mixed $offset): void
|
||||
{
|
||||
unset($this->grille[$offset]);
|
||||
}
|
||||
}
|
||||
|
95
Trie.php
Normal file
95
Trie.php
Normal 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;
|
||||
}
|
||||
}
|
99
dico.php
99
dico.php
@ -1,47 +1,66 @@
|
||||
<?php
|
||||
include_once "Trie.php";
|
||||
|
||||
|
||||
const MIN_PREMIER_MOT = 1;
|
||||
const MIN_MOTS_SUIVANTS = 1;
|
||||
|
||||
|
||||
$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();
|
||||
}
|
||||
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;
|
||||
|
||||
$dico = [];
|
||||
if (($handle = fopen("dico.csv", "r")) !== FALSE) {
|
||||
$header = fgetcsv($handle, 0, "\t");
|
||||
while (($ligne = fgetcsv($handle, 0, "\t")) !== FALSE) {
|
||||
if (count($ligne) >= 2) {
|
||||
$mot = $ligne[0];
|
||||
$definition = $ligne[1];
|
||||
$dico[$mot] = $definition;
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
foreach ($mots_de_n_lettres as $n => $mots) {
|
||||
shuffle($mots_de_n_lettres[$n]);
|
||||
}
|
||||
|
||||
function mots_espaces($max, $min=0) {
|
||||
global $mots_de_n_lettres;
|
||||
global $dico;
|
||||
|
||||
foreach($mots_de_n_lettres[$max] as $mot) {
|
||||
yield $mot;
|
||||
}
|
||||
for ($i = ceil($max / 2); $max - $i -1 >= $min; $i++) {
|
||||
foreach ($mots_de_n_lettres[$i] as $mot1) {
|
||||
foreach (mots_espaces($max - $i -1, $min) as $mot2) {
|
||||
if ($mot1 != $mot2) {
|
||||
$dico["$mot1 $mot2"] = $dico[$mot1] && $dico[$mot2] ? "{$dico[$mot1]}. {$dico[$mot2]}." : $dico[$mot1] . $dico[$mot2];
|
||||
yield "$mot1 $mot2";
|
||||
$dico["$mot2 $mot1"] = $dico[$mot2] && $dico[$mot1] ? "{$dico[$mot2]}. {$dico[$mot1]}." : $dico[$mot2] . $dico[$mot1];
|
||||
yield "$mot2 $mot1";
|
||||
$definitions = array_slice($ligne, 1);
|
||||
$mot = str_replace("-", " ", $mot);
|
||||
|
||||
$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;
|
||||
}
|
||||
|
BIN
favicon.ico
Normal file
BIN
favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 518 B |
50
favicon.svg
Normal file
50
favicon.svg
Normal 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 |
225
index.php
225
index.php
@ -1,83 +1,200 @@
|
||||
<?php
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('html_errors', 1);
|
||||
ini_set('error_reporting', E_ALL);
|
||||
|
||||
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, 'lignes', FILTER_VALIDATE_INT, [
|
||||
"options" => [
|
||||
"default" => HAUTEUR_PAR_DEFAUT,
|
||||
"min_range" => 2,
|
||||
"max_range" => 10
|
||||
"default" => HAUTEUR_DEFAUT,
|
||||
"min_range" => HAUTEUR_MIN,
|
||||
"max_range" => HAUTEUR_MAX
|
||||
]
|
||||
]);
|
||||
|
||||
$largeur = filter_input(INPUT_GET, 'colonnes', FILTER_VALIDATE_INT, [
|
||||
"options" => [
|
||||
"default" => LARGEUR_PAR_DEFAUT,
|
||||
"min_range" => 2,
|
||||
"max_range" => 10
|
||||
"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.svg">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="grille">
|
||||
<table class="grille">
|
||||
<tr>
|
||||
<th></th>
|
||||
<?php for ($c = 0; $c < $largeur; $c++): ?>
|
||||
<th><?= chr($c + 65) ?></th>
|
||||
<?php endfor; ?>
|
||||
</tr>
|
||||
<?php for ($l = 0; $l < $hauteur; $l++): ?>
|
||||
<tr>
|
||||
<th><?= $l + 1 ?></th>
|
||||
<?php for ($c = 0; $c < $largeur; $c++): ?>
|
||||
<td class="case <?= $grille->grille[$l][$c]==" "?"noire": "blanche" ?>">
|
||||
<?php if ($grille->grille[$l][$c] == " "): ?>
|
||||
<input type="text" maxlength="1" size="1" name="<?= $l . $c ?>" disabled/>
|
||||
<?php else: ?>
|
||||
<input type="text" maxlength="1" size="1" name="<?= $l . $c ?>" />
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<?php endfor; ?>
|
||||
</tr>
|
||||
<?php endfor; ?>
|
||||
</table>
|
||||
</div>
|
||||
<div class="definitions">
|
||||
<div class="horizontales">
|
||||
<h2>Horizontalement</h2>
|
||||
<ol>
|
||||
<?php for ($l = 0; $l < $hauteur; $l++): ?>
|
||||
<li><?= $dico[$grille->get_ligne($l, $largeur)] ?></li>
|
||||
<?php endfor; ?>
|
||||
</ol>
|
||||
<form id="grilleForm" method="get" location=".">
|
||||
<h1 class="large width">
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td colspan="2"></td>
|
||||
<td>M</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>c</td>
|
||||
<td>r</td>
|
||||
<td>o</td>
|
||||
<td>i</td>
|
||||
<td>s</td>
|
||||
<td>é</td>
|
||||
<td>s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2"></td>
|
||||
<td>t</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2"></td>
|
||||
<td>s</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</h1>
|
||||
<h1 class="small width">Mots■croisés</h1>
|
||||
<div class="grille-et-definitions">
|
||||
<?php if ($grille_valide): ?>
|
||||
<div class="grille">
|
||||
<table>
|
||||
<tr>
|
||||
<th></th>
|
||||
<?php for ($x = 0; $x < $largeur; $x++): ?>
|
||||
<th><?= chr($x + 65) ?></th>
|
||||
<?php endfor; ?>
|
||||
<th></th>
|
||||
</tr>
|
||||
<?php for ($y = 0; $y < $hauteur; $y++): ?>
|
||||
<tr>
|
||||
<th><?= $y + 1 ?></th>
|
||||
<?php for ($x = 0; $x < $largeur; $x++): ?>
|
||||
<?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; ?>
|
||||
</table>
|
||||
</div>
|
||||
<div class="definitions horizontales">
|
||||
<h2>Horizontalement</h2>
|
||||
<ol type="1">
|
||||
<?php foreach ($definitions_horizontales as $y => $definitions): ?>
|
||||
<li>
|
||||
<?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 endforeach; ?>
|
||||
</ol>
|
||||
</div>
|
||||
<div class="definitions verticales">
|
||||
<h2>Verticalement</h2>
|
||||
<ol type="A">
|
||||
<?php foreach ($definitions_verticales as $x => $definitions): ?>
|
||||
<li>
|
||||
<?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 endforeach; ?>
|
||||
</ol>
|
||||
</div>
|
||||
<?php else: http_response_code(500); ?>
|
||||
<h3 class="erreur">Erreur de génération de la grille</h3>
|
||||
<?php endif ?>
|
||||
</div>
|
||||
<div class="verticales">
|
||||
<h2>Verticalement</h2>
|
||||
<ol type="A">
|
||||
<?php for ($c = 0; $c < $largeur; $c++): ?>
|
||||
<li><?= $dico[$grille->get_colonne($c, $hauteur)] ?></li>
|
||||
<?php endfor; ?>
|
||||
</ol>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<script src="script.js"></script>
|
||||
<script>navigator?.serviceWorker.register('service-worker.js')</script>
|
||||
</body>
|
||||
|
||||
</html>
|
258
index0.php
258
index0.php
@ -1,258 +0,0 @@
|
||||
<?php
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('html_errors', 1);
|
||||
ini_set('error_reporting', E_ALL);
|
||||
|
||||
$default_lignes = 3;
|
||||
$default_colonnes = 4;
|
||||
|
||||
$hauteur = filter_input(INPUT_GET, 'lignes', FILTER_VALIDATE_INT, [
|
||||
"options" => [
|
||||
"default" => $default_lignes,
|
||||
"min_range" => 2,
|
||||
"max_range" => 10
|
||||
]
|
||||
]);
|
||||
$largeur = filter_input(INPUT_GET, 'colonnes', FILTER_VALIDATE_INT, [
|
||||
"options" => [
|
||||
"default" => $default_colonnes,
|
||||
"min_range" => 2,
|
||||
"max_range" => 10
|
||||
]
|
||||
]);
|
||||
|
||||
$dico = [];
|
||||
if (($handle = fopen("dico.csv", "r")) !== FALSE) {
|
||||
$header = fgetcsv($handle, 0, "\t");
|
||||
while (($ligne = fgetcsv($handle, 0, "\t")) !== FALSE) {
|
||||
if (count($ligne) >= 2) {
|
||||
$mot = $ligne[0];
|
||||
$definition = $ligne[1];
|
||||
$dico[$mot] = $definition;
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
foreach ($mots_de_n_lettres as $n => $liste_mots) {
|
||||
shuffle($mots_de_n_lettres[$n]);
|
||||
}
|
||||
|
||||
$mots_par_position = [];
|
||||
foreach ([$hauteur, $largeur] as $n) {
|
||||
$mots_par_position[$n] = [];
|
||||
foreach ($mots_de_n_lettres[$n] as $mot) {
|
||||
foreach (str_split($mot) as $i => $lettre) {
|
||||
if (!isset($mots_par_position[$n][$i])) {
|
||||
$mots_par_position[$n][$i] = [];
|
||||
}
|
||||
if (!isset($mots_par_position[$n][$i][$lettre])) {
|
||||
$mots_par_position[$n][$i][$lettre] = [];
|
||||
}
|
||||
$mots_par_position[$n][$i][$lettre][] = $mot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function pire_contrainte($tests, $nb_lettres, $i, $mot)
|
||||
{
|
||||
global $mots_par_position;
|
||||
$nb_mots_min = PHP_INT_MAX;
|
||||
$pire_contrainte = 0;
|
||||
foreach ($tests as $test) {
|
||||
if (
|
||||
!array_key_exists($i, $mots_par_position[$nb_lettres]) ||
|
||||
!array_key_exists($mot[$test], $mots_par_position[$nb_lettres][$i])
|
||||
) {
|
||||
return -1;
|
||||
} else {
|
||||
$nb_mots = count($mots_par_position[$nb_lettres][$i][$mot[$test]]);
|
||||
if ($nb_mots < $nb_mots_min) {
|
||||
$pire_contrainte = $test;
|
||||
$nb_mots_min = $nb_mots;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $pire_contrainte;
|
||||
}
|
||||
|
||||
|
||||
class Grille
|
||||
{
|
||||
public $hauteur;
|
||||
public $largeur;
|
||||
public $grille;
|
||||
public $lignes_restantes;
|
||||
public $colonnes_restantes;
|
||||
|
||||
public function __construct($hauteur, $largeur)
|
||||
{
|
||||
$this->hauteur = $hauteur;
|
||||
$this->largeur = $largeur;
|
||||
$this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '.'));
|
||||
$this->lignes_restantes = range(0, $hauteur - 1);
|
||||
$this->colonnes_restantes = range(0, $largeur - 1);
|
||||
}
|
||||
|
||||
public function get_ligne($l)
|
||||
{
|
||||
return implode("", $this->grille[$l]);
|
||||
}
|
||||
|
||||
public function set_ligne($l, $mot)
|
||||
{
|
||||
for ($i = 0; $i < strlen($mot); $i++) {
|
||||
$this->grille[$l][$i] = $mot[$i];
|
||||
}
|
||||
}
|
||||
public function get_colonne($c)
|
||||
{
|
||||
$colonne = "";
|
||||
for ($i = 0; $i < $this->hauteur; $i++) {
|
||||
$colonne .= $this->grille[$i][$c];
|
||||
}
|
||||
return $colonne;
|
||||
}
|
||||
public function set_colonne($c, $mot)
|
||||
{
|
||||
for ($i = 0; $i < strlen($mot); $i++) {
|
||||
$this->grille[$i][$c] = $mot[$i];
|
||||
}
|
||||
}
|
||||
|
||||
public function genere()
|
||||
{
|
||||
global $mots_de_n_lettres;
|
||||
|
||||
$l = $this->largeur / 2;
|
||||
array_splice($this->lignes_restantes, $l, 1);
|
||||
foreach ($mots_de_n_lettres[$this->largeur] as $mot_lig) {
|
||||
$this->set_ligne($l, $mot_lig);
|
||||
yield from $this->trouve_une_colonne($l, $mot_lig);
|
||||
}
|
||||
$this->lignes_restantes[] = $l;
|
||||
$this->grille[$l] = array_fill(0, $this->largeur, '.');
|
||||
}
|
||||
|
||||
public function trouve_une_colonne($l, $mot_lig)
|
||||
{
|
||||
global $mots_par_position;
|
||||
|
||||
$c = pire_contrainte($this->colonnes_restantes, $this->hauteur, $l, $mot_lig);
|
||||
if ($c == -1) {
|
||||
return;
|
||||
}
|
||||
$colonne = $this->get_colonne($c);
|
||||
array_splice($this->colonnes_restantes, $c, 1);
|
||||
foreach ($mots_par_position[$this->hauteur][$l][$mot_lig[$c]] as $mot_col) {
|
||||
if ($mot_col == $colonne || preg_match("/^$colonne$/", $mot_col)) {
|
||||
$this->set_colonne($c, $mot_col);
|
||||
if (count($this->lignes_restantes)) {
|
||||
yield from $this->trouve_une_ligne($c, $mot_col);
|
||||
} else if (count($this->colonnes_restantes)) {
|
||||
yield from $this->trouve_une_colonne($l, $mot_lig);
|
||||
} else {
|
||||
yield;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->colonnes_restantes[] = $c;
|
||||
$this->set_colonne($c, $colonne);
|
||||
}
|
||||
|
||||
public function trouve_une_ligne($c, $mot_col)
|
||||
{
|
||||
global $mots_par_position;
|
||||
|
||||
$l = pire_contrainte($this->lignes_restantes, $this->largeur, $c, $mot_col);
|
||||
if ($l == -1) {
|
||||
return;
|
||||
}
|
||||
$ligne = $this->get_ligne($l);
|
||||
array_splice($this->lignes_restantes, $l, 1);
|
||||
foreach ($mots_par_position[$this->largeur][$c][$mot_col[$l]] as $mot_lig) {
|
||||
if ($mot_lig == $ligne || preg_match("/^$ligne$/", $mot_lig)) {
|
||||
$this->set_ligne($l, $mot_lig);
|
||||
if (count($this->colonnes_restantes)) {
|
||||
yield from $this->trouve_une_colonne($l, $mot_lig);
|
||||
} else if (count($this->lignes_restantes)) {
|
||||
yield from $this->trouve_une_ligne($c, $mot_col);
|
||||
} else {
|
||||
yield;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->lignes_restantes[] = $l;
|
||||
$this->set_ligne($l, $ligne);
|
||||
}
|
||||
|
||||
public function affiche()
|
||||
{
|
||||
echo "<table>";
|
||||
echo "<tr><th></th>";
|
||||
for ($c = 0; $c < $this->largeur; $c++) {
|
||||
echo "<th>" . chr($c + 65) . "</th>";
|
||||
}
|
||||
echo "</tr>";
|
||||
for ($l = 0; $l < $this->hauteur; $l++) {
|
||||
echo "<tr><th>" . $l . "</th>";
|
||||
for ($c = 0; $c < $this->largeur; $c++) {
|
||||
echo "<td>" . $this->grille[$l][$c] . "</td>";
|
||||
}
|
||||
echo "</tr>";
|
||||
}
|
||||
echo "</table>";
|
||||
}
|
||||
}
|
||||
|
||||
$grille = new Grille($hauteur, $largeur);
|
||||
$grille->genere()->current();
|
||||
|
||||
?>
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Mots croisés</title>
|
||||
<style>
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
td {
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
text-align: center;
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<table>
|
||||
<tr>
|
||||
<th></th>
|
||||
<?php for ($c = 0; $c < $largeur; $c++): ?>
|
||||
<th><?= chr($c + 65) ?></th>
|
||||
<?php endfor; ?>
|
||||
</tr>
|
||||
<?php for ($l = 0; $l < $hauteur; $l++): ?>
|
||||
<tr>
|
||||
<th><?= $l ?></th>
|
||||
<?php for ($c = 0; $c < $largeur; $c++): ?>
|
||||
<td><?= $grille->grille[$l][$c] ?></td>
|
||||
<?php endfor; ?>
|
||||
</tr>
|
||||
<?php endfor; ?>
|
||||
</table>
|
||||
|
||||
</html>
|
102
script.js
Normal file
102
script.js
Normal file
@ -0,0 +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("");
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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.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.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();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
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
86
service-worker.js
Normal 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.
|
||||
});
|
333
style.css
333
style.css
@ -1,45 +1,324 @@
|
||||
body {
|
||||
margin: 1rem;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: white;
|
||||
font-family: Times, "Times New Roman", Georgia, serif;
|
||||
}
|
||||
|
||||
|
||||
form {
|
||||
display: flex;
|
||||
padding: 1rem;
|
||||
min-height: calc(100vh - 2rem);
|
||||
flex-flow: column;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
letter-spacing: 0.2em;
|
||||
}
|
||||
|
||||
h1.large.width {
|
||||
display: inherit;
|
||||
padding-top: 1rem;
|
||||
}
|
||||
|
||||
h1.small.width {
|
||||
display: none;
|
||||
}
|
||||
|
||||
h1 table {
|
||||
margin: auto;
|
||||
line-height: 0.8;
|
||||
}
|
||||
|
||||
h1 td {
|
||||
width: 0.7em;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
h1,
|
||||
h2 {
|
||||
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;
|
||||
gap: 1em;
|
||||
}
|
||||
|
||||
.grille {
|
||||
margin: 2rem auto;
|
||||
border-collapse: collapse;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
th, td {
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
text-align: center;
|
||||
.grille table {
|
||||
border-collapse: collapse;
|
||||
margin: auto;
|
||||
height: fit-content;
|
||||
}
|
||||
|
||||
td {
|
||||
border: 1px solid black;
|
||||
padding: 2px;
|
||||
.grille th,
|
||||
.grille td {
|
||||
width: 2rem;
|
||||
height: 2rem;
|
||||
text-align: center;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.case.noire {
|
||||
background-color: black;
|
||||
.grille td {
|
||||
width: 2rem;
|
||||
height: 2rem;
|
||||
border: 1px solid black;
|
||||
padding: 2px;
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
input {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border: none;
|
||||
padding: 0;
|
||||
text-align: center;
|
||||
font-size: 1em;
|
||||
.grille tr:nth-of-type(2) td {
|
||||
border-top-width: 3px;
|
||||
}
|
||||
.grille tr:last-of-type td {
|
||||
border-bottom-width: 3px;
|
||||
}
|
||||
.grille td:first-of-type {
|
||||
border-left-width: 3px;
|
||||
}
|
||||
.grille td:last-child {
|
||||
border-right-width: 3px;
|
||||
}
|
||||
|
||||
input[disabled] {
|
||||
background-color: black;
|
||||
.grille .case.noire {
|
||||
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;
|
||||
}
|
||||
|
||||
.grille input[disabled] {
|
||||
background-color: black;
|
||||
}
|
||||
|
||||
.grille input::placeholder {
|
||||
color: transparent;
|
||||
}
|
||||
|
||||
.definitions {
|
||||
display: flex;
|
||||
justify-content: space-evenly;
|
||||
width: 30%;
|
||||
}
|
||||
|
||||
.definitions h2 {
|
||||
font-variant-caps: petite-caps;
|
||||
}
|
||||
.definitions.horizontales {
|
||||
order: -1;
|
||||
}
|
||||
|
||||
.definitions ol {
|
||||
padding-left: 2em;
|
||||
}
|
||||
|
||||
.definitions > div > ol > li::marker {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.definitions li ol {
|
||||
padding-left: 0em;
|
||||
list-style: parenthese;
|
||||
}
|
||||
|
||||
@counter-style parenthese {
|
||||
system: extends decimal;
|
||||
suffix: ") ";
|
||||
}
|
||||
|
||||
.definitions li li {
|
||||
margin-left: 0.8em;
|
||||
counter-increment: count;
|
||||
}
|
||||
|
||||
.definitions li li::marker {
|
||||
font-size: small;
|
||||
}
|
||||
|
||||
.definitions em {
|
||||
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;
|
||||
}
|
||||
|
||||
.nouvelle-grille img {
|
||||
margin: 0 0.5em;
|
||||
}
|
||||
|
||||
.nouvelle-grille {
|
||||
margin: 1em auto 0 auto;
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
}
|
||||
|
||||
.nouvelle-grille button,
|
||||
.nouvelle-grille input {
|
||||
border: none;
|
||||
background: none;
|
||||
color: inherit;
|
||||
font-family: inherit;
|
||||
font-size: 1em;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.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) {
|
||||
h1.large.width {
|
||||
display: none;
|
||||
}
|
||||
|
||||
h1.small.width {
|
||||
display: initial;
|
||||
}
|
||||
|
||||
h1 table {
|
||||
line-height: 0.7;
|
||||
}
|
||||
|
||||
.grille {
|
||||
width: 100%;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
.definitions {
|
||||
width: 45%;
|
||||
}
|
||||
|
||||
.definitions.horizontales {
|
||||
order: inherit;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
width: auto;
|
||||
margin: 0;
|
||||
padding: 1rem 0;
|
||||
}
|
||||
|
||||
h1.large.width {
|
||||
display: none;
|
||||
}
|
||||
|
||||
h1.small.width {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.grille td {
|
||||
width: 2.5rem;
|
||||
height: 2.5rem;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 1.2em;
|
||||
margin: 1em 0 0.5em 0;
|
||||
}
|
||||
|
||||
.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) {
|
||||
html {
|
||||
-webkit-text-size-adjust: 100%;
|
||||
-ms-text-size-adjust: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
body,
|
||||
button {
|
||||
background-color: #02081a;
|
||||
color: #c6c6c6;
|
||||
}
|
||||
|
||||
.grille td,
|
||||
.grille input {
|
||||
background-color: #edeeee;
|
||||
}
|
||||
|
||||
.nouvelle-grille button:hover {
|
||||
color: #479fec;
|
||||
}
|
||||
|
||||
.nouvelle-grille button:active {
|
||||
color: orchid;
|
||||
}
|
||||
}
|
||||
|
181
test0.py
181
test0.py
@ -1,181 +0,0 @@
|
||||
import csv
|
||||
from re import compile, match
|
||||
from random import choice, sample, randrange
|
||||
from collections import defaultdict
|
||||
from math import ceil
|
||||
from itertools import product, chain
|
||||
|
||||
|
||||
dico = defaultdict(list)
|
||||
with open("dico.csv", "r", encoding="utf-8") as fichier:
|
||||
for mot, definition in csv.reader(fichier, delimiter="\t"):
|
||||
if not mot.startswith("#"):
|
||||
dico[mot].append(definition)
|
||||
|
||||
mots = defaultdict(set)
|
||||
for mot in dico:
|
||||
mots[len(mot)].add(mot)
|
||||
|
||||
def melange(iterable):
|
||||
liste = list(iterable)
|
||||
return sample(liste, len(liste))
|
||||
|
||||
def mots_de_n_lettres(n):
|
||||
for mot in mots[n]:
|
||||
yield mot
|
||||
# for mot in mots[n-1]:
|
||||
# yield f"{mot} "
|
||||
# yield f" {mot}"
|
||||
for i in range(2, ceil(n / 2)):
|
||||
for mot1, mot2 in product(mots[i], mots_de_n_lettres(n - i - 1)):
|
||||
yield f"{mot1} {mot2}"
|
||||
yield f"{mot2} {mot1}"
|
||||
# for mot1, mot2 in product(mots[i], mots_de_n_lettres(n - i - 2)):
|
||||
# yield f" {mot1} {mot2}"
|
||||
# yield f"{mot2} {mot1} "
|
||||
# for mot1, mot2 in product(mots[i-1], mots_de_n_lettres(n - i - 1)):
|
||||
# yield f" {mot1} {mot2}"
|
||||
# yield f"{mot2} {mot1} "
|
||||
|
||||
|
||||
class Ligne:
|
||||
def __init__(self, grille):
|
||||
self.grille = grille
|
||||
|
||||
def __getitem__(self, n):
|
||||
return "".join(self.grille[n])
|
||||
|
||||
def __setitem__(self, n, mot):
|
||||
self.grille[n] = list(mot)
|
||||
|
||||
|
||||
class Colonne:
|
||||
def __init__(self, grille):
|
||||
self.grille = grille
|
||||
|
||||
def __getitem__(self, n):
|
||||
return "".join(ligne[n] for ligne in self.grille)
|
||||
|
||||
def __setitem__(self, n, mot):
|
||||
for i, char in enumerate(mot):
|
||||
self.grille[i][n] = char
|
||||
|
||||
|
||||
class Grille:
|
||||
def __init__(self, hauteur, largeur):
|
||||
self.hauteur = hauteur
|
||||
self.largeur = largeur
|
||||
self.grille = [["." for _ in range(largeur)] for _ in range(hauteur)]
|
||||
self.ligne = Ligne(self.grille)
|
||||
self.colonne = Colonne(self.grille)
|
||||
|
||||
self.mots_de_n_lettres = {
|
||||
hauteur: set(mots_de_n_lettres(hauteur)),
|
||||
largeur: set(mots_de_n_lettres(largeur)),
|
||||
}
|
||||
self.mots_par_position = defaultdict(lambda: defaultdict(list))
|
||||
for nb_lettres in (self.largeur, self.hauteur):
|
||||
for mot in self.mots_de_n_lettres[nb_lettres]:
|
||||
for i, lettre in enumerate(mot):
|
||||
self.mots_par_position[nb_lettres][(i, lettre)].append(mot)
|
||||
|
||||
self.generations = self.genere()
|
||||
try:
|
||||
next(self)
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
return next(self.generations)
|
||||
|
||||
def genere(self):
|
||||
self.lignes_restantes = set(range(self.hauteur))
|
||||
self.colonnes_restantes = set(range(self.largeur))
|
||||
|
||||
l = 0
|
||||
self.lignes_restantes.remove(l)
|
||||
for mot_lig in self.mots_de_n_lettres[self.largeur]:
|
||||
if ' ' in mot_lig:
|
||||
continue
|
||||
self.ligne[l] = mot_lig
|
||||
yield from self.trouve_une_colonne(l, mot_lig)
|
||||
#self.ligne[l] = "." * self.largeur
|
||||
#self.lignes_restantes.add(l)
|
||||
|
||||
def trouve_une_colonne(self, l, mot_lig):
|
||||
#print((len(self.colonnes_restantes) + len(self.lignes_restantes)) / (self.largeur + self.hauteur))
|
||||
#print(self)
|
||||
c = min(
|
||||
self.colonnes_restantes,
|
||||
key=lambda c: len(self.mots_par_position[self.hauteur][(l, mot_lig[c])])
|
||||
)
|
||||
if not self.mots_par_position[self.hauteur][(l, mot_lig[c])]:
|
||||
return
|
||||
colonne = self.colonne[c]
|
||||
self.colonnes_restantes.remove(c)
|
||||
pattern = compile(rf"\b{colonne}\b")
|
||||
for mot_col in self.mots_par_position[self.hauteur][(l, mot_lig[c])]:
|
||||
if colonne == mot_col or ('.' in colonne and pattern.match(mot_col)):
|
||||
self.colonne[c] = mot_col
|
||||
if self.lignes_restantes:
|
||||
yield from self.trouve_une_ligne(c, mot_col)
|
||||
elif self.colonnes_restantes:
|
||||
yield from self.trouve_une_colonne(l, mot_lig)
|
||||
else:
|
||||
yield self
|
||||
self.colonne[c] = colonne
|
||||
self.colonnes_restantes.add(c)
|
||||
|
||||
def trouve_une_ligne(self, c, mot_col):
|
||||
l = min(
|
||||
self.lignes_restantes,
|
||||
key=lambda l: len(self.mots_par_position[self.largeur][(c, mot_col[l])])
|
||||
)
|
||||
if not self.mots_par_position[self.largeur][(c, mot_col[l])]:
|
||||
return
|
||||
ligne = self.ligne[l]
|
||||
self.lignes_restantes.remove(l)
|
||||
pattern = compile(rf"\b{ligne}\b")
|
||||
for mot_lig in self.mots_par_position[self.largeur][(c, mot_col[l])]:
|
||||
if ligne == mot_lig or ('.' in ligne and pattern.match(mot_lig)):
|
||||
self.ligne[l] = mot_lig
|
||||
if self.colonnes_restantes:
|
||||
yield from self.trouve_une_colonne(l, mot_lig)
|
||||
elif self.lignes_restantes:
|
||||
yield from self.trouve_une_ligne(c, mot_col)
|
||||
else:
|
||||
yield self
|
||||
self.ligne[l] = ligne
|
||||
self.lignes_restantes.add(l)
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
" "
|
||||
+ " ".join(chr(65 + i) for i in range(self.largeur))
|
||||
+ "\n"
|
||||
+ "\n".join(
|
||||
f"{i + 1:2} " + " ".join(ligne) for i, ligne in enumerate(self.grille)
|
||||
)
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import time
|
||||
|
||||
class Timer:
|
||||
def __enter__(self):
|
||||
self.start = time.time()
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc_info):
|
||||
end = time.time()
|
||||
print(f"Execution time: {end - self.start:.2f} seconds")
|
||||
|
||||
with Timer():
|
||||
print(Grille(5, 5))
|
144
test1.py
144
test1.py
@ -1,144 +0,0 @@
|
||||
import csv
|
||||
from re import compile, match
|
||||
from random import choice, sample, randrange
|
||||
from collections import defaultdict
|
||||
from math import ceil
|
||||
from itertools import product, chain
|
||||
|
||||
|
||||
dico = defaultdict(list)
|
||||
with open("dico.csv", "r", encoding="utf-8") as fichier:
|
||||
for mot, definition in csv.reader(fichier, delimiter="\t"):
|
||||
if not mot.startswith("#"):
|
||||
dico[mot].append(definition)
|
||||
|
||||
mots_de_n_lettres = defaultdict(set)
|
||||
for mot in dico:
|
||||
mots_de_n_lettres[len(mot)].add(mot)
|
||||
|
||||
|
||||
def mots_espaces(n):
|
||||
for mot in mots_de_n_lettres[n]:
|
||||
yield mot
|
||||
# for mot in mots_de_n_lettres[n-1]:
|
||||
# yield f"{mot} "
|
||||
# yield f" {mot}"
|
||||
for i in range(1, ceil(n / 2)):
|
||||
for mot1, mot2 in product(mots_de_n_lettres[i], mots_espaces(n - i - 1)):
|
||||
yield f"{mot1} {mot2}"
|
||||
yield f"{mot2} {mot1}"
|
||||
# for mot1, mot2 in product(mots_de_n_lettres[i], mots_espaces(n - i - 2)):
|
||||
# yield f" {mot1} {mot2}"
|
||||
# yield f"{mot2} {mot1} "
|
||||
# for mot1, mot2 in product(mots_de_n_lettres[i-1], mots_espaces(n - i - 1)):
|
||||
# yield f" {mot1} {mot2}"
|
||||
# yield f"{mot2} {mot1} "
|
||||
|
||||
|
||||
class Ligne:
|
||||
def __init__(self, grille):
|
||||
self.grille = grille
|
||||
|
||||
def __getitem__(self, n):
|
||||
return "".join(self.grille[n])
|
||||
|
||||
def __setitem__(self, n, mot):
|
||||
self.grille[n] = list(mot)
|
||||
|
||||
|
||||
class Colonne:
|
||||
def __init__(self, grille):
|
||||
self.grille = grille
|
||||
|
||||
def __getitem__(self, n):
|
||||
return "".join(ligne[n] for ligne in self.grille)
|
||||
|
||||
def __setitem__(self, n, mot):
|
||||
for i, char in enumerate(mot):
|
||||
self.grille[i][n] = char
|
||||
|
||||
|
||||
class Grille:
|
||||
def __init__(self, hauteur, largeur):
|
||||
self.hauteur = hauteur
|
||||
self.largeur = largeur
|
||||
self.grille = [["." for _ in range(largeur)] for _ in range(hauteur)]
|
||||
self.ligne = Ligne(self.grille)
|
||||
self.colonne = Colonne(self.grille)
|
||||
|
||||
self.mots_commencant_par = defaultdict(lambda: defaultdict(list))
|
||||
for dimension in (hauteur,) if hauteur == largeur else (hauteur, largeur):
|
||||
for mot in mots_espaces(dimension):
|
||||
for i in range(dimension+1):
|
||||
self.mots_commencant_par[dimension][mot[:i]].append(mot)
|
||||
|
||||
self.grilles = self.genere_grilles()
|
||||
next(self.grilles)
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
return next(self.grilles)
|
||||
|
||||
def genere_grilles(self):
|
||||
print(f"Grille({self.hauteur}, {self.largeur})")
|
||||
yield from self.trouve_une_ligne(0)
|
||||
|
||||
def trouve_une_ligne(self, l):
|
||||
for mot in self.mots_commencant_par[self.largeur][self.ligne[l][:l]]:
|
||||
self.ligne[l] = mot
|
||||
if all(
|
||||
self.colonne[c][:l+1] in self.mots_commencant_par[self.hauteur]
|
||||
for c in range(l, self.largeur)
|
||||
):
|
||||
if l < self.largeur:
|
||||
yield from self.trouve_une_colonne(l)
|
||||
elif l + 1 < self.hauteur:
|
||||
yield from self.trouve_une_ligne(l + 1)
|
||||
else:
|
||||
yield self
|
||||
|
||||
def trouve_une_colonne(self, c):
|
||||
for mot in self.mots_commencant_par[self.hauteur][self.colonne[c][:c+1]]:
|
||||
self.colonne[c] = mot
|
||||
if all(
|
||||
self.ligne[l][:c+1] in self.mots_commencant_par[self.largeur]
|
||||
for l in range(c, self.largeur)
|
||||
):
|
||||
if c + 1 < self.hauteur:
|
||||
yield from self.trouve_une_ligne(c + 1)
|
||||
elif c + 1 < self.largeur:
|
||||
yield from self.trouve_une_colonne(c + 1)
|
||||
else:
|
||||
yield self
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
" "
|
||||
+ " ".join(chr(65 + i) for i in range(self.largeur))
|
||||
+ "\n"
|
||||
+ "\n".join(
|
||||
f"{i + 1:2} " + " ".join(ligne) for i, ligne in enumerate(self.grille)
|
||||
)
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import time
|
||||
|
||||
class Timer:
|
||||
def __enter__(self):
|
||||
self.start = time.time()
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc_info):
|
||||
end = time.time()
|
||||
print(f"Execution time: {end - self.start:.2f} seconds")
|
||||
|
||||
for n in range(2, 14):
|
||||
with Timer():
|
||||
print(Grille(n, n))
|
136
test2.py
136
test2.py
@ -1,136 +0,0 @@
|
||||
import csv
|
||||
from re import compile, match
|
||||
from random import choice, sample, randrange
|
||||
from collections import defaultdict
|
||||
from math import ceil
|
||||
from itertools import product, chain
|
||||
|
||||
|
||||
dico = defaultdict(list)
|
||||
with open("dico.csv", "r", encoding="utf-8") as fichier:
|
||||
for mot, definition in csv.reader(fichier, delimiter="\t"):
|
||||
if not mot.startswith("#"):
|
||||
dico[mot].append(definition)
|
||||
|
||||
mots_de_n_lettres = defaultdict(set)
|
||||
for mot in dico:
|
||||
mots_de_n_lettres[len(mot)].add(mot)
|
||||
|
||||
|
||||
def mots_espaces(n):
|
||||
for mot in mots_de_n_lettres[n]:
|
||||
yield mot
|
||||
# for mot in mots_de_n_lettres[n-1]:
|
||||
# yield f"{mot} "
|
||||
# yield f" {mot}"
|
||||
for i in range(2, ceil(n / 2)):
|
||||
for mot1, mot2 in product(mots_de_n_lettres[i], mots_espaces(n - i - 1)):
|
||||
yield f"{mot1} {mot2}"
|
||||
yield f"{mot2} {mot1}"
|
||||
# for mot1, mot2 in product(mots_de_n_lettres[i], mots_espaces(n - i - 2)):
|
||||
# yield f" {mot1} {mot2}"
|
||||
# yield f"{mot2} {mot1} "
|
||||
# for mot1, mot2 in product(mots_de_n_lettres[i-1], mots_espaces(n - i - 1)):
|
||||
# yield f" {mot1} {mot2}"
|
||||
# yield f"{mot2} {mot1} "
|
||||
|
||||
|
||||
class Ligne:
|
||||
def __init__(self, grille):
|
||||
self.grille = grille
|
||||
|
||||
def __getitem__(self, n):
|
||||
return "".join(self.grille[n])
|
||||
|
||||
def __setitem__(self, n, mot):
|
||||
self.grille[n] = list(mot)
|
||||
|
||||
|
||||
class Colonne:
|
||||
def __init__(self, grille):
|
||||
self.grille = grille
|
||||
|
||||
def __getitem__(self, n):
|
||||
return "".join(ligne[n] for ligne in self.grille)
|
||||
|
||||
def __setitem__(self, n, mot):
|
||||
for i, char in enumerate(mot):
|
||||
self.grille[i][n] = char
|
||||
|
||||
|
||||
class Grille:
|
||||
def __init__(self, hauteur, largeur):
|
||||
self.hauteur = hauteur
|
||||
self.largeur = largeur
|
||||
self.grille = [["." for _ in range(largeur)] for _ in range(hauteur)]
|
||||
self.ligne = Ligne(self.grille)
|
||||
self.colonne = Colonne(self.grille)
|
||||
|
||||
self.mot_commencant_par = defaultdict(lambda: defaultdict(list))
|
||||
self.mots_finissant_par = defaultdict(lambda: defaultdict(list))
|
||||
for dimension in (hauteur,) if hauteur == largeur else (hauteur, largeur):
|
||||
for mot in mots_espaces(dimension):
|
||||
for i in range(dimension+1):
|
||||
self.mot_commencant_par[dimension][mot[:i]].append(mot)
|
||||
self.mots_finissant_par[dimension][mot[i:]].append(mot)
|
||||
|
||||
self.grilles = self.genere_grilles()
|
||||
next(self.grilles)
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
return next(self.grilles)
|
||||
|
||||
def genere_grilles(self):
|
||||
yield from self.trouve_une_ligne(0)
|
||||
|
||||
def trouve_une_ligne(self, i):
|
||||
for mot in self.mots_finissant_par[self.largeur][self.ligne[i][self.largeur-i:]]:
|
||||
self.ligne[i] = mot
|
||||
if i < self.largeur:
|
||||
yield from self.trouve_une_colonne(i)
|
||||
elif i + 1 < self.hauteur:
|
||||
yield from self.trouve_une_ligne(i + 1)
|
||||
else:
|
||||
yield self
|
||||
|
||||
def trouve_une_colonne(self, i):
|
||||
for mot in self.mot_commencant_par[self.hauteur][self.colonne[self.largeur-1-i][:i+1]]:
|
||||
self.colonne[self.largeur-1-i] = mot
|
||||
if i + 1 < self.hauteur:
|
||||
yield from self.trouve_une_ligne(i + 1)
|
||||
elif i + 1 < self.largeur:
|
||||
yield from self.trouve_une_colonne(i + 1)
|
||||
else:
|
||||
yield self
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
" "
|
||||
+ " ".join(chr(65 + i) for i in range(self.largeur))
|
||||
+ "\n"
|
||||
+ "\n".join(
|
||||
f"{i + 1:2} " + " ".join(ligne) for i, ligne in enumerate(self.grille)
|
||||
)
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import time
|
||||
|
||||
class Timer:
|
||||
def __enter__(self):
|
||||
self.start = time.time()
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc_info):
|
||||
end = time.time()
|
||||
print(f"Execution time: {end - self.start:.2f} seconds")
|
||||
|
||||
with Timer():
|
||||
print(Grille(5, 5))
|
BIN
thumbnail.png
Normal file
BIN
thumbnail.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.9 KiB |
Reference in New Issue
Block a user