o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
make me a webpage that creates random mazes
- User
make me a webpage that creates random mazes
- Assistant
make me a webpage that creates random mazes
Certainly! Below is an example of a simple webpage that generates and displays random mazes using the recursive backtracking algorithm. The maze is displayed on an HTML5 canvas. You can save this code as an HTML file (e.g., maze.html) and open it in a web browser to see the maze generation in action.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Random Maze Generator</title>
<style>
body {
text-align: center;
background-color: #f0f0f0;
}
#mazeCanvas {
border: 1px solid black;
margin-top: 20px;
}
#generateButton {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Random Maze Generator</h1>
<canvas id="mazeCanvas" width="600" height="600"></canvas>
<br>
<button id="generateButton">Generate New Maze</button>
<script>
// Maze generation parameters
const canvas = document.getElementById('mazeCanvas');
const ctx = canvas.getContext('2d');
const rows = 30; // Number of rows
const cols = 30; // Number of columns
const cellSize = canvas.width / cols;
let grid = [];
let stack = [];
// Cell constructor
function Cell(i, j) {
this.i = i;
this.j = j;
this.walls = [true, true, true, true]; // Top, Right, Bottom, Left
this.visited = false;
this.checkNeighbors = function() {
let neighbors = [];
let top = grid[index(i, j - 1)];
let right = grid[index(i + 1, j)];
let bottom = grid[index(i, j + 1)];
let left = grid[index(i - 1, 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 (neighbors.length > 0) {
let r = Math.floor(Math.random() * neighbors.length);
return neighbors[r];
} else {
return undefined;
}
}
this.highlight = function() {
let x = this.i * cellSize;
let y = this.j * cellSize;
ctx.fillStyle = 'green';
ctx.fillRect(x, y, cellSize, cellSize);
}
this.show = function() {
let x = this.i * cellSize;
let y = this.j * cellSize;
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
if (this.walls[0]) {
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + cellSize, y);
ctx.stroke();
}
if (this.walls[1]) {
ctx.beginPath();
ctx.moveTo(x + cellSize, y);
ctx.lineTo(x + cellSize, y + cellSize);
ctx.stroke();
}
if (this.walls[2]) {
ctx.beginPath();
ctx.moveTo(x + cellSize, y + cellSize);
ctx.lineTo(x, y + cellSize);
ctx.stroke();
}
if (this.walls[3]) {
ctx.beginPath();
ctx.moveTo(x, y + cellSize);
ctx.lineTo(x, y);
ctx.stroke();
}
if (this.visited) {
ctx.fillStyle = '#cccccc';
ctx.fillRect(x, y, cellSize, cellSize);
}
}
}
function index(i, j) {
if (i < 0 || j < 0 || i > cols - 1 || j > rows - 1) {
return -1;
}
return i + j * cols;
}
function setup() {
// Initialize grid
grid = [];
for (let j = 0; j < rows; j++) {
for (let i = 0; i < cols; i++) {
let cell = new Cell(i, j);
grid.push(cell);
}
}
// Start generation from top-left cell
current = grid[0];
current.visited = true;
stack = [];
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Generate the maze
generateMaze();
}
function generateMaze() {
let current = grid[0];
stack.push(current);
while (stack.length > 0) {
current = stack.pop();
let next = current.checkNeighbors();
if (next) {
stack.push(current);
// Remove walls between current and next
removeWalls(current, next);
next.visited = true;
stack.push(next);
}
}
// Draw the maze
for (let i = 0; i < grid.length; i++) {
grid[i].show();
}
}
function removeWalls(a, b) {
let x = a.i - b.i;
let y = a.j - b.j;
if (x === 1) {
a.walls[3] = false;
b.walls[1] = false;
} else if (x === -1) {
a.walls[1] = false;
b.walls[3] = false;
}
if (y === 1) {
a.walls[0] = false;
b.walls[2] = false;
} else if (y === -1) {
a.walls[2] = false;
b.walls[0] = false;
}
}
// Generate a maze when the page loads
window.onload = function() {
setup();
}
// Regenerate maze on button click
document.getElementById('generateButton').addEventListener('click', function() {
setup();
});
</script>
</body>
</html>
HTML Structure:
mazeCanvas is used to draw the maze.CSS Styling:
JavaScript Logic:
Cell object that knows its position (i, j), its walls, and whether it has been visited.Event Handling:
.html extension, such as maze.html.Adjust Maze Size:
rows and cols variables to change the size of the maze.rows = 20 and cols = 20 creates a smaller maze.Change Cell Size:
cellSize is calculated based on the canvas width and the number of columns.cellSize and adjust the canvas dimensions accordingly.Styling:
Enjoy exploring and customizing your random maze generator!
Not yet included in a published release.