Maze generation working with new Maze object.

This commit is contained in:
2020-05-19 09:09:21 -05:00
parent 2fbcc0b345
commit eece3deffb
3 changed files with 16 additions and 52 deletions
+8 -9
View File
@@ -6,7 +6,6 @@ class Maze{
this.height = mazeHeight; this.height = mazeHeight;
this.cellWidth = cellWidth; this.cellWidth = cellWidth;
this.workingStack = []; this.workingStack = [];
this.currentCell = {};
this.grid = []; this.grid = [];
for (let y = 0; y < this.rows; y++) { for (let y = 0; y < this.rows; y++) {
@@ -28,12 +27,12 @@ class Maze{
buildNextCell(){ buildNextCell(){
//Build the maze //Build the maze
this.currentCell = this.workingStack.pop(); let currentCell = this.workingStack.pop();
//currentCell.visited = true; //currentCell.visited = true;
let nextCell = getNeighbors(this.currentCell); let nextCell = this.getNeighbors(currentCell);
if(nextCell){ if(nextCell){
this.workingStack.push(this.currentCell); this.workingStack.push(currentCell);
currentCell.Selected = false; currentCell.Selected = false;
currentCell.visited = true; currentCell.visited = true;
nextCell.visited = true; nextCell.visited = true;
@@ -44,7 +43,7 @@ class Maze{
else else
this.workingStack.pop(); this.workingStack.pop();
this.currentCell.selected = false; currentCell.selected = false;
} }
get mazeGenDone() { get mazeGenDone() {
@@ -54,10 +53,10 @@ class Maze{
getNeighbors(cell){ getNeighbors(cell){
var visited = []; var visited = [];
var top = this.grid[index(cell.x, cell.y - 1)]; var top = this.grid[this.index(cell.x, cell.y - 1)];
var left = this.grid[index(cell.x - 1, cell.y)]; var left = this.grid[this.index(cell.x - 1, cell.y)];
var bottom = this.grid[index(cell.x, cell.y + 1)]; var bottom = this.grid[this.index(cell.x, cell.y + 1)];
var right = this.grid[index(cell.x + 1, cell.y)]; var right = this.grid[this.index(cell.x + 1, cell.y)];
if (top && !top.visited) { if (top && !top.visited) {
visited.push(top); visited.push(top);
+1 -4
View File
@@ -22,11 +22,8 @@
clear: both; clear: both;
} }
</style> </style>
<script>
</script>
<script src="cell.js"></script> <script src="cell.js"></script>
<script src="gen-maze.js"></script>
<script src="../p5.js"></script> <script src="../p5.js"></script>
<script src="../addons/p5.sound.min.js"></script> <script src="../addons/p5.sound.min.js"></script>
<script src="sketch.js"></script> <script src="sketch.js"></script>
+7 -39
View File
@@ -14,49 +14,17 @@ function setup() {
canvas.parent('sketch-holder'); canvas.parent('sketch-holder');
maze = new Maze(width, height, cellWidth); maze = new Maze(width, height, cellWidth);
frameRate(60); 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() { function draw() {
background(220); background(220);
for (var i = 0; i < grid.length; i++) { maze.updateMazeDisplay();
grid[i].show();
}
if(cellStack.length > 0){ if(maze.workingStack.length > 0){
//Build the maze maze.buildNextCell();
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++) { for (var i = 0; i < grid.length; i++) {
grid[i].visited = false; grid[i].visited = false;
} }
@@ -91,9 +59,9 @@ function draw() {
if(currentNode.x == 9 && currentNode.y == 9) if(currentNode.x == 9 && currentNode.y == 9)
//currentNode.end = true; //currentNode.end = true;
frontier = []; frontier = [];
} }*/
else{ else{
tmp = explored.pop(); /*tmp = explored.pop();
while (tmp.parent != undefined) { while (tmp.parent != undefined) {
tmp.finalPath = true; tmp.finalPath = true;
@@ -103,7 +71,7 @@ function draw() {
for (var i = 0; i < grid.length; i++) { for (var i = 0; i < grid.length; i++) {
grid[i].show(); grid[i].show();
} }*/
print("Maze search done"); print("Maze search done");