Compare commits
2 Commits
ef49f35e2a
...
ea141bfde6
Author | SHA1 | Date | |
---|---|---|---|
ea141bfde6 | |||
b9eb5b725f |
@ -14,7 +14,7 @@ class Grille {
|
|||||||
private $mots_commencant_par;
|
private $mots_commencant_par;
|
||||||
private $mots_utilises = [];
|
private $mots_utilises = [];
|
||||||
|
|
||||||
public function __construct($hauteur, $largeur) {
|
public function __construct($hauteur, $largeur, $id="") {
|
||||||
$this->hauteur = $hauteur;
|
$this->hauteur = $hauteur;
|
||||||
$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, '.'));
|
||||||
@ -27,7 +27,7 @@ class Grille {
|
|||||||
$this->mots_commencant_par = [];
|
$this->mots_commencant_par = [];
|
||||||
foreach ($dimensions as $longueur) {
|
foreach ($dimensions as $longueur) {
|
||||||
$this->mots_commencant_par[$longueur] = [];
|
$this->mots_commencant_par[$longueur] = [];
|
||||||
foreach(mots_espaces($longueur, MIN_LETTRES) as $mot) {
|
foreach(mots_espaces($longueur, MIN_LETTRES, crc32($id)) 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])) {
|
||||||
|
20
dico.php
20
dico.php
@ -21,14 +21,28 @@ foreach ($dico as $mot => $definition) {
|
|||||||
}
|
}
|
||||||
$mots_de_n_lettres[$n][] = $mot;
|
$mots_de_n_lettres[$n][] = $mot;
|
||||||
}
|
}
|
||||||
foreach ($mots_de_n_lettres as $n => $mots) {
|
|
||||||
shuffle($mots_de_n_lettres[$n]);
|
function fisherYatesShuffle(&$items, $seed)
|
||||||
|
{
|
||||||
|
@mt_srand($seed);
|
||||||
|
for ($i = count($items) - 1; $i > 0; $i--)
|
||||||
|
{
|
||||||
|
$j = @mt_rand(0, $i);
|
||||||
|
$tmp = $items[$i];
|
||||||
|
$items[$i] = $items[$j];
|
||||||
|
$items[$j] = $tmp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function mots_espaces($max, $min=0) {
|
function mots_espaces($max, $min=0, $seed=0) {
|
||||||
global $mots_de_n_lettres;
|
global $mots_de_n_lettres;
|
||||||
global $dico;
|
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) {
|
foreach($mots_de_n_lettres[$max] as $mot) {
|
||||||
yield $mot;
|
yield $mot;
|
||||||
}
|
}
|
||||||
|
94
index.php
94
index.php
@ -3,28 +3,42 @@ ini_set('display_errors', 1);
|
|||||||
ini_set('html_errors', 1);
|
ini_set('html_errors', 1);
|
||||||
ini_set('error_reporting', E_ALL);
|
ini_set('error_reporting', E_ALL);
|
||||||
|
|
||||||
|
const HAUTEUR_PAR_DEFAUT = 6;
|
||||||
|
const LARGEUR_PAR_DEFAUT = 6;
|
||||||
|
|
||||||
|
|
||||||
|
$id = filter_input(INPUT_GET, 'grille', FILTER_VALIDATE_REGEXP, [
|
||||||
|
"options" => [
|
||||||
|
"regexp" => "/^[a-f0-9]{13}$/"
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
if (!$id) {
|
||||||
|
$_GET["grille"] = uniqid();
|
||||||
|
header("Location: " . $_SERVER['PHP_SELF'] . "?" . http_build_query($_GET));
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
include_once "dico.php";
|
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,
|
||||||
"min_range" => 2,
|
"min_range" => 2,
|
||||||
"max_range" => 10
|
"max_range" => 30
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
$largeur = filter_input(INPUT_GET, 'colonnes', FILTER_VALIDATE_INT, [
|
$largeur = filter_input(INPUT_GET, 'colonnes', FILTER_VALIDATE_INT, [
|
||||||
"options" => [
|
"options" => [
|
||||||
"default" => LARGEUR_PAR_DEFAUT,
|
"default" => LARGEUR_PAR_DEFAUT,
|
||||||
"min_range" => 2,
|
"min_range" => 2,
|
||||||
"max_range" => 10
|
"max_range" => 30
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$grille = new Grille($hauteur, $largeur);
|
$grille = new Grille($hauteur, $largeur, $id);
|
||||||
|
|
||||||
?>
|
?>
|
||||||
<!DOCTYPE HTML>
|
<!DOCTYPE HTML>
|
||||||
@ -65,7 +79,10 @@ $grille = new Grille($hauteur, $largeur);
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</h1>
|
</h1>
|
||||||
<form id="grilleForm" class="grille">
|
<form id="grilleForm" class="grille" method="get" location=".">
|
||||||
|
<input type="hidden" id="hauteur" name="lignes" value="<?= $hauteur ?>" />
|
||||||
|
<input type="hidden" id="largeur" name="colonnes" value="<?= $largeur ?>" />
|
||||||
|
<input type="hidden" id="solution_hashee" value="<?= $grille->hash() ?>" />
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th></th>
|
<th></th>
|
||||||
@ -79,9 +96,9 @@ $grille = new Grille($hauteur, $largeur);
|
|||||||
<?php for ($c = 0; $c < $largeur; $c++): ?>
|
<?php for ($c = 0; $c < $largeur; $c++): ?>
|
||||||
<td class="case <?= $grille->grille[$l][$c] == " " ? "noire" : "blanche" ?>">
|
<td class="case <?= $grille->grille[$l][$c] == " " ? "noire" : "blanche" ?>">
|
||||||
<?php if ($grille->grille[$l][$c] == " "): ?>
|
<?php if ($grille->grille[$l][$c] == " "): ?>
|
||||||
<input type="text" maxlength="1" size="1" name="<?= $l . $c ?>" value=" " disabled />
|
<input type="text" maxlength="1" size="1" value=" " disabled />
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<input type="text" maxlength="1" size="1" name="<?= $l . $c ?>" required pattern="[A-Z]" />
|
<input type="text" maxlength="1" size="1" required pattern="[A-Z]" />
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</td>
|
</td>
|
||||||
<?php endfor; ?>
|
<?php endfor; ?>
|
||||||
@ -108,64 +125,7 @@ $grille = new Grille($hauteur, $largeur);
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script src="script.js"></script>
|
||||||
const inputs = Array.from(grilleForm.querySelectorAll('input[type="text"]'))
|
</body>
|
||||||
let largeur = <?= $largeur ?>;
|
|
||||||
let nb_cases = inputs.length;
|
|
||||||
let index = 0;
|
|
||||||
|
|
||||||
async function sha256(text) {
|
|
||||||
const encoder = new TextEncoder();
|
|
||||||
const data = encoder.encode(text);
|
|
||||||
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
|
||||||
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
||||||
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
|
||||||
}
|
|
||||||
|
|
||||||
inputs.forEach(input => {
|
|
||||||
input.index = index++
|
|
||||||
|
|
||||||
input.onfocus = function(event) {
|
|
||||||
input.select();
|
|
||||||
}
|
|
||||||
|
|
||||||
input.onkeydown = function(event) {
|
|
||||||
switch (event.key) {
|
|
||||||
case "ArrowUp":
|
|
||||||
inputs[(input.index - largeur + nb_cases) % nb_cases].focus()
|
|
||||||
break;
|
|
||||||
case "ArrowDown":
|
|
||||||
inputs[(input.index + largeur) % nb_cases].focus()
|
|
||||||
break;
|
|
||||||
case "ArrowLeft":
|
|
||||||
inputs[(input.index - 1 + nb_cases) % nb_cases].focus()
|
|
||||||
break;
|
|
||||||
case "ArrowRight":
|
|
||||||
inputs[(input.index + 1) % nb_cases].focus()
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
input.oninput = function(event) {
|
|
||||||
this.value = this.value.toUpperCase()
|
|
||||||
if (!input.checkValidity()) {
|
|
||||||
input.value = ""
|
|
||||||
}
|
|
||||||
if (grilleForm.checkValidity()) {
|
|
||||||
sha256(inputs.map(input => input.value).join("")).then(
|
|
||||||
hash => {
|
|
||||||
if (hash == "<?= $grille->hash() ?>") {
|
|
||||||
alert("Bravo ! Vous avez trouvé la grille !")
|
|
||||||
if (confirm("Bravo ! \nUne nouvelle partie ?")) {
|
|
||||||
location = "."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
// <?= $grille->hash() ?>
|
|
||||||
</script>
|
|
||||||
|
|
||||||
</html>
|
</html>
|
81
script.js
Normal file
81
script.js
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
const inputs = Array.from(grilleForm.querySelectorAll('input[type="text"]'));
|
||||||
|
let nb_cases = inputs.length;
|
||||||
|
|
||||||
|
async function sha256(text) {
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
const data = encoder.encode(text);
|
||||||
|
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
||||||
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
||||||
|
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
let index = 0;
|
||||||
|
inputs.forEach(input => {
|
||||||
|
input.index = index++;
|
||||||
|
|
||||||
|
input.onfocus = function (event) {
|
||||||
|
input.select();
|
||||||
|
};
|
||||||
|
|
||||||
|
input.onkeydown = function (event) {
|
||||||
|
console.log(largeur);
|
||||||
|
largeur = Number(largeur.value);
|
||||||
|
console.log(largeur);
|
||||||
|
switch (event.key) {
|
||||||
|
case 'ArrowUp':
|
||||||
|
inputs[(input.index - largeur + nb_cases) % nb_cases].focus();
|
||||||
|
break;
|
||||||
|
case 'ArrowDown':
|
||||||
|
inputs[(input.index + largeur) % nb_cases].focus();
|
||||||
|
break;
|
||||||
|
case 'ArrowLeft':
|
||||||
|
inputs[(input.index - 1 + nb_cases) % nb_cases].focus();
|
||||||
|
break;
|
||||||
|
case 'ArrowRight':
|
||||||
|
inputs[(input.index + 1) % nb_cases].focus();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
input.oninput = function (event) {
|
||||||
|
this.value = this.value.toUpperCase();
|
||||||
|
if (!input.checkValidity()) {
|
||||||
|
input.value = '';
|
||||||
|
}
|
||||||
|
if (grilleForm.checkValidity()) {
|
||||||
|
sha256(inputs.map(input => input.value).join('')).then(hash => {
|
||||||
|
if (hash == '<?= $grille->hash() ?>') {
|
||||||
|
if (confirm('Bravo ! \nUne nouvelle partie ?')) {
|
||||||
|
location.href = location.href.replace(/grille=[a-f0-9]{13}&/, '');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
document.querySelectorAll('input').forEach(input => {
|
||||||
|
input.onkeydown = function (event) {
|
||||||
|
switch (event.key) {
|
||||||
|
case 'ArrowRight':
|
||||||
|
inputs[(input.index + 1) % nb_cases].focus();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
input.oninput = function (event) {
|
||||||
|
this.value = this.value.toUpperCase();
|
||||||
|
if (!input.checkValidity()) {
|
||||||
|
input.value = '';
|
||||||
|
}
|
||||||
|
if (grilleForm.checkValidity()) {
|
||||||
|
sha256(inputs.map(input => input.value).join('')).then(hash => {
|
||||||
|
if (hash == solution_hashee.value) {
|
||||||
|
if (confirm('Bravo ! \nUne nouvelle partie ?')) {
|
||||||
|
grilleForm.submit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user