From f7754632dea470f93e817690e18ae9d16275b32f Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Thu, 25 Apr 2024 17:52:58 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20starts=20cleaning=20code=20befor?= =?UTF-8?q?e=20adding=20labyrinth=20generation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/js/load.js | 93 ++++++++++++++++++----------------------------- index.php | 5 ++- list/index.php | 9 +++-- save.php | 56 +++++++++++++++------------- 4 files changed, 74 insertions(+), 89 deletions(-) diff --git a/assets/js/load.js b/assets/js/load.js index e097b72..2bbb3bd 100644 --- a/assets/js/load.js +++ b/assets/js/load.js @@ -1,3 +1,8 @@ +const KO = 0, + OK = 1, + WALL = 2, + LOSE = 3, + WIN = 4; let clicked = false, sounds = $.map(["ko.wav", "ok.wav", "undefined.wav", "gameover.wav", "welldone.wav"], function(sound) { return 'assets/sounds/'+sound; @@ -5,7 +10,7 @@ let clicked = false, boardbg = '#FFFFFF', score = 0, time = 0, - compte, nbsnd, oksnd, kosnd, nosnd, failsnd, level, left, top, bottom, right, currentPosition, toleft, toright, totop, tobot; + compte, nbsnd, oksnd, kosnd, nosnd, failsnd, level, p_left, p_top, p_bottom, p_right, currentPosition, toright, totop, tobot; //console.log('level :'+level); $(document).ready(function(){ @@ -34,15 +39,15 @@ $(document).ready(function(){ $('#ok').live('click', function(e){ e.preventDefault(); - playSound(1); + playSound(OK); }); $('#ko').live('click', function(e){ e.preventDefault(); - playSound(0); + playSound(KO); }); $('#unknown').live('click', function(e){ e.preventDefault(); - playSound(2); + playSound(WALL); }); $('#retry').live('click', function(e){ @@ -59,16 +64,16 @@ $(document).ready(function(){ time = 0; nbsnd = sounds.length; //console.log('loading '+nbsnd+' sounds'); - oksnd = new Audio(sounds[1]); - kosnd = new Audio(sounds[0]); - nosnd = new Audio(sounds[2]); - failsnd = new Audio(sounds[3]); - endsnd = new Audio(sounds[4]); + oksnd = new Audio(sounds[OK]); + kosnd = new Audio(sounds[KO]); + nosnd = new Audio(sounds[WALL]); + failsnd = new Audio(sounds[LOSE]); + endsnd = new Audio(sounds[WIN]); - left = 2; - right = 2; - top = 2; - bottom = 2; + p_left = 2; + p_right = 2; + p_top = 2; + p_bottom = 2; $('body').html('').append('').append('
'); $('#board').append('Start?
Instruction Instructions en français'); @@ -93,11 +98,11 @@ $(document).ready(function(){ function playSound(sound){ playColor(sound); switch(sound){ - case 0: kosnd.play(); break; - case 1: oksnd.play(); break; - case 2: nosnd.play(); break; - case 3: failsnd.play(); break; - case 4: endsnd.play(); break; + case KO: kosnd.play(); break; + case OK: oksnd.play(); break; + case WALL: nosnd.play(); break; + case LOSE: failsnd.play(); break; + case WIN: endsnd.play(); break; default: /*failsnd.play();*/ break; } } @@ -128,43 +133,18 @@ $(document).ready(function(){ clearTimeout(totop); clearTimeout(toright); clearTimeout(tobot); - left = level[currentPosition[1]][currentPosition[0]-1] == undefined ? 2 : level[currentPosition[1]][currentPosition[0]-1]; - shikitop = level[currentPosition[1]-1] == undefined ? 2 : level[currentPosition[1]-1][currentPosition[0]]; - right = level[currentPosition[1]][currentPosition[0]+1] == undefined ? 2 : level[currentPosition[1]][currentPosition[0]+1]; - bottom = level[currentPosition[1]+1] == undefined ? 2 : level[currentPosition[1]+1][currentPosition[0]]; - playSound(left); // left - totop = setTimeout(playSound, 800, shikitop); // top - toright = setTimeout(playSound, 1600, right); // right - tobot = setTimeout(playSound, 2400, bottom); // bottom + p_left = level[currentPosition[1]][currentPosition[0]-1] == undefined ? 2 : level[currentPosition[1]][currentPosition[0]-1]; + p_top = level[currentPosition[1]-1] == undefined ? 2 : level[currentPosition[1]-1][currentPosition[0]]; + p_right = level[currentPosition[1]][currentPosition[0]+1] == undefined ? 2 : level[currentPosition[1]][currentPosition[0]+1]; + p_bottom = level[currentPosition[1]+1] == undefined ? 2 : level[currentPosition[1]+1][currentPosition[0]]; + playSound(p_left); // left + totop = setTimeout(playSound, 800, p_top); // top + toright = setTimeout(playSound, 1600, p_right); // right + tobot = setTimeout(playSound, 2400, p_bottom); // bottom } - function check1caseOnLeft(){ - return currentPosition[0] > 0; - } - - function check1caseOnTop(){ - return currentPosition[1] > 0; - } - function check1caseOnRight(){ - return currentPosition[0] + 1 < level[0].length; - } - function check1caseOnBottom(){ - return currentPosition[1] + 1 <= level.length; - } - function check2caseOnLeft(){ - return !(currentPosition[0] <= 1 || level[currentPosition[1]][currentPosition[0] - 1] == 1); - } - function check2caseOnTop(){ - return !(currentPosition[1] <= 1 || level[currentPosition[1] - 1][currentPosition[0]] == 1); - } - function check2caseOnRight(){ - - } - function check2caseOnBottom(){ - - } function generateLevel(){ - // generer largeur / hauteur + // generate largeur / hauteur var min = 5; var max = 15; var largeur = Math.floor(Math.random() * (max - min + 1)) + min; @@ -237,9 +217,8 @@ $(document).ready(function(){ function gameover(){ clearInterval(compte); clicked = false; - //console.log('game over'); - playSound(3); - var finalTime = parseFloat(time/10); + playSound(LOSE); + let finalTime = Math.round(time / 10); $('#board').html('You lost... Do you want to try again? You played : '+finalTime+'seconds'); } @@ -256,7 +235,7 @@ $(document).ready(function(){ function winningGame(){ clicked = false; //console.log('You won !'); - playSound(4); + playSound(WIN); $('#board').html(''); } @@ -307,7 +286,7 @@ $(document).ready(function(){ } break; // bas case 32: // barre espace - playKey(); + playKey(); break; default: break; } diff --git a/index.php b/index.php index 152214b..2ecce03 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,5 @@  - + + Blind Laby + Blind Laby - Create your own
isDir()){ + if (!$file->isDir() && $file->getExtension() === 'js') { echo 'getFileName()) echo ' class="current"'; + if($currentFile !== null && $currentFile === $file->getFileName()) echo ' class="current"'; echo '>laby '.substr($file->getFileName(), 0, -3).'
'; } } diff --git a/save.php b/save.php index f93b55c..0d4bcba 100644 --- a/save.php +++ b/save.php @@ -1,25 +1,29 @@ -$v){ - if(!in_array($n, array('startingPoint','endingPoint','largeur','hauteur'))){ - $coord = getCoordForCase($n); - $cases[$coord[0]][$coord[1]] = 1; - } +$largeur = (int)$_POST['largeur']; +$hauteur = (int)$_POST['hauteur']; +$cases = []; +foreach ($_POST as $n => $v) { + if (!in_array($n, ['startingPoint', 'endingPoint', 'largeur', 'hauteur'])) { + $coord = getCoordForCase($n); + $cases[$coord[0]][$coord[1]] = 1; + } } -function getCoordForPt($t){ - if (preg_match('/^check(\d{1,2})-(\d{1,2})$/', $t, $m) !== 0) { +function getCoordForPt($t) +{ + if (preg_match('/^check(\d{1,2})-(\d{1,2})$/', $t, $m) !== 0) { return '[' . $m[2] . ',' . $m[1] . ']'; } - return null; + + return null; } -function getCoordForCase($t){ - if (preg_match('/^check(\d{1,2})-(\d{1,2})$/', $t, $m) !== 0) { + +function getCoordForCase($t) +{ + if (preg_match('/^check(\d{1,2})-(\d{1,2})$/', $t, $m) !== 0) { return [$m[1], $m[2]]; } @@ -27,24 +31,24 @@ function getCoordForCase($t){ } $level = '['; -for($i=0; $i<$hauteur; $i++){ - $level .= '['; - for($j=0; $j<$largeur; $j++){ - if(!empty($cases[$i][$j])) { +for ($i = 0; $i < $hauteur; $i++) { + $level .= '['; + for ($j = 0; $j < $largeur; $j++) { + if (!empty($cases[$i][$j])) { $level .= '1,'; } else { $level .= '0,'; } - } - $level = substr($level, 0, -1).'],'; + } + $level = substr($level, 0, -1) . '],'; } -$level = substr($level, 0, -1).'];'; +$level = substr($level, 0, -1) . '];'; $file = ''; -$file .= 'currentPosition = '.getCoordForPt($startingPoint).';'; -$file .= 'finalPosition ='.getCoordForPt($endingPoint).';'; -$file .= 'level = '.$level; +$file .= 'currentPosition = ' . getCoordForPt($startingPoint) . ';'; +$file .= 'finalPosition =' . getCoordForPt($endingPoint) . ';'; +$file .= 'level = ' . $level; $tod = gettimeofday(); -$finalFilename = $tod['sec'].mt_rand(0,50).'.js'; -file_put_contents(__DIR__ .'/custom/'.$finalFilename, $file); +$finalFilename = $tod['sec'] . mt_rand(0, 50) . '.js'; +file_put_contents(__DIR__ . '/custom/' . $finalFilename, $file); $_SESSION['file'] = $finalFilename; header('Location: list/'); \ No newline at end of file