final version

This commit is contained in:
JasterV 2020-04-07 15:44:34 +02:00
parent f0a36ac9b4
commit b4f8dc03d7
2 changed files with 30 additions and 26 deletions

View file

@ -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();
}
} }

View file

@ -87,23 +87,33 @@ 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 (walls.top) if (visited) {
line(x, y, x + w, y); if (walls.top)
if (walls.left) line(x, y, x + w, y);
line(x, y, x, y + w); if (walls.left)
if (walls.bottom) line(x, y, x, y + w);
line(x, y + w, x + w, y + w); if (walls.bottom)
if (walls.right) line(x, y + w, x + w, y + w);
line(x + w, y, x + w, y + w); if (walls.right)
noStroke(); line(x + w, y, x + w, y + w);
fill(color); noStroke();
rect(x, y, w, w); fill(color);
rect(x, y, w, w);
}
} }
} }
} }