Compare commits

...

2 Commits

Author SHA1 Message Date
17e807674e tests python 2025-04-23 17:02:38 +02:00
7dc6c0246e class Grille 2025-04-23 09:35:51 +02:00
4 changed files with 305 additions and 138 deletions

Binary file not shown.

289
index.php
View File

@ -1,4 +1,8 @@
<?php
ini_set('display_errors', 1);
ini_set('html_errors', 1);
ini_set('error_reporting', E_ALL);
$default_lignes = 3;
$default_colonnes = 4;
@ -30,100 +34,48 @@ if (($handle = fopen("dico.csv", "r")) !== FALSE) {
fclose($handle);
}
$mots = [];
$mots_de_n_lettres = [];
foreach ($dico as $mot => $definition) {
$nb_lettres = strlen($mot);
if(!isset($mots[$nb_lettres])) {
$mots[$nb_lettres] = [];
$n = strlen($mot);
if (!isset($mots_de_n_lettres[$n])) {
$mots_de_n_lettres[$n] = [];
}
$mots[$nb_lettres][] = $mot;
$mots_de_n_lettres[$n][] = $mot;
}
foreach ($mots as $nb_lettres => $liste_mots) {
shuffle($mots[$nb_lettres]);
foreach ($mots_de_n_lettres as $n => $liste_mots) {
shuffle($mots_de_n_lettres[$n]);
}
$dimensions = [$hauteur, $largeur];
$mots_par_position = [];
foreach($dimensions as $nb_lettres) {
$mots_par_position[$nb_lettres] = [];
foreach($mots[$nb_lettres] as $mot) {
foreach(str_split($mot) as $i => $lettre) {
if (!isset($mots_par_position[$nb_lettres][$i])) {
$mots_par_position[$nb_lettres][$i] = [];
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[$nb_lettres][$i][$lettre])) {
$mots_par_position[$nb_lettres][$i][$lettre] = [];
if (!isset($mots_par_position[$n][$i][$lettre])) {
$mots_par_position[$n][$i][$lettre] = [];
}
$mots_par_position[$nb_lettres][$i][$lettre][] = $mot;
$mots_par_position[$n][$i][$lettre][] = $mot;
}
}
}
$grille = [];
for($l = 0; $l < $hauteur; $l++) {
$grille[$l] = array_fill(0, $largeur, '.');
}
function get_ligne($l) {
global $grille;
return implode("", $grille[$l]);
}
function set_ligne($l, $mot) {
global $grille;
for($i = 0; $i < strlen($mot); $i++) {
$grille[$l][$i] = $mot[$i];
}
}
function get_colonne($c) {
global $grille;
$colonne = "";
for($i = 0; $i < count($grille); $i++) {
$colonne .= $grille[$i][$c];
}
return $colonne;
}
function set_colonne($c, $mot) {
global $grille;
for($i = 0; $i < strlen($mot); $i++) {
$grille[$i][$c] = $mot[$i];
}
}
$lignes_restantes = range(0, $hauteur-1);
$colonnes_restantes = range(0, $largeur-1);
function genere() {
global $mots;
global $grille;
global $lignes_restantes;
global $largeur;
$l = $largeur / 2;
array_splice($lignes_restantes, $l, 1);
foreach($mots[$largeur] as $mot_lig) {
set_ligne($l, $mot_lig);
yield from trouve_une_colonne($l, $mot_lig);
}
$lignes_restantes[] = $l;
$grille[$l] = array_fill(0, $largeur, '.');
}
function pire_contrainte($tests, $nb_lettres, $i, $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(
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) {
if ($nb_mots < $nb_mots_min) {
$pire_contrainte = $test;
$nb_mots_min = $nb_mots;
}
@ -132,69 +84,141 @@ function pire_contrainte($tests, $nb_lettres, $i, $mot) {
return $pire_contrainte;
}
function trouve_une_colonne($l, $mot_lig) {
global $grille;
global $colonnes_restantes;
global $lignes_restantes;
global $hauteur;
global $mots_par_position;
$c = pire_contrainte($colonnes_restantes, $hauteur, $l, $mot_lig);
if ($c == -1) {
return;
}
$colonne = get_colonne($c);
array_splice($colonnes_restantes, $c, 1);
foreach ($mots_par_position[$hauteur][$l][$mot_lig[$c]] as $mot_col) {
if ($mot_col == $colonne || preg_match("/^$colonne$/", $mot_col)) {
set_colonne($c, $mot_col);
if (count($lignes_restantes)) {
yield from trouve_une_ligne($c, $mot_col);
} else if (count($colonnes_restantes)) {
yield from trouve_une_colonne($l, $mot_lig);
} else {
yield;
}
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];
}
}
$colonnes_restantes[] = $c;
set_colonne($c, $colonne);
}
function trouve_une_ligne($c, $mot_col) {
global $grille;
global $colonnes_restantes;
global $lignes_restantes;
global $largeur;
global $mots_par_position;
$l = pire_contrainte($lignes_restantes, $largeur, $c, $mot_col);
if ($l == -1) {
return;
public function get_colonne($c)
{
$colonne = "";
for ($i = 0; $i < $this->hauteur; $i++) {
$colonne .= $this->grille[$i][$c];
}
return $colonne;
}
$ligne = get_ligne($l);
array_splice($lignes_restantes, $l, 1);
foreach ($mots_par_position[$largeur][$c][$mot_col[$l]] as $mot_lig) {
if ($mot_lig == $ligne || preg_match("/^$ligne$/", $mot_lig)) {
set_ligne($l, $mot_lig);
if (count($colonnes_restantes)) {
yield from trouve_une_colonne($l, $mot_lig);
} else if (count($lignes_restantes)) {
yield from trouve_une_ligne($c, $mot_col);
} else {
yield;
}
public function set_colonne($c, $mot)
{
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$i][$c] = $mot[$i];
}
}
$lignes_restantes[] = $l;
set_ligne($l, $ligne);
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>";
}
}
genere()->current();
$grille = new Grille($hauteur, $largeur);
$grille->genere()->current();
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Mots croisés</title>
@ -202,6 +226,7 @@ genere()->current();
table {
border-collapse: collapse;
}
td {
width: 30px;
height: 30px;
@ -210,21 +235,23 @@ genere()->current();
}
</style>
</head>
<body>
<table>
<tr>
<th></th>
<?php for($c=0; $c<$largeur; $c++): ?>
<th><?=chr($c+65)?></th>
<?php for ($c = 0; $c < $largeur; $c++): ?>
<th><?= chr($c + 65) ?></th>
<?php endfor; ?>
</tr>
<?php for($l=0; $l<$hauteur; $l++): ?>
<?php for ($l = 0; $l < $hauteur; $l++): ?>
<tr>
<th><?=$l?></th>
<?php for($c=0; $c<$largeur; $c++): ?>
<td><?=$grille[$l][$c]?></td>
<th><?= $l ?></th>
<?php for ($c = 0; $c < $largeur; $c++): ?>
<td><?= $grille->grille[$l][$c] ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table>
</html>

14
test.py
View File

@ -21,9 +21,9 @@ def melange(iterable):
return sample(liste, len(liste))
def mots_de_n_lettres(n):
for mot in melange(mots[n]):
for mot in mots[n]:
yield mot
for mot in melange(mots[n-1]):
for mot in mots[n-1]:
yield f"{mot} "
yield f" {mot}"
for i in range(2, ceil(n / 2)):
@ -54,7 +54,7 @@ class Colonne:
self.grille = grille
def __getitem__(self, n):
return "".join(row[n] for row in self.grille)
return "".join(ligne[n] for ligne in self.grille)
def __setitem__(self, n, mot):
for i, char in enumerate(mot):
@ -70,8 +70,8 @@ class 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)),
hauteur: melange(mots_de_n_lettres(hauteur)),
largeur: melange(mots_de_n_lettres(largeur)),
}
self.mots_par_position = defaultdict(lambda: defaultdict(set))
for nb_lettres in (self.largeur, self.hauteur):
@ -98,8 +98,8 @@ class Grille:
l = 0
self.lignes_restantes.remove(l)
for mot_lig in self.mots_de_n_lettres[self.largeur]:
if ' ' in mot_lig:
continue
# if ' ' in mot_lig:
# continue
self.ligne[l] = mot_lig
yield from self.trouve_une_colonne(l, mot_lig)
self.ligne[l] = "." * self.largeur

140
test2.py Normal file
View File

@ -0,0 +1,140 @@
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 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][: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[:n+1])
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))
for dimension in (hauteur,) if hauteur == largeur else (hauteur, largeur):
for mot in mots_de_n_lettres(dimension):
for i in range(dimension+1):
self.mot_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, i):
for mot in self.mot_commencant_par[self.largeur][self.ligne[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[i]]:
self.colonne[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")
for i in range(2, 14):
with Timer():
print(Grille(i, i))
with Timer():
print(Grille(i + 1, i))
with Timer():
print(Grille(i, i + 1))