mirror of
https://codeberg.org/JasterV/maze-generator.js.git
synced 2026-04-26 18:20:04 +00:00
final version
This commit is contained in:
parent
f0a36ac9b4
commit
b4f8dc03d7
2 changed files with 30 additions and 26 deletions
|
|
@ -7,15 +7,13 @@ let grid;
|
||||||
// Data structures for implementing
|
// Data structures for implementing
|
||||||
// Depth-first search
|
// Depth-first search
|
||||||
const stack = createStack();
|
const stack = createStack();
|
||||||
const visiteds = [];
|
|
||||||
|
|
||||||
function setup() {
|
function setup() {
|
||||||
createCanvas(windowWidth, windowHeight);
|
createCanvas(windowWidth, windowHeight);
|
||||||
// Setting Frame Rate to 30
|
// Setting Frame Rate to 30
|
||||||
// to visualize better the maze
|
// to visualize better the maze
|
||||||
// generation algorithm
|
// generation algorithm
|
||||||
frameRate(60);
|
frameRate(20);
|
||||||
background(0);
|
|
||||||
cols = floor(width / w);
|
cols = floor(width / w);
|
||||||
rows = floor(height / w);
|
rows = floor(height / w);
|
||||||
grid = createGrid(rows, cols);
|
grid = createGrid(rows, cols);
|
||||||
|
|
@ -23,31 +21,27 @@ function setup() {
|
||||||
// to the stack and setting it
|
// to the stack and setting it
|
||||||
// to visited
|
// to visited
|
||||||
let init_cell = grid.getCell(0, 0);
|
let init_cell = grid.getCell(0, 0);
|
||||||
visiteds.push(init_cell);
|
init_cell.setVisited();
|
||||||
stack.push(init_cell);
|
stack.push(init_cell);
|
||||||
}
|
}
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
|
background(0);
|
||||||
|
grid.show();
|
||||||
if (!stack.isEmpty()) {
|
if (!stack.isEmpty()) {
|
||||||
let currCell = stack.pop();
|
let currCell = stack.pop();
|
||||||
// Paint the cell in pink color if we are
|
|
||||||
// Removing it from the Stack
|
|
||||||
currCell.show(color(255,0,100));
|
|
||||||
let neighbours = grid.getCellNeighbours(currCell)
|
let neighbours = grid.getCellNeighbours(currCell)
|
||||||
.filter((value) => !visiteds.includes(value));
|
.filter((value) => !value.isVisited());
|
||||||
|
// Show the current cell in pink
|
||||||
|
currCell.show(color(255, 0, 200));
|
||||||
if (neighbours.length > 0) {
|
if (neighbours.length > 0) {
|
||||||
stack.push(currCell);
|
stack.push(currCell);
|
||||||
// Paint the cell in white if
|
|
||||||
// we still pushing into the stack
|
|
||||||
currCell.show(255);
|
|
||||||
let neighbour = neighbours[Math.floor(Math.random() * neighbours.length)];
|
let neighbour = neighbours[Math.floor(Math.random() * neighbours.length)];
|
||||||
grid.removeWallBetween(currCell, neighbour);
|
grid.removeWallBetween(currCell, neighbour);
|
||||||
visiteds.push(neighbour);
|
neighbour.setVisited();
|
||||||
stack.push(neighbour);
|
stack.push(neighbour);
|
||||||
}
|
}
|
||||||
} else{
|
} else
|
||||||
grid.show();
|
|
||||||
noLoop();
|
noLoop();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
||||||
10
src/utils.js
10
src/utils.js
|
|
@ -87,12 +87,20 @@ function createCell(i, j) {
|
||||||
bottom: true,
|
bottom: true,
|
||||||
right: true,
|
right: true,
|
||||||
}
|
}
|
||||||
|
let visited = false;
|
||||||
return {
|
return {
|
||||||
i,
|
i,
|
||||||
j,
|
j,
|
||||||
walls,
|
walls,
|
||||||
|
setVisited() {
|
||||||
|
visited = true;
|
||||||
|
},
|
||||||
|
isVisited() {
|
||||||
|
return visited;
|
||||||
|
},
|
||||||
show(color) {
|
show(color) {
|
||||||
stroke(0);
|
stroke(0);
|
||||||
|
if (visited) {
|
||||||
if (walls.top)
|
if (walls.top)
|
||||||
line(x, y, x + w, y);
|
line(x, y, x + w, y);
|
||||||
if (walls.left)
|
if (walls.left)
|
||||||
|
|
@ -105,5 +113,7 @@ function createCell(i, j) {
|
||||||
fill(color);
|
fill(color);
|
||||||
rect(x, y, w, w);
|
rect(x, y, w, w);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue