ajustement de mots_espaces

This commit is contained in:
Adrien MALINGREY 2025-04-25 02:11:21 +02:00
parent 64bc91d4a0
commit 4b69fa4803
5 changed files with 50 additions and 37 deletions

View File

@ -3,9 +3,6 @@
include_once "dico.php";
const MIN_LETTRES = 1;
class Grille {
public $grille;
public $hauteur;
@ -19,15 +16,16 @@ class Grille {
$this->largeur = $largeur;
$this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '.'));
if ($hauteur == $largeur) {
$dimensions = [$hauteur];
if ($id == "") {
mt_srand();
} else {
$dimensions = [$hauteur, $largeur];
mt_srand(crc32($id));
}
$this->mots_commencant_par = [];
foreach ($dimensions as $longueur) {
foreach ($hauteur == $largeur? [$hauteur]: [$hauteur, $largeur] as $longueur) {
$this->mots_commencant_par[$longueur] = [];
foreach(mots_espaces($longueur, MIN_LETTRES, crc32($id)) as $mot) {
$nb_mots = 0;
foreach(mots_espaces($longueur) as $mot) {
for ($i = 0; $i <= $longueur; $i++) {
$debut = substr($mot, 0, $i);
if (!isset($this->mots_commencant_par[$longueur][$debut])) {
@ -37,6 +35,8 @@ class Grille {
}
}
}
mt_srand();
$this->grilles = $this->generateur();
$this->grilles->current();
}

View File

@ -1,4 +1,5 @@
#MOT Définition
A
ABAT Tripe ou étripe. (Robert Scipion)
ABATTIS Architecte à la retraite

1 #MOT Définition
2
3 A
4 ABAT Tripe ou étripe. (Robert Scipion)
5 ABATTIS Architecte à la retraite

View File

@ -1,5 +1,10 @@
<?php
const MIN_LETTRES = 0;
const MAX_MOTS = 100000;
$dico = [];
if (($handle = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($handle, 0, "\t");
@ -13,7 +18,7 @@ if (($handle = fopen("dico.csv", "r")) !== FALSE) {
fclose($handle);
}
$mots_de_n_lettres = [[]];
$mots_de_n_lettres = [];
foreach ($dico as $mot => $definition) {
$n = strlen($mot);
if (!isset($mots_de_n_lettres[$n])) {
@ -22,39 +27,41 @@ foreach ($dico as $mot => $definition) {
$mots_de_n_lettres[$n][] = $mot;
}
function fisherYatesShuffle(&$items, $seed)
function fisherYatesShuffle(&$items)
{
mt_srand($seed);
for ($i = count($items) - 1; $i > 0; $i--)
{
$j = @mt_rand(0, $i);
for ($i = count($items) - 1; $i > 0; $i--) {
$j = mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
mt_srand();
}
function mots_espaces($max, $min=0, $seed=0) {
function mots_espaces($longueur) {
global $mots_de_n_lettres;
global $dico;
if ($seed) {
fisherYatesShuffle($mots_de_n_lettres[$max], $seed);
} else {
shuffle($mots_de_n_lettres[$max]);
}
foreach($mots_de_n_lettres[$max] as $mot) {
$nb_mots = 0;
fisherYatesShuffle($mots_de_n_lettres[$longueur]);
foreach($mots_de_n_lettres[$longueur] as $mot) {
yield $mot;
$nb_mots++;
if ($nb_mots > MAX_MOTS) {
return;
}
for ($i = ceil($max / 2); $max - $i -1 >= $min; $i++) {
}
for ($i = 2; $longueur - $i - 1 >= MIN_LETTRES; $i++) {
foreach ($mots_de_n_lettres[$i] as $mot1) {
foreach (mots_espaces($max - $i -1, $min) as $mot2) {
foreach (mots_espaces($longueur - $i - 1) as $mot2) {
if ($mot1 != $mot2) {
$dico["$mot1 $mot2"] = $dico[$mot1] && $dico[$mot2] ? "{$dico[$mot1]}<br/>{$dico[$mot2]}." : $dico[$mot1] . $dico[$mot2];
yield "$mot1 $mot2";
$dico["$mot2 $mot1"] = $dico[$mot2] && $dico[$mot1] ? "{$dico[$mot2]}<br/>{$dico[$mot1]}." : $dico[$mot2] . $dico[$mot1];
yield "$mot2 $mot1";
$nb_mots += 2;
if ($nb_mots > MAX_MOTS) {
break;
}
}
}
}

View File

@ -1,18 +1,14 @@
<?php
const HAUTEUR_PAR_DEFAUT = 6;
const LARGEUR_PAR_DEFAUT = 6;
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
$id = filter_input(INPUT_GET, 'grille', FILTER_VALIDATE_REGEXP, [
"options" => [
"regexp" => "/^[a-f0-9]{13}$/"
]
]);
if (!$id) {
if (!isset($_GET["grille"])) {
$_GET["grille"] = uniqid();
header("Location: " . $_SERVER['DOCUMENT_URI'] . "?" . http_build_query($_GET));
header("Location: " . dirname($_SERVER['DOCUMENT_URI']) . "?" . http_build_query($_GET));
exit;
} else {
$id = htmlspecialchars($_GET["grille"]);
}
@ -20,6 +16,10 @@ include_once "dico.php";
include_once "Grille.php";
const HAUTEUR_PAR_DEFAUT = 6;
const LARGEUR_PAR_DEFAUT = 6;
$hauteur = filter_input(INPUT_GET, 'lignes', FILTER_VALIDATE_INT, [
"options" => [
"default" => HAUTEUR_PAR_DEFAUT,
@ -119,8 +119,8 @@ $grille = new Grille($hauteur, $largeur, $id);
</div>
</div>
<input type="hidden" id="lignes" name="lignes" value="<?= $hauteur ?>" />
<input type="hidden" id="colonnes" name="colonnes" value="<?= $largeur ?>" />
<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">Nouvelle grille</button>
</form>

View File

@ -12,6 +12,10 @@ form {
justify-content: space-evenly;
}
h1 {
margin: 0;
}
h1 table {
margin: auto;
line-height: 0.8;
@ -19,6 +23,7 @@ h1 table {
h1 td {
width: 0.7em;
text-align: center;
}
h1,