retour du récursif

This commit is contained in:
Adrien MALINGREY 2025-04-30 14:06:14 +02:00
parent 590e9ab3ec
commit 64e4113aa7
3 changed files with 70 additions and 66 deletions

View File

@ -3,16 +3,16 @@
include_once "dico.php"; include_once "dico.php";
function melanger_cles($tableau) { function melanger_cles($tableau)
uksort($tableau, function($a, $b) { {
uksort($tableau, function ($a, $b) {
return mt_rand(-1, 1); return mt_rand(-1, 1);
}); });
return $tableau; return $tableau;
} }
class Grille class Grille implements ArrayAccess {
{
public $grille; public $grille;
public $hauteur; public $hauteur;
public $largeur; public $largeur;
@ -20,6 +20,7 @@ class Grille
private $lettres_suivantes; private $lettres_suivantes;
private $positions; private $positions;
private $nb_positions; private $nb_positions;
private $mots_utilises;
public function __construct($hauteur, $largeur, $id = "") public function __construct($hauteur, $largeur, $id = "")
{ {
@ -51,7 +52,9 @@ class Grille
} }
$this->nb_positions = count($this->positions); $this->nb_positions = count($this->positions);
$this->grilles = $this->generateur($id); mt_srand($id == "" ? null : crc32($id));
$this->mots_utilises = [];
$this->grilles = $this->generateur();
} }
public function get_ligne($y, $largeur) public function get_ligne($y, $largeur)
@ -72,30 +75,30 @@ class Grille
return $colonne; return $colonne;
} }
public function generateur($id = "") public function generateur($i = 0)
{ {
mt_srand($id == ""? null : crc32($id)); if ($i == $this->nb_positions) {
yield $this;
$mots_utilises = []; return;
$pile = [];
$lettres_communes = melanger_cles(array_intersect_key(
$this->lettres_suivantes[$this->largeur],
$this->lettres_suivantes[$this->hauteur]
));
foreach ($lettres_communes as $lettre => $_) {
$pile[] = [0, $lettre];
} }
while (!empty($pile)) { [$x, $y] = $this->positions[$i];
[$i, $lettre] = array_pop($pile);
[$x, $y] = $this->positions[$i];
$this->grille[$y][$x] = $lettre;
if ($i == $this->nb_positions - 1) { $lettres_suivantes_ligne = $this->lettres_suivantes[$this->largeur];
yield $this; for ($x2 = 0; $x2 < $x; $x2++) {
continue; $lettres_suivantes_ligne = $lettres_suivantes_ligne[$this->grille[$y][$x2]];
} }
$lettres_suivantes_colonne = $this->lettres_suivantes[$this->hauteur];
for ($y2 = 0; $y2 < $y; $y2++) {
$lettres_suivantes_colonne = $lettres_suivantes_colonne[$this->grille[$y2][$x]];
}
$lettres_communes = melanger_cles(array_intersect_key(
$lettres_suivantes_ligne,
$lettres_suivantes_colonne
));
foreach ($lettres_communes as $lettre => $_) {
$this->grille[$y][$x] = $lettre;
if ($x == $this->largeur - 1) { if ($x == $this->largeur - 1) {
$mots_utilises[$y] = $this->get_ligne($y, $x); $mots_utilises[$y] = $this->get_ligne($y, $x);
@ -108,28 +111,23 @@ class Grille
} }
} }
$i++; if ($i < $this->nb_positions) {
[$x, $y] = $this->positions[$i]; yield from $this->generateur($i + 1);
$lettres_suivantes_ligne = $this->lettres_suivantes[$this->largeur]; } else {
for ($x2 = 0; $x2 < $x; $x2++) { yield $this;
$lettres_suivantes_ligne = $lettres_suivantes_ligne[$this->grille[$y][$x2]];
}
$lettres_suivantes_colonne = $this->lettres_suivantes[$this->hauteur];
for ($y2 = 0; $y2 < $y; $y2++) {
$lettres_suivantes_colonne = $lettres_suivantes_colonne[$this->grille[$y2][$x]];
}
$lettres_communes = melanger_cles(array_intersect_key(
$lettres_suivantes_ligne,
$lettres_suivantes_colonne
));
foreach ($lettres_communes as $lettre => $_) {
$pile[] = [$i, $lettre, $lettres_suivantes_ligne[$lettre]];
} }
} }
} }
public function hash()
{
$string = "";
foreach ($this->grille as $ligne) {
$string .= implode("", $ligne);
}
return hash('sha256', $string);
}
public function current() public function current()
{ {
return $this->grilles->current(); return $this->grilles->current();
@ -140,12 +138,20 @@ class Grille
return $this->grilles->valid(); return $this->grilles->valid();
} }
public function hash() public function offsetExists(mixed $offset): bool {
{ return isset($this->grille[$offset]);
$string = "";
foreach ($this->grille as $ligne) {
$string .= implode("", $ligne);
}
return hash('sha256', $string);
} }
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]);
}
} }

View File

@ -96,11 +96,11 @@ $grille->current();
<tr> <tr>
<th><?= $y + 1 ?></th> <th><?= $y + 1 ?></th>
<?php for ($x = 0; $x < $largeur; $x++): ?> <?php for ($x = 0; $x < $largeur; $x++): ?>
<td class="case <?= $grille->grille[$y][$x] == " " ? "noire" : "blanche" ?>"> <td class="case <?= $grille[$y][$x] == " " ? "noire" : "blanche" ?>">
<?php if ($grille->grille[$y][$x] == " "): ?> <?php if ($grille[$y][$x] == " "): ?>
<input type="text" maxlength="1" size="1" value=" " disabled /> <input type="text" maxlength="1" size="1" value=" " disabled />
<?php else: ?> <?php else: ?>
<input type="text" maxlength="1" size="1" pattern="[A-Z]"/> <input type="text" maxlength="1" size="1" pattern="[A-Z]" placeholder="<?= $grille[$y][$x] ?>" />
<?php endif; ?> <?php endif; ?>
</td> </td>
<?php endfor; ?> <?php endfor; ?>

View File

@ -43,23 +43,17 @@ h2 {
.grille-et-definitions { .grille-et-definitions {
display: flex; display: flex;
flex-flow: row; flex-flow: row;
justify-content: space-around; justify-content: space-evenly;
flex-wrap: wrap; flex-wrap: wrap;
height: max-content; height: max-content;
align-items: flex-start; flex-grow: 1;
margin: auto; align-items: center;
}
.grille {
display: flex;
flex-flow: column;
justify-content: center;
margin: auto 0;
} }
.grille table { .grille table {
border-collapse: collapse; border-collapse: collapse;
margin: auto; margin: 0 auto;
height: fit-content;
} }
.grille th, .grille th,
@ -114,6 +108,10 @@ h2 {
background-color: black; background-color: black;
} }
.grille input::placeholder {
color: transparent;
}
.definitions { .definitions {
width: 30%; width: 30%;
} }
@ -164,7 +162,7 @@ button[type='submit'] {
button[type='submit'] { button[type='submit'] {
width: 100%; width: 100%;
margin: 0 auto; margin: auto;
border: none; border: none;
background: none; background: none;
font-family: inherit; font-family: inherit;