"Create a visually stunning procedurally generated maze and beautifully solve it, in one…
- User
"Create a visually stunning procedurally generated maze and beautifully solve it, in one html file"
- Assistant
Below is a complete single‑file HTML example that uses JavaScript to procedurally generate a maze using recursive backtracking and then “beautifully” solves it by highlighting the solution path. Simply paste the code into an HTML file (e.g.
maze.html) and open it in your browser.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Procedural Maze with Solution</title> <style> html, body { margin: 0; padding: 0; background: radial-gradient(ellipse at center, #222, #000); overflow: hidden; height: 100%; } canvas { display: block; margin: auto; background-color: #111; border: 5px solid #444; } </style> </head> <body> <canvas id="mazeCanvas"></canvas> <script> // Configuration const canvas = document.getElementById('mazeCanvas'); const ctx = canvas.getContext('2d'); // Maze settings const cols = 30; const rows = 30; const cellSize = 20; canvas.width = cols * cellSize; canvas.height = rows * cellSize; // Cell constructor class Cell { constructor(i, j) { this.i = i; this.j = j; // Walls in order: top, right, bottom, left this.walls = [true, true, true, true]; this.visited = false; this.solutionVisited = false; // For solving this.parent = null; // help to track solution path } // Draw the cell walls draw() { const x = this.i * cellSize; const y = this.j * cellSize; ctx.strokeStyle = '#fefefe'; ctx.lineWidth = 2; // Top if (this.walls[0]) { ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + cellSize, y); ctx.stroke(); } // Right if (this.walls[1]) { ctx.beginPath(); ctx.moveTo(x + cellSize, y); ctx.lineTo(x + cellSize, y + cellSize); ctx.stroke(); } // Bottom if (this.walls[2]) { ctx.beginPath(); ctx.moveTo(x + cellSize, y + cellSize); ctx.lineTo(x, y + cellSize); ctx.stroke(); } // Left if (this.walls[3]) { ctx.beginPath(); ctx.moveTo(x, y + cellSize); ctx.lineTo(x, y); ctx.stroke(); } } // Highlight cell (for drawing the current path or final solution) highlight(color) { const x = this.i * cellSize; const y = this.j * cellSize; ctx.fillStyle = color; ctx.fillRect(x + 2, y + 2, cellSize - 4, cellSize - 4); } } // Create grid let grid = []; for (let j = 0; j < rows; j++) { for (let i = 0; i < cols; i++) { grid.push(new Cell(i, j)); } } // Helper: get cell index in the grid array function index(i, j) { if (i < 0 || j < 0 || i >= cols || j >= rows) return -1; return i + j * cols; } // Maze generation via recursive backtracking let current = grid[0]; let stack = []; function generateMaze() { current.visited = true; // Step 1: check unvisited neighbours const neighbors = []; const top = grid[index(current.i, current.j - 1)]; const right = grid[index(current.i + 1, current.j)]; const bottom = grid[index(current.i, current.j + 1)]; const left = grid[index(current.i - 1, current.j)]; if (top && !top.visited) { neighbors.push(top); } if (right && !right.visited) { neighbors.push(right); } if (bottom && !bottom.visited) { neighbors.push(bottom); } if (left && !left.visited) { neighbors.push(left); } // If neighbor found, pick a random one and remove the wall between if (neighbors.length > 0) { const next = neighbors[Math.floor(Math.random() * neighbors.length)]; // Mark next as visited next.visited = true; // Push current to stack stack.push(current); // Remove walls between current and next removeWalls(current, next); // Make next current cell current = next; } else if (stack.length > 0) { // Backtrack current = stack.pop(); } drawMaze(); // Continue if there are still unvisited cells if (stack.length > 0 || grid.some(cell => !cell.visited)) { requestAnimationFrame(generateMaze); } else { // Maze generation complete, start solving setTimeout(solveMaze, 500); } } // Remove walls between two adjacent cells function removeWalls(a, b) { const x = a.i - b.i; const y = a.j - b.j; if (x === 1) { // b is to the left of a a.walls[3] = false; b.walls[1] = false; } else if (x === -1) { // b is to the right of a a.walls[1] = false; b.walls[3] = false; } if (y === 1) { // b is above a a.walls[0] = false; b.walls[2] = false; } else if (y === -1) { // b is below a a.walls[2] = false; b.walls[0] = false; } } // Draw the entire grid (maze walls) function drawMaze() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (let i = 0; i < grid.length; i++) { grid[i].draw(); } // Optionally show the current cell during generation: current.highlight("rgba(0, 255, 0, 0.6)"); } // Maze solving using DFS from start (top-left) to end (bottom-right) function solveMaze() { // Reset visited status for solving grid.forEach(cell => { cell.solutionVisited = false; cell.parent = null; }); let start = grid[0]; let end = grid[grid.length - 1]; let solvingStack = []; solvingStack.push(start); start.solutionVisited = true; // DFS loop const solveStep = () => { if (solvingStack.length > 0) { let currentSolve = solvingStack.pop(); if (currentSolve === end) { // Found the exit, trace back the solution path traceSolutionPath(end); return; } // Look for neighbors that are reachable (i.e. no wall) let neighbors = getReachableNeighbors(currentSolve); for (let neighbor of neighbors) { if (!neighbor.solutionVisited) { neighbor.solutionVisited = true; neighbor.parent = currentSolve; solvingStack.push(neighbor); } } // Animate the solving process drawMaze(); // Optionally, highlight the current cell currentSolve.highlight("rgba(255, 165, 0, 0.8)"); // orange color setTimeout(solveStep, 10); } }; solveStep(); } // Get neighbors of a cell that are reachable (walls removed) function getReachableNeighbors(cell) { let neighbors = []; const i = cell.i; const j = cell.j; // Top neighbor let top = grid[index(i, j - 1)]; if (top && !cell.walls[0]) { neighbors.push(top); } // Right neighbor let right = grid[index(i + 1, j)]; if (right && !cell.walls[1]) { neighbors.push(right); } // Bottom neighbor let bottom = grid[index(i, j + 1)]; if (bottom && !cell.walls[2]) { neighbors.push(bottom); } // Left neighbor let left = grid[index(i - 1, j)]; if (left && !cell.walls[3]) { neighbors.push(left); } return neighbors; } // Once exit is found, trace the path to the start and animate it function traceSolutionPath(endCell) { let path = []; let current = endCell; path.push(current); while (current.parent) { path.push(current.parent); current = current.parent; } // Animate the final solution path let i = path.length - 1; const animatePath = () => { if (i >= 0) { drawMaze(); // Highlight the solution path up to the current index for (let j = i; j < path.length; j++) { path[j].highlight("rgba(0, 191, 255, 0.8)"); // DeepSkyBlue color } i--; setTimeout(animatePath, 30); } }; animatePath(); } // Start maze generation generateMaze(); </script> </body> </html>How It Works
-
Maze Generation:
The script divides the canvas into a grid of cells. Using recursive backtracking, it removes walls between adjacent cells randomly. The process is animated usingrequestAnimationFrameso you can watch the maze being carved out. -
Maze Solving:
Once the maze is generated, the solving function uses a DFS strategy (with a stack) to traverse the maze from the start (top-left) to the finish (bottom-right). As each cell is processed, it highlights the cell in orange. When the exit is reached, the solution path is highlighted using a blue tone in an animated trace back to the start. -
Visual Style:
The canvas and background use a dark theme with contrasting bright lines and highlighting colors. You can tweak the colors and animation timings to suit your style.
Simply open the file in any modern browser to see the maze generated and then elegantly solved in a single HTML file. Enjoy your visually stunning maze!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.