mots-croises/dico.php
2025-04-24 04:10:25 +02:00

44 lines
1.1 KiB
PHP

<?php
$dico = [];
if (($handle = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($handle, 0, "\t");
while (($ligne = fgetcsv($handle, 0, "\t")) !== FALSE) {
if (count($ligne) >= 2) {
$mot = $ligne[0];
$definition = $ligne[1];
$dico[$mot] = $definition;
}
}
fclose($handle);
}
$mots_de_n_lettres = [[]];
foreach ($dico as $mot => $definition) {
$n = strlen($mot);
if (!isset($mots_de_n_lettres[$n])) {
$mots_de_n_lettres[$n] = [];
}
$mots_de_n_lettres[$n][] = $mot;
}
foreach ($mots_de_n_lettres as $n => $mots) {
shuffle($mots_de_n_lettres[$n]);
}
function mots_espaces($max, $min=0) {
global $mots_de_n_lettres;
foreach($mots_de_n_lettres[$max] as $mot) {
yield $mot;
}
for ($i = ceil($max / 2); $max - $i -1 >= $min; $i++) {
foreach ($mots_de_n_lettres[$i] as $mot1) {
foreach (mots_espaces($max - $i -1, $min) as $mot2) {
if ($mot1 != $mot2) {
yield "$mot1 $mot2";
yield "$mot2 $mot1";
}
}
}
}
}