ajustement de mots_espaces
This commit is contained in:
parent
64bc91d4a0
commit
4b69fa4803
16
Grille.php
16
Grille.php
@ -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();
|
||||
}
|
||||
|
1
dico.csv
1
dico.csv
@ -1,4 +1,5 @@
|
||||
#MOT Définition
|
||||
|
||||
A
|
||||
ABAT Tripe ou étripe. (Robert Scipion)
|
||||
ABATTIS Architecte à la retraite
|
||||
|
|
39
dico.php
39
dico.php
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
26
index.php
26
index.php
@ -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,
|
||||
@ -118,9 +118,9 @@ $grille = new Grille($hauteur, $largeur, $id);
|
||||
</ol>
|
||||
</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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user