Self Solving Maze
Home
Snippets
Self Solving Maze
HTML
CSS
JS
<div class="maze" id="maze"></div> <div class="label">Generating & solving maze</div>
body { margin: 0; height: 100vh; background: #0b0e17; display: flex; justify-content: center; align-items: center; font-family: system-ui, sans-serif; color: #fff; } .maze { display: grid; grid-template-columns: repeat(20, 22px); gap: 2px; } .cell { width: 22px; height: 22px; background: #1a1f35; } .wall { background: #050814; } .start { background: #00e5ff; } .end { background: #ff4d6d; } .visited { background: #2f3a7a; } .path { background: #7aa2ff; } .label { position: fixed; bottom: 20px; font-size: 14px; opacity: 0.8; letter-spacing: 1px; }
const SIZE = 20; const mazeEl = document.getElementById("maze"); let cells = []; let grid = []; function index(x, y) { return y * SIZE + x; } function generateMaze() { mazeEl.innerHTML = ""; cells = []; grid = []; for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { const div = document.createElement("div"); div.className = "cell"; const wall = Math.random() < 0.3; grid.push({ x, y, wall, visited: false, el: div }); if (wall) div.classList.add("wall"); mazeEl.appendChild(div); cells.push(div); } } grid[0].wall = false; grid[grid.length - 1].wall = false; grid[0].el.classList.remove("wall"); grid[grid.length - 1].el.classList.remove("wall"); grid[0].el.classList.add("start"); grid[grid.length - 1].el.classList.add("end"); } function solveMaze() { const stack = [{ x: 0, y: 0 }]; const parent = {}; const timer = setInterval(() => { if (!stack.length) { clearInterval(timer); return; } const { x, y } = stack.pop(); const cell = grid[index(x, y)]; if (cell.visited) return; cell.visited = true; if (!cell.el.classList.contains("start")) { cell.el.classList.add("visited"); } if (x === SIZE - 1 && y === SIZE - 1) { drawPath(parent, x, y); clearInterval(timer); return; } const dirs = [ [1, 0], [-1, 0], [0, 1], [0, -1] ]; dirs.forEach(([dx, dy]) => { const nx = x + dx; const ny = y + dy; if (nx >= 0 && ny >= 0 && nx < SIZE && ny < SIZE) { const next = grid[index(nx, ny)]; if (!next.wall && !next.visited) { parent[`${nx},${ny}`] = { x, y }; stack.push({ x: nx, y: ny }); } } }); }, 35); } function drawPath(parent, x, y) { let cur = { x, y }; const timer = setInterval(() => { const key = `${cur.x},${cur.y}`; const p = parent[key]; if (!p) { clearInterval(timer); return; } const c = grid[index(p.x, p.y)]; if (!c.el.classList.contains("start")) { c.el.classList.add("path"); } cur = p; }, 40); } generateMaze(); setTimeout(solveMaze, 600);
Ad #1
Ad #2
Scroll to Top