interface trie avec des strings
This commit is contained in:
parent
d5a120cd9a
commit
ddebd453df
@ -171,7 +171,6 @@ class Grille implements ArrayAccess
|
|||||||
$this->grille
|
$this->grille
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
var_dump($_SESSION);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function load($id)
|
public function load($id)
|
||||||
@ -180,7 +179,6 @@ class Grille implements ArrayAccess
|
|||||||
session_start(["use_cookies" => false]);
|
session_start(["use_cookies" => false]);
|
||||||
|
|
||||||
if (!isset($_SESSION["$this->largeur,$this->hauteur"])) {
|
if (!isset($_SESSION["$this->largeur,$this->hauteur"])) {
|
||||||
var_dump($_SESSION);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
46
Trie.php
46
Trie.php
@ -5,8 +5,7 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable {
|
|||||||
public array $branches = [];
|
public array $branches = [];
|
||||||
private $nb_branches = 0;
|
private $nb_branches = 0;
|
||||||
|
|
||||||
// ArrayAccess
|
public function arraySet($cles, $valeur) {
|
||||||
public function offsetSet($cles, $valeur): void {
|
|
||||||
$this->nb_branches++;
|
$this->nb_branches++;
|
||||||
$cle = $cles[0];
|
$cle = $cles[0];
|
||||||
$cles = array_slice($cles, 1);
|
$cles = array_slice($cles, 1);
|
||||||
@ -14,38 +13,38 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable {
|
|||||||
$this->branches[$cle] = $valeur;
|
$this->branches[$cle] = $valeur;
|
||||||
} else {
|
} else {
|
||||||
if (!isset($this->branches[$cle])) $this->branches[$cle] = new Trie();
|
if (!isset($this->branches[$cle])) $this->branches[$cle] = new Trie();
|
||||||
$this->branches[$cle]->offsetSet($cles, $valeur);
|
$this->branches[$cle]->arraySet($cles, $valeur);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function offsetExists($cles): bool {
|
public function arrayExists($cles) {
|
||||||
$cle = $cles[0];
|
$cle = $cles[0];
|
||||||
$cles = array_slice($cles, 1);
|
$cles = array_slice($cles, 1);
|
||||||
if ($cles == []) {
|
if ($cles == []) {
|
||||||
return isset($this->branches[$cle]);
|
return isset($this->branches[$cle]);
|
||||||
} else {
|
} else {
|
||||||
return isset($this->branches[$cle]) && $this->branches[$cle]->offsetExists($cles);
|
return isset($this->branches[$cle]) && $this->branches[$cle]->arrayExists($cles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function &offsetGet($cles): mixed {
|
public function &arrayGet($cles) {
|
||||||
$cle = $cles[0];
|
$cle = $cles[0];
|
||||||
$cles = array_slice($cles, 1);
|
$cles = array_slice($cles, 1);
|
||||||
if ($cles == []) {
|
if ($cles == []) {
|
||||||
return $this->branches[$cle];
|
return $this->branches[$cle];
|
||||||
} else {
|
} else {
|
||||||
return $this->branches[$cle]->offsetGet($cles);
|
return $this->branches[$cle]->arrayGet($cles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function offsetUnset($cles): void {
|
public function arrayUnset($cles) {
|
||||||
$cle = $cles[0];
|
$cle = $cles[0];
|
||||||
$cles = array_slice($cles, 1);
|
$cles = array_slice($cles, 1);
|
||||||
if ($cles == []) {
|
if ($cles == []) {
|
||||||
unset($this->branches[$cle]);
|
unset($this->branches[$cle]);
|
||||||
$this->nb_branches--;
|
$this->nb_branches--;
|
||||||
} else {
|
} else {
|
||||||
$this->branches[$cle]->offsetUnset($cles);
|
$this->branches[$cle]->arrayUnset($cles);
|
||||||
$this->nb_branches--;
|
$this->nb_branches--;
|
||||||
if (count($this->branches[$cle]) == 0) {
|
if (count($this->branches[$cle]) == 0) {
|
||||||
unset($this->branches[$cle]);
|
unset($this->branches[$cle]);
|
||||||
@ -53,11 +52,10 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IteratorAggregate
|
public function arrayIterator() {
|
||||||
public function getIterator(): Traversable {
|
|
||||||
foreach ($this->branches as $cle => $branche) {
|
foreach ($this->branches as $cle => $branche) {
|
||||||
if ($branche instanceof Trie) {
|
if ($branche instanceof Trie) {
|
||||||
foreach($branche as $sous_cles => $feuille) {
|
foreach($branche->arrayIterator() as $sous_cles => $feuille) {
|
||||||
yield array_merge([$cle], $sous_cles) => $feuille;
|
yield array_merge([$cle], $sous_cles) => $feuille;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -66,6 +64,30 @@ class Trie implements ArrayAccess, IteratorAggregate, Countable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ArrayAccess
|
||||||
|
public function offsetSet($string, $valeur): void {
|
||||||
|
$this->arraySet(str_split($string), $valeur);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetExists($string): bool {
|
||||||
|
return $this->arrayExists(str_split($string));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function &offsetGet($string): mixed {
|
||||||
|
return $this->arrayGet(str_split($string));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetUnset($string): void {
|
||||||
|
$this->arrayUnset(str_split($string));
|
||||||
|
}
|
||||||
|
|
||||||
|
// IteratorAggregate
|
||||||
|
public function getIterator(): Traversable {
|
||||||
|
foreach($this->arrayIterator() as $array => $valeur) {
|
||||||
|
yield implode("", $array) => $valeur;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Countable
|
// Countable
|
||||||
public function count(): int {
|
public function count(): int {
|
||||||
return $this->nb_branches;
|
return $this->nb_branches;
|
||||||
|
8
dico.php
8
dico.php
@ -31,8 +31,8 @@ function dico($longueur_max) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$mot = str_split(strtoupper($mot));
|
$mot = strtoupper($mot);
|
||||||
$longueur = count($mot);
|
$longueur = strlen($mot);
|
||||||
if (!isset($dico[$longueur])) $dico[$longueur] = new Trie();
|
if (!isset($dico[$longueur])) $dico[$longueur] = new Trie();
|
||||||
if (!isset($dico[$longueur][$mot])) $dico[$longueur][$mot] = [];
|
if (!isset($dico[$longueur][$mot])) $dico[$longueur][$mot] = [];
|
||||||
if (strlen($definition)) $dico[$longueur][$mot][] = $definition;
|
if (strlen($definition)) $dico[$longueur][$mot][] = $definition;
|
||||||
@ -50,9 +50,9 @@ function mots_espaces($longueur_max) {
|
|||||||
for ($longueur = 1; $longueur <= $longueur_max; $longueur++) {
|
for ($longueur = 1; $longueur <= $longueur_max; $longueur++) {
|
||||||
for ($position_espace = MIN_PREMIER_MOT; $position_espace + MIN_MOTS_SUIVANTS < $longueur; $position_espace++) {
|
for ($position_espace = MIN_PREMIER_MOT; $position_espace + MIN_MOTS_SUIVANTS < $longueur; $position_espace++) {
|
||||||
$mots_suivants = $dico[$longueur - $position_espace - 1];
|
$mots_suivants = $dico[$longueur - $position_espace - 1];
|
||||||
foreach ($dico[$position_espace] as $premier_mot => $definition) {
|
foreach ($dico[$position_espace]->arrayIterator() as $premier_mot => $definition) {
|
||||||
$premier_mot[] = " ";
|
$premier_mot[] = " ";
|
||||||
$dico[$longueur][$premier_mot] = $mots_suivants;
|
$dico[$longueur]->arraySet($premier_mot, $mots_suivants);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
include_once "dico.php";
|
|
||||||
include_once "Grille.php";
|
include_once "Grille.php";
|
||||||
|
|
||||||
|
|
||||||
@ -49,7 +48,7 @@ if ($grille_valide) {
|
|||||||
for ($y = 0; $y < $hauteur; $y++) {
|
for ($y = 0; $y < $hauteur; $y++) {
|
||||||
$definitions_horizontales[$y] = [];
|
$definitions_horizontales[$y] = [];
|
||||||
foreach ($grille->lignes[$y] as $mot) {
|
foreach ($grille->lignes[$y] as $mot) {
|
||||||
$definitions = $grille->dico[strlen($mot)][str_split($mot)];
|
$definitions = $grille->dico[strlen($mot)][$mot];
|
||||||
if (count($definitions)) {
|
if (count($definitions)) {
|
||||||
$definitions_horizontales[$y][] = $definitions[mt_rand(0, count($definitions) - 1)];
|
$definitions_horizontales[$y][] = $definitions[mt_rand(0, count($definitions) - 1)];
|
||||||
}
|
}
|
||||||
@ -59,7 +58,7 @@ if ($grille_valide) {
|
|||||||
for ($x = 0 ; $x < $largeur; $x++) {
|
for ($x = 0 ; $x < $largeur; $x++) {
|
||||||
$definitions_verticales[$x] = [];
|
$definitions_verticales[$x] = [];
|
||||||
foreach ($grille->colonnes[$x] as $mot) {
|
foreach ($grille->colonnes[$x] as $mot) {
|
||||||
$definitions = $grille->dico[strlen($mot)][str_split($mot)];
|
$definitions = $grille->dico[strlen($mot)][$mot];
|
||||||
if (count($definitions)) {
|
if (count($definitions)) {
|
||||||
$definitions_verticales[$x][] = $definitions[mt_rand(0, count($definitions) - 1)];
|
$definitions_verticales[$x][] = $definitions[mt_rand(0, count($definitions) - 1)];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user