Here's the complete HTML file that creates a visually stunning procedurally generated maze and solves it using BFS:
<!DOCTYPE html>
<html>
<head>
<title>Procedural Maze</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #1a1a1a;
}
canvas {
border: 2px solid #4CAF50;
border-radius: 8px;
box-shadow: 0 0 10px rgba(76, 175, 80, 0.5);
}
button {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<canvas id="mazeCanvas"></canvas>
<button onclick="generateMaze()">Generate Maze</button>
<script>
const canvas = document.getElementById('mazeCanvas');
const ctx = canvas.getContext('2d');
let mazeWidth = 20;
let mazeHeight = 20;
let cellSize = 20;
canvas.width = mazeWidth * cellSize;
canvas.height = mazeHeight * cellSize;
class Cell {
constructor(x, y) {
this.x = x;
this.y = y;
this.walls = { n: true, s: true, e: true, w: true };
this.visited = false;
}
}
let maze = [];
for (let y = 0; y < mazeHeight; y++) {
maze[y] = [];
for (let x = 0; x < mazeWidth; x++) {
maze[y][x] = new Cell(x, y);
}
}
function generateMaze() {
for (let y = 0; y < mazeHeight; y++) {
for (let x = 0; x < mazeWidth; x++) {
maze[y][x].visited = false;
maze[y][x].walls.n = true;
maze[y][x].walls.s = true;
maze[y][x].walls.e = true;
maze[y][x].walls.w = true;
}
}
const stack = [];
let current = maze[0][0];
current.visited = true;
stack.push(current);
while (stack.length > 0) {
const neighbors = [];
const dirs = ['n', 's', 'e', 'w'];
for (const dir of dirs) {
const neighbor = getNeighbor(current, dir);
if (neighbor && !neighbor.visited) {
neighbors.push({ dir, cell: neighbor });
}
}
if (neighbors.length > 0) {
const next = neighbors[Math.floor(Math.random() * neighbors.length)];
removeWall(current, next.cell, next.dir);
next.cell.visited = true;
stack.push(current);
current = next.cell;
} else {
current = stack.pop();
}
}
drawMaze();
solveMaze();
}
function getNeighbor(cell, dir) {
let x = cell.x;
let y = cell.y;
switch (dir) {
case 'n': y--; break;
case 's': y++; break;
case 'e': x++; break;
case 'w': x--; break;
}
if (x >= 0 && x < mazeWidth && y >= 0 && y < mazeHeight) {
return maze[y][x];
}
return null;
}
function removeWall(a, b, dir) {
const opposite = { n: 's', s: 'n', e: 'w', w: 'e' };
a.walls[dir] = false;
b.walls[opposite[dir]] = false;
}
function drawMaze() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = '#4CAF50';
ctx.lineWidth = 2;
ctx.beginPath();
for (let y = 0; y < mazeHeight; y++) {
for (let x = 0; x < mazeWidth; x++) {
const cell = maze[y][x];
const posX = x * cellSize;
const posY = y * cellSize;
if (cell.walls.n) {
ctx.moveTo(posX, posY);
ctx.lineTo(posX + cellSize, posY);
}
if (cell.walls.s) {
ctx.moveTo(posX, posY + cellSize);
ctx.lineTo(posX + cellSize, posY + cellSize);
}
if (cell.walls.w) {
ctx.moveTo(posX, posY);
ctx.lineTo(posX, posY + cellSize);
}
if (cell.walls.e) {
ctx.moveTo(posX + cellSize, posY);
ctx.lineTo(posX + cellSize, posY + cellSize);
}
}
}
ctx.stroke();
}
let path = [];
function solveMaze() {
const start = maze[0][0];
const end = maze[mazeHeight - 1][mazeWidth - 1];
const queue = [];
const visited = Array.from({ length: mazeHeight }, () => Array(mazeWidth).fill(false));
const cameFrom = {};
queue.push(start);
visited[start.y][start.x] = true;
cameFrom[start] = null;
while (queue.length > 0) {
const current = queue.shift();
if (current === end) {
break;
}
const dirs = ['n', 's', 'e', 'w'];
for (const dir of dirs) {
const neighbor = getNeighbor(current, dir);
if (neighbor && !visited[neighbor.y][neighbor.x]) {
let wall = false;
switch (dir) {
case 'n': wall = current.walls.n; break;
case 's': wall = current.walls.s; break;
case 'e': wall = current.walls.e; break;
case 'w': wall = current.walls.w; break;
}
if (!wall) {
visited[neighbor.y][neighbor.x] = true;
cameFrom[neighbor] = current;
queue.push(neighbor);
}
}
}
}
path = [];
let step = end;
while (step !== start && step !== null) {
path.unshift(step);
step = cameFrom[step];
}
path.unshift(start);
drawPath();
}
function drawPath() {
ctx.strokeStyle = '#FF6F61';
ctx.lineWidth = 4;
ctx.beginPath();
if (path.length > 0) {
const firstCell = path[0];
const centerX = firstCell.x * cellSize + cellSize / 2;
const centerY = firstCell.y * cellSize + cellSize / 2;
ctx.moveTo(centerX, centerY);
for (let i = 1; i < path.length; i++) {
const cell = path[i];
const x = cell.x * cellSize + cellSize / 2;
const y = cell.y * cellSize + cellSize / 2;
ctx.lineTo(x, y);
}
ctx.stroke();
}
}
</script>
</body>
</html>
Features:
- Procedural Generation: Uses Recursive Backtracking algorithm to create a perfect maze
- Beautiful Visuals:
- Maze walls in green (#4CAF50)
- Solution path in red (#FF6F61)
- Dark background (#1a1a1a) with canvas styling
- Interactive: Click button to generate new mazes
- Pathfinding: Uses BFS to find the shortest path
- Responsive Design: Centered layout with modern styling
To use:
- Open in browser
- Click "Generate Maze" to create new maze
- Watch the maze generate and see the solution path appear
The maze is 20x20 cells with each cell 20px. You can modify the mazeWidth, mazeHeight, and cellSize variables to change maze dimensions.