Maze generation working with new Maze object.
This commit is contained in:
+8
-9
@@ -6,7 +6,6 @@ class Maze{
|
||||
this.height = mazeHeight;
|
||||
this.cellWidth = cellWidth;
|
||||
this.workingStack = [];
|
||||
this.currentCell = {};
|
||||
this.grid = [];
|
||||
|
||||
for (let y = 0; y < this.rows; y++) {
|
||||
@@ -28,12 +27,12 @@ class Maze{
|
||||
|
||||
buildNextCell(){
|
||||
//Build the maze
|
||||
this.currentCell = this.workingStack.pop();
|
||||
let currentCell = this.workingStack.pop();
|
||||
//currentCell.visited = true;
|
||||
let nextCell = getNeighbors(this.currentCell);
|
||||
let nextCell = this.getNeighbors(currentCell);
|
||||
|
||||
if(nextCell){
|
||||
this.workingStack.push(this.currentCell);
|
||||
this.workingStack.push(currentCell);
|
||||
currentCell.Selected = false;
|
||||
currentCell.visited = true;
|
||||
nextCell.visited = true;
|
||||
@@ -44,7 +43,7 @@ class Maze{
|
||||
else
|
||||
this.workingStack.pop();
|
||||
|
||||
this.currentCell.selected = false;
|
||||
currentCell.selected = false;
|
||||
}
|
||||
|
||||
get mazeGenDone() {
|
||||
@@ -54,10 +53,10 @@ class Maze{
|
||||
getNeighbors(cell){
|
||||
var visited = [];
|
||||
|
||||
var top = this.grid[index(cell.x, cell.y - 1)];
|
||||
var left = this.grid[index(cell.x - 1, cell.y)];
|
||||
var bottom = this.grid[index(cell.x, cell.y + 1)];
|
||||
var right = this.grid[index(cell.x + 1, cell.y)];
|
||||
var top = this.grid[this.index(cell.x, cell.y - 1)];
|
||||
var left = this.grid[this.index(cell.x - 1, cell.y)];
|
||||
var bottom = this.grid[this.index(cell.x, cell.y + 1)];
|
||||
var right = this.grid[this.index(cell.x + 1, cell.y)];
|
||||
|
||||
if (top && !top.visited) {
|
||||
visited.push(top);
|
||||
|
||||
+1
-4
@@ -22,11 +22,8 @@
|
||||
clear: both;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
|
||||
|
||||
</script>
|
||||
<script src="cell.js"></script>
|
||||
<script src="gen-maze.js"></script>
|
||||
<script src="../p5.js"></script>
|
||||
<script src="../addons/p5.sound.min.js"></script>
|
||||
<script src="sketch.js"></script>
|
||||
|
||||
@@ -14,49 +14,17 @@ function setup() {
|
||||
canvas.parent('sketch-holder');
|
||||
maze = new Maze(width, height, cellWidth);
|
||||
frameRate(60);
|
||||
|
||||
rows = floor(width / cellWidth);
|
||||
columns = floor(height / cellWidth);
|
||||
|
||||
for (let y = 0; y < rows; y++) {
|
||||
for (var x = 0; x < columns; x++) {
|
||||
grid.push(new Cell(x, y, cellWidth));
|
||||
}
|
||||
}
|
||||
|
||||
//currentCell = grid[0];
|
||||
grid[0].visited = true;
|
||||
grid[0].selected = true;
|
||||
cellStack.push(grid[0]);
|
||||
}
|
||||
|
||||
function draw() {
|
||||
background(220);
|
||||
|
||||
for (var i = 0; i < grid.length; i++) {
|
||||
grid[i].show();
|
||||
maze.updateMazeDisplay();
|
||||
|
||||
if(maze.workingStack.length > 0){
|
||||
maze.buildNextCell();
|
||||
}
|
||||
|
||||
if(cellStack.length > 0){
|
||||
//Build the maze
|
||||
let currentCell = cellStack.pop();
|
||||
//currentCell.visited = true;
|
||||
let nextCell = getNeighbors(currentCell);
|
||||
|
||||
if(nextCell){
|
||||
cellStack.push(currentCell);
|
||||
currentCell.Selected = false;
|
||||
currentCell.visited = true;
|
||||
nextCell.visited = true;
|
||||
nextCell.selected = true;
|
||||
removeWalls(currentCell, nextCell);
|
||||
cellStack.push(nextCell);
|
||||
|
||||
} else cellStack.pop();
|
||||
|
||||
currentCell.selected = false;
|
||||
}
|
||||
else if (cellStack.length == 0 && frontier.length == 0 && explored.length == 0){
|
||||
/*else if (cellStack.length == 0 && frontier.length == 0 && explored.length == 0){
|
||||
for (var i = 0; i < grid.length; i++) {
|
||||
grid[i].visited = false;
|
||||
}
|
||||
@@ -91,9 +59,9 @@ function draw() {
|
||||
if(currentNode.x == 9 && currentNode.y == 9)
|
||||
//currentNode.end = true;
|
||||
frontier = [];
|
||||
}
|
||||
}*/
|
||||
else{
|
||||
tmp = explored.pop();
|
||||
/*tmp = explored.pop();
|
||||
|
||||
while (tmp.parent != undefined) {
|
||||
tmp.finalPath = true;
|
||||
@@ -103,7 +71,7 @@ function draw() {
|
||||
|
||||
for (var i = 0; i < grid.length; i++) {
|
||||
grid[i].show();
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
print("Maze search done");
|
||||
|
||||
Reference in New Issue
Block a user