diff --git a/index.php b/index.php index 021a5a0..f4c8528 100644 --- a/index.php +++ b/index.php @@ -13,7 +13,8 @@ $id = filter_input(INPUT_GET, 'grille', FILTER_VALIDATE_REGEXP, [ ] ]); if (!$id) { - header("Location: " . $_SERVER['PHP_SELF'] . "?grille=" . uniqid() . "&" . http_build_query($_GET)); + $_GET["grille"] = uniqid(); + header("Location: " . $_SERVER['PHP_SELF'] . "?" . http_build_query($_GET)); exit; } @@ -78,7 +79,10 @@ $grille = new Grille($hauteur, $largeur, $id); -
+ + + + @@ -92,9 +96,9 @@ $grille = new Grille($hauteur, $largeur, $id); @@ -121,64 +125,7 @@ $grille = new Grille($hauteur, $largeur, $id); - + + \ No newline at end of file diff --git a/script.js b/script.js new file mode 100644 index 0000000..7a6522d --- /dev/null +++ b/script.js @@ -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 == '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(); + } + } + }); + } + }; +});
"> grille[$l][$c] == " "): ?> - + - +