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

View File

@ -1,4 +1,5 @@
#MOT Définition #MOT Définition
A A
ABAT Tripe ou étripe. (Robert Scipion) ABAT Tripe ou étripe. (Robert Scipion)
ABATTIS Architecte à la retraite 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 <?php
const MIN_LETTRES = 0;
const MAX_MOTS = 100000;
$dico = []; $dico = [];
if (($handle = fopen("dico.csv", "r")) !== FALSE) { if (($handle = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($handle, 0, "\t"); $header = fgetcsv($handle, 0, "\t");
@ -13,7 +18,7 @@ if (($handle = fopen("dico.csv", "r")) !== FALSE) {
fclose($handle); fclose($handle);
} }
$mots_de_n_lettres = [[]]; $mots_de_n_lettres = [];
foreach ($dico as $mot => $definition) { foreach ($dico as $mot => $definition) {
$n = strlen($mot); $n = strlen($mot);
if (!isset($mots_de_n_lettres[$n])) { if (!isset($mots_de_n_lettres[$n])) {
@ -22,39 +27,41 @@ foreach ($dico as $mot => $definition) {
$mots_de_n_lettres[$n][] = $mot; $mots_de_n_lettres[$n][] = $mot;
} }
function fisherYatesShuffle(&$items, $seed) function fisherYatesShuffle(&$items)
{ {
mt_srand($seed); for ($i = count($items) - 1; $i > 0; $i--) {
for ($i = count($items) - 1; $i > 0; $i--) $j = mt_rand(0, $i);
{
$j = @mt_rand(0, $i);
$tmp = $items[$i]; $tmp = $items[$i];
$items[$i] = $items[$j]; $items[$i] = $items[$j];
$items[$j] = $tmp; $items[$j] = $tmp;
} }
mt_srand();
} }
function mots_espaces($max, $min=0, $seed=0) { function mots_espaces($longueur) {
global $mots_de_n_lettres; global $mots_de_n_lettres;
global $dico; global $dico;
if ($seed) { $nb_mots = 0;
fisherYatesShuffle($mots_de_n_lettres[$max], $seed); fisherYatesShuffle($mots_de_n_lettres[$longueur]);
} else { foreach($mots_de_n_lettres[$longueur] as $mot) {
shuffle($mots_de_n_lettres[$max]);
}
foreach($mots_de_n_lettres[$max] as $mot) {
yield $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_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) { if ($mot1 != $mot2) {
$dico["$mot1 $mot2"] = $dico[$mot1] && $dico[$mot2] ? "{$dico[$mot1]}<br/>{$dico[$mot2]}." : $dico[$mot1] . $dico[$mot2]; $dico["$mot1 $mot2"] = $dico[$mot1] && $dico[$mot2] ? "{$dico[$mot1]}<br/>{$dico[$mot2]}." : $dico[$mot1] . $dico[$mot2];
yield "$mot1 $mot2"; yield "$mot1 $mot2";
$dico["$mot2 $mot1"] = $dico[$mot2] && $dico[$mot1] ? "{$dico[$mot2]}<br/>{$dico[$mot1]}." : $dico[$mot2] . $dico[$mot1]; $dico["$mot2 $mot1"] = $dico[$mot2] && $dico[$mot1] ? "{$dico[$mot2]}<br/>{$dico[$mot1]}." : $dico[$mot2] . $dico[$mot1];
yield "$mot2 $mot1"; yield "$mot2 $mot1";
$nb_mots += 2;
if ($nb_mots > MAX_MOTS) {
break;
}
} }
} }
} }

View File

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

View File

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