Commit 43962c5
Changed files (2)
004.Maze-Runner
004.Maze-Runner/index.html
@@ -47,6 +47,9 @@
Maze Generation Algorithm
</a>
</li>
+ <li>
+ Who knows the maze better,in maze runner?<br/>Close this box and try typing his name.
+ </li>
</ul>
</p>
</div>
004.Maze-Runner/script.js
@@ -1,6 +1,12 @@
let cells;
let maze_created = false;
let won = false;
+let answer_stack = [];
+let answer_found = false;
+let cheatcodes = [];
+const cheatkeys = ["m", "i", "n", "h", "o"];
+let cheatTimeout;
+let isCheatCodeActivated = false;
const rows = 15;
const cols = 15;
@@ -42,13 +48,13 @@ function getRandomUnVisitedNeighbour(current) {
}
function removeWall(current, next) {
- /*
-Explanation of the following code:
-If the direction is 1, it means that the random neighbour is to the right of the current cell
-If the direction is -1, it means that the random neighbour is to the left of the current cell
-If the direction is cols, it means that the random neighbour is below the current cell
-If the direction is -cols, it means that the random neighbour is above the current cell
-*/
+ /*
+ Explanation of the following code:
+ If the direction is 1, it means that the random neighbour is to the right of the current cell
+ If the direction is -1, it means that the random neighbour is to the left of the current cell
+ If the direction is cols, it means that the random neighbour is below the current cell
+ If the direction is -cols, it means that the random neighbour is above the current cell
+ */
const direction = next - current;
if (direction === -cols) {
cells[current].style.borderTop = "none";
@@ -72,17 +78,27 @@ async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
-async function dfs(maze,start) {
+async function dfs(maze, start) {
const stack = [start];
- while (stack.length > 0){
+ answer_stack = [start];
+ while (stack.length > 0) {
const current = stack.pop();
+ if (current === rows * cols - 1) {
+ answer_found = true;
+ } else if (!answer_found) {
+ answer_stack.pop();
+ }
maze.children[current].classList.add("current-gen");
- visited.add(current);
+ visited.add(current);
const next = getRandomUnVisitedNeighbour(current);
if (next) {
removeWall(current, next);
stack.push(current);
stack.push(next);
+ if (!answer_found) {
+ answer_stack.push(current);
+ answer_stack.push(next);
+ }
}
await sleep(10);
maze.children[current].classList.remove("current-gen");
@@ -92,8 +108,8 @@ async function dfs(maze,start) {
tooltip.innerHTML = "Use W A S D or arrow keys to move";
}
-function lightUpButtons(btn){
- switch(btn){
+function lightUpButtons(btn) {
+ switch (btn) {
case "w":
w.classList.add("lit");
setTimeout(() => { w.classList.remove("lit"); }, 100);
@@ -113,29 +129,29 @@ function lightUpButtons(btn){
}
}
-function movement(maze,movement){
- if(won){
+function movement(maze, movement) {
+ if (won) {
return;
}
const current = document.querySelector(".current");
const index = Array.from(maze.children).indexOf(current);
- if(movement === "UP" && current.style.borderTop === "none"){
+ if (movement === "UP" && current.style.borderTop === "none") {
maze.children[index].classList.remove("current");
- maze.children[index-cols].classList.add("current");
+ maze.children[index - cols].classList.add("current");
}
- if(movement === "DOWN" && current.style.borderBottom === "none"){
+ if (movement === "DOWN" && current.style.borderBottom === "none") {
maze.children[index].classList.remove("current");
- maze.children[index+cols].classList.add("current");
+ maze.children[index + cols].classList.add("current");
}
- if(movement === "LEFT" && current.style.borderLeft === "none"){
+ if (movement === "LEFT" && current.style.borderLeft === "none") {
maze.children[index].classList.remove("current");
- maze.children[index-1].classList.add("current");
+ maze.children[index - 1].classList.add("current");
}
- if(movement === "RIGHT" && current.style.borderRight === "none"){
+ if (movement === "RIGHT" && current.style.borderRight === "none") {
maze.children[index].classList.remove("current");
- maze.children[index+1].classList.add("current");
+ maze.children[index + 1].classList.add("current");
}
- if(document.querySelector(".current").classList.contains("final")){
+ if (document.querySelector(".current").classList.contains("final")) {
document.querySelector(".current").classList.add("won");
won = true;
tooltip.innerHTML = "You won! Press F5 to play again";
@@ -158,54 +174,103 @@ document.addEventListener("DOMContentLoaded", async function () {
const final_position = rows * cols - 1;
cells = document.querySelectorAll(".cell");
- await dfs(maze,initial_position);
+ await dfs(maze, initial_position);
cells[final_position].classList.add("final");
document.querySelector(".controls").style.visibility = "visible";
document.addEventListener("keydown", function (event) {
- if(!maze_created || won){
+ if (!maze_created || won || isCheatCodeActivated) {
return;
}
- if(event.key ==="w" || event.key === "W" || event.key === "ArrowUp"){
- movement(maze,"UP");
+ if (event.key === "w" || event.key === "W" || event.key === "ArrowUp") {
+ movement(maze, "UP");
lightUpButtons("w");
}
- if(event.key ==="s" || event.key === "S" || event.key === "ArrowDown"){
- movement(maze,"DOWN");
+ if (event.key === "s" || event.key === "S" || event.key === "ArrowDown") {
+ movement(maze, "DOWN");
lightUpButtons("s");
}
- if(event.key ==="a" || event.key === "A" || event.key === "ArrowLeft"){
- movement(maze,"LEFT");
+ if (event.key === "a" || event.key === "A" || event.key === "ArrowLeft") {
+ movement(maze, "LEFT");
lightUpButtons("a");
}
- if(event.key ==="d" || event.key === "D" || event.key === "ArrowRight"){
- movement(maze,"RIGHT");
+ if (event.key === "d" || event.key === "D" || event.key === "ArrowRight") {
+ movement(maze, "RIGHT");
lightUpButtons("d");
}
// f5 key
- if(event.key === "F5"){
+ if (event.key === "F5") {
window.location.reload();
}
+
+ // cheatcode
+
+ if (cheatkeys.includes(event.key.toLowerCase())) {
+ cheatcodes.push(event.key.toLowerCase());
+ clearTimeout(cheatTimeout);
+ cheatTimeout = setTimeout(() => {
+ cheatcodes = [];
+ }, 3000);
+
+ if (cheatcodes.length == 5) {
+ let cheatcode = cheatcodes.join("");
+ if (cheatcode === "minho") {
+ isCheatCodeActivated = true;
+ moveAccordingToTheAnswerStack();
+ tooltip.textContent = `Cheat code activated!`;
+ }
+ cheatcodes = [];
+ }
+ } else {
+ cheatcodes = [];
+ }
});
w.onclick = () => {
- movement(maze,"UP");
+ movement(maze, "UP");
lightUpButtons("w");
}
-
+
s.onclick = () => {
- movement(maze,"DOWN");
+ movement(maze, "DOWN");
lightUpButtons("s");
}
-
+
a.onclick = () => {
- movement(maze,"LEFT");
+ movement(maze, "LEFT");
lightUpButtons("a");
}
-
+
d.onclick = () => {
- movement(maze,"RIGHT");
+ movement(maze, "RIGHT");
lightUpButtons("d");
}
-
+
});
+
+
+async function moveAccordingToTheAnswerStack() {
+ if (answer_stack.length === 0) {
+ return;
+ }
+ const maze = document.querySelector("#maze");
+ while (answer_stack.length > 0) {
+ let current = document.querySelector(".current");
+ current.style.backgroundColor = "rgba(255, 215, 0, 0.7)";
+ let index = Array.from(maze.children).indexOf(current);
+ let next = answer_stack.shift();
+ if (next === index - cols) {
+ movement(maze, "UP");
+ }
+ if (next === index + cols) {
+ movement(maze, "DOWN");
+ }
+ if (next === index - 1) {
+ movement(maze, "LEFT");
+ }
+ if (next === index + 1) {
+ movement(maze, "RIGHT");
+ }
+ await sleep(50);
+ }
+}