-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added multiple maze generation algorithms
- added growing tree algorithm - added binary tree generator - added sidewinder algorithm - removed mazeGenerator.js
- Loading branch information
1 parent
c6aa1bc
commit a8beede
Showing
5 changed files
with
403 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** Class for generating a maze using the binary tree generator. | ||
* The maze generated by this class can be represented as a binary tree. | ||
*/ | ||
class BinaryTreeGenerator { | ||
constructor() {} | ||
|
||
/** | ||
* Fills the grid with walls as preparation for the algorithm. | ||
*/ | ||
fillGrid() { | ||
for (let y = 0; y < gridData.length; y++) { | ||
for (let x = 0; x < gridData[y].length; x++) { | ||
gridData[y][x].type = "wall"; | ||
d3.select("#node-" + y + "-" + x) | ||
.transition() | ||
.duration(500) | ||
.attr("fill", "#171717"); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Function for running the binary tree generator. | ||
* Loops over the whole grid and generates the maze row by row. | ||
*/ | ||
async startMaze() { | ||
// fill the grid so the algorithm can carve the paths | ||
this.fillGrid(); | ||
|
||
for (let y = 1; y < gridData.length - 1; y += 2) { | ||
for (let x = 1; x < gridData[y].length - 1; x += 2) { | ||
let dirs = []; | ||
|
||
// adding the directions the algorithm can carve | ||
// the path in to an array. | ||
if (y > 1) { | ||
// carve upwards | ||
dirs.push({ y: y - 1, x }); | ||
} | ||
if (x < gridData[y].length - 2) { | ||
// carve right | ||
dirs.push({ y: y, x: x + 1 }); | ||
} | ||
|
||
// set the current cell to type "floor" | ||
gridData[y][x].type = "floor"; | ||
await colorBlock("#node-" + y + "-" + x, "#FFFFFF", 100, 10); | ||
|
||
// The last cell in the first row has no direction to carve in. | ||
// It is the only cell where the length of the dirs array is 0. | ||
if (dirs.length != 0) { | ||
// randomly choose a direction | ||
let taken = dirs.length > 1 ? Math.round(Math.random()) : 0; | ||
var direction = dirs[taken]; | ||
|
||
// update the cell in the direction that was chosen earlier | ||
gridData[direction.y][direction.x].type = "floor"; | ||
await colorBlock( | ||
"#node-" + direction.y + "-" + direction.x, | ||
"#FFFFFF", | ||
100, | ||
10 | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/** Class for generating a maze using the growing tree algorithm. */ | ||
class GrowingTree { | ||
constructor() {} | ||
|
||
/** | ||
* Generates the maze using the growing tree algorithm. | ||
* @param {Object} cell - The starting cell, usually randomly chosen. | ||
*/ | ||
async startMaze(cell) { | ||
// filling the grid so the algorithm can carve out ways | ||
this.fillGrid(); | ||
|
||
// declaring a list of unvisited cells | ||
// and a list of possible directions | ||
let cells = []; | ||
let dirs = [ | ||
{ dy: 0, dx: 2 }, | ||
{ dy: 2, dx: 0 }, | ||
{ dy: -2, dx: 0 }, | ||
{ dy: 0, dx: -2 }, | ||
]; | ||
|
||
let width = gridData[0].length; | ||
let height = gridData.length; | ||
|
||
cells.push(cell); | ||
|
||
// running as long as there are unvisited cells left | ||
while (cells.length != 0) { | ||
// shuffling the list of directions | ||
dirs = this.shuffle(dirs); | ||
|
||
let index = Math.round(Math.random() * (cells.length - 1)); | ||
|
||
let x = cells[index].x; | ||
let y = cells[index].y; | ||
|
||
gridData[y][x] = "floor"; | ||
await colorBlock("#node-" + y + "-" + x, "#fff", 100, 10); | ||
|
||
// looping over the list of directions to check for unvisited neighbours | ||
for (const direction in dirs) { | ||
let dx = dirs[direction].dx; | ||
let dy = dirs[direction].dy; | ||
|
||
let nx = x + dx; | ||
let ny = y + dy; | ||
|
||
// check if the cell in the current direction | ||
// is within bounds and unvisited | ||
if ( | ||
nx > 0 && | ||
ny > 0 && | ||
nx < width && | ||
ny < height && | ||
gridData[ny][nx].type == "wall" | ||
) { | ||
// set the cell to type "floor" | ||
// color it and add it to the list | ||
// of cells to check | ||
gridData[ny][nx] = "floor"; | ||
await colorBlock("#node-" + ny + "-" + nx, "#fff", 10, 0); | ||
|
||
// updating the cell between the other cells | ||
if (dx != 0) { | ||
gridData[y][x + dx / 2] = "floor"; | ||
await colorBlock( | ||
"#node-" + y + "-" + (x + dx / 2), | ||
"#fff", | ||
100, | ||
10 | ||
); | ||
} else { | ||
gridData[y + dy / 2][x] = "floor"; | ||
await colorBlock("#node-" + (y + dy / 2) + "-" + x, "#fff", 10, 0); | ||
} | ||
|
||
cells.push({ x: nx, y: ny }); | ||
index = undefined; | ||
break; | ||
} | ||
} | ||
// there were nor unvisited neighbours | ||
// -> delete cell from the list | ||
if (index != undefined) { | ||
cells.splice(index, 1); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Fills the grid with walls as preparation for the algorithm. | ||
*/ | ||
fillGrid() { | ||
for (let y = 0; y < gridData.length; y++) { | ||
for (let x = 0; x < gridData[y].length; x++) { | ||
gridData[y][x].type = "wall"; | ||
d3.select("#node-" + y + "-" + x) | ||
.transition() | ||
.duration(500) | ||
.attr("fill", "#171717"); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Fisher-Yates shuffle for shuffling the list of neighbouring cells. | ||
* @param {Object[]} array - The list of neighbouring cells | ||
* @returns {Object[]} - The shuffled list | ||
*/ | ||
shuffle(array) { | ||
var j, x, i; | ||
for (i = array.length - 1; i > 0; i--) { | ||
j = Math.floor(Math.random() * (i + 1)); | ||
x = array[i]; | ||
array[i] = array[j]; | ||
array[j] = x; | ||
} | ||
return array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.