mirror of
https://codeberg.org/JasterV/maze-generator.js.git
synced 2026-04-26 18:20:04 +00:00
first commit
This commit is contained in:
commit
edaf00dab7
6 changed files with 118338 additions and 0 deletions
0
README.md
Normal file
0
README.md
Normal file
17
index.html
Normal file
17
index.html
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Maze generator</title>
|
||||||
|
|
||||||
|
<script src="libraries/p5.js"></script>
|
||||||
|
<script src="libraries/p5.sound.js"></script>
|
||||||
|
<script src="utils.js"></script>
|
||||||
|
<script src="skecth.js"></script>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
106332
libraries/p5.js
Normal file
106332
libraries/p5.js
Normal file
File diff suppressed because one or more lines are too long
11834
libraries/p5.sound.js
Normal file
11834
libraries/p5.sound.js
Normal file
File diff suppressed because one or more lines are too long
50
skecth.js
Normal file
50
skecth.js
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
const windowHeight = 800;
|
||||||
|
const windowWidth = 800;
|
||||||
|
const w = 40;
|
||||||
|
let cols, rows;
|
||||||
|
let grid;
|
||||||
|
|
||||||
|
// Data structures for implementing
|
||||||
|
// Depth-first search
|
||||||
|
const stack = createStack();
|
||||||
|
const visiteds = [];
|
||||||
|
|
||||||
|
function setup() {
|
||||||
|
createCanvas(windowWidth, windowHeight);
|
||||||
|
background(51);
|
||||||
|
// Setting Frame Rate to 10
|
||||||
|
// to visualize better the maze
|
||||||
|
// generation algorithm
|
||||||
|
frameRate(20);
|
||||||
|
cols = floor(width / w);
|
||||||
|
rows = floor(height / w);
|
||||||
|
grid = createGrid(rows, cols);
|
||||||
|
// Pushing the initial cell
|
||||||
|
// to the stack and setting it
|
||||||
|
// to visited
|
||||||
|
let init_cell = grid.getCell(0, 0);
|
||||||
|
visiteds.push(init_cell);
|
||||||
|
stack.push(init_cell);
|
||||||
|
}
|
||||||
|
|
||||||
|
function draw() {
|
||||||
|
background(255);
|
||||||
|
if (!stack.isEmpty()) {
|
||||||
|
let currCell = stack.pop();
|
||||||
|
// Painting the current cell
|
||||||
|
// to help to visualize
|
||||||
|
fill(255, 0, 200);
|
||||||
|
rect(currCell.j * w, currCell.i * w, w, w);
|
||||||
|
let neighbours = grid.getCellNeighbours(currCell)
|
||||||
|
.filter((value) => !visiteds.includes(value));
|
||||||
|
if (neighbours.length > 0) {
|
||||||
|
stack.push(currCell);
|
||||||
|
let neighbour = neighbours[Math.floor(Math.random() * neighbours.length)];
|
||||||
|
grid.removeWallBetween(currCell, neighbour);
|
||||||
|
visiteds.push(neighbour);
|
||||||
|
stack.push(neighbour);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
grid.show();
|
||||||
|
}
|
||||||
|
|
||||||
105
utils.js
Normal file
105
utils.js
Normal file
|
|
@ -0,0 +1,105 @@
|
||||||
|
function createStack() {
|
||||||
|
let items = [];
|
||||||
|
return {
|
||||||
|
push(value) {
|
||||||
|
items.push(value);
|
||||||
|
},
|
||||||
|
pop() {
|
||||||
|
if (items.length == 0)
|
||||||
|
throw new Error("StackUnderflow");
|
||||||
|
return items.pop();
|
||||||
|
},
|
||||||
|
peek() {
|
||||||
|
return items[items.length - 1];
|
||||||
|
},
|
||||||
|
isEmpty() {
|
||||||
|
return items.length == 0;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createGrid(rows, cols) {
|
||||||
|
/**
|
||||||
|
* Returns an object that represents Grid of rows x cols dimensions
|
||||||
|
*/
|
||||||
|
let cells = [];
|
||||||
|
for (let i = 0; i < rows; i++)
|
||||||
|
for (let j = 0; j < cols; j++)
|
||||||
|
cells.push(createCell(i, j));
|
||||||
|
return {
|
||||||
|
rows,
|
||||||
|
cols,
|
||||||
|
getCell(i, j) {
|
||||||
|
if (i >= rows || i < 0 || j >= cols || j < 0)
|
||||||
|
return null;
|
||||||
|
let index = i * cols + j;
|
||||||
|
return cells[index];
|
||||||
|
},
|
||||||
|
getCellNeighbours(cell) {
|
||||||
|
let neighbours = [];
|
||||||
|
let bottom = this.getCell(cell.i + 1, cell.j);
|
||||||
|
let top = this.getCell(cell.i - 1, cell.j);
|
||||||
|
let left = this.getCell(cell.i, cell.j - 1);
|
||||||
|
let right = this.getCell(cell.i, cell.j + 1);
|
||||||
|
if (bottom !== null)
|
||||||
|
neighbours.push(bottom);
|
||||||
|
if (top !== null)
|
||||||
|
neighbours.push(top);
|
||||||
|
if (left !== null)
|
||||||
|
neighbours.push(left);
|
||||||
|
if (right !== null)
|
||||||
|
neighbours.push(right);
|
||||||
|
return neighbours;
|
||||||
|
},
|
||||||
|
removeWallBetween(cell, neighbour) {
|
||||||
|
if (cell.i < neighbour.i && cell.j == neighbour.j) {
|
||||||
|
cell.walls.bottom = false;
|
||||||
|
neighbour.walls.top = false;
|
||||||
|
} else if (cell.i > neighbour.i && cell.j == neighbour.j) {
|
||||||
|
cell.walls.top = false;
|
||||||
|
neighbour.walls.bottom = false;
|
||||||
|
} else if (cell.i == neighbour.i && cell.j < neighbour.j) {
|
||||||
|
cell.walls.right = false;
|
||||||
|
neighbour.walls.left = false;
|
||||||
|
} else if (cell.i == neighbour.i && cell.j > neighbour.j) {
|
||||||
|
cell.walls.left = false;
|
||||||
|
neighbour.walls.right = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
show() {
|
||||||
|
for (let i = 0; i < cells.length; i++)
|
||||||
|
cells[i].show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createCell(i, j) {
|
||||||
|
/**
|
||||||
|
* Returns an object that represents a Cell
|
||||||
|
* on a grid
|
||||||
|
*/
|
||||||
|
let x = j * w;
|
||||||
|
let y = i * w;
|
||||||
|
let walls = {
|
||||||
|
top: true,
|
||||||
|
left: true,
|
||||||
|
bottom: true,
|
||||||
|
right: true,
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
i,
|
||||||
|
j,
|
||||||
|
walls,
|
||||||
|
show() {
|
||||||
|
stroke(0);
|
||||||
|
if (walls.top)
|
||||||
|
line(x, y, x + w, y);
|
||||||
|
if (walls.left)
|
||||||
|
line(x, y, x, y + w);
|
||||||
|
if (walls.bottom)
|
||||||
|
line(x, y + w, x + w, y + w);
|
||||||
|
if (walls.right)
|
||||||
|
line(x + w, y, x + w, y + w);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue