Created
February 9, 2019 11:31
-
-
Save jjantschulev/ac1c219edbb53268e1fb16d2faf330ae to your computer and use it in GitHub Desktop.
P5.js Maze Generator
This file contains hidden or 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
const mapWidth = 50; | |
const mapHeight = 50; | |
const path = []; | |
const stack = []; | |
const map = []; | |
let currentIndex = 0; | |
let mazeGenFinished = false; | |
let gridSizeX = 10; | |
let gridSizeY = 10; | |
function setup() { | |
createCanvas(1000, 1000); | |
gridSizeX = width / mapWidth; | |
gridSizeY = height / mapHeight; | |
generateMap(); | |
// frameRate(10); | |
} | |
function draw() { | |
background(245); | |
renderMap(); | |
if (!mazeGenFinished) { | |
for (let i = 0; i < 5; i++) { | |
iterate(); | |
} | |
let x = currentIndex % mapWidth; | |
let y = Math.floor(currentIndex / mapWidth); | |
fill(255, 0, 0); | |
ellipse( | |
x * gridSizeX + gridSizeX / 2, | |
y * gridSizeY + gridSizeY / 2, | |
gridSizeX / 2, | |
gridSizeY / 2 | |
); | |
} | |
} | |
function generateMap() { | |
for (let i = 0; i < mapWidth * mapHeight; i++) { | |
map[i] = [true, true, true, true]; | |
} | |
} | |
function renderMap() { | |
strokeWeight(2); | |
stroke(0); | |
for (let i = 0; i < map.length; i++) { | |
// Render four walls | |
let x = i % mapWidth; | |
let y = Math.floor(i / mapWidth); | |
if (map[i][0]) { | |
line( | |
x * gridSizeX, | |
y * gridSizeY, | |
(x + 1) * gridSizeX, | |
y * gridSizeY | |
); | |
} | |
if (map[i][1]) { | |
line( | |
(x + 1) * gridSizeX, | |
y * gridSizeY, | |
(x + 1) * gridSizeX, | |
(y + 1) * gridSizeY | |
); | |
} | |
if (map[i][2]) { | |
line( | |
x * gridSizeX, | |
(y + 1) * gridSizeY, | |
(x + 1) * gridSizeX, | |
(y + 1) * gridSizeY | |
); | |
} | |
if (map[i][3]) { | |
line( | |
x * gridSizeX, | |
y * gridSizeY, | |
x * gridSizeX, | |
(y + 1) * gridSizeY | |
); | |
} | |
} | |
} | |
function getAvaliableSpaces(index) { | |
let result = []; | |
if ((index + 1) % mapWidth !== 0) result.push(index + 1); | |
if (index % mapWidth !== 0) result.push(index - 1); | |
if (index >= mapWidth) result.push(index - mapWidth); | |
if (index < (mapHeight - 1) * mapWidth) result.push(index + mapHeight); | |
result = result.filter(i => !path.includes(i)); | |
return result; | |
} | |
function iterate() { | |
if (mazeGenFinished) return; | |
let spaces = getAvaliableSpaces(currentIndex); | |
if (spaces.length <= 0) { | |
if (path.length === map.length) { | |
mazeGenFinished = true; | |
} else { | |
stack.pop(); | |
currentIndex = stack[stack.length - 1]; | |
} | |
} else { | |
let randomPos = spaces[floor(random(0, spaces.length))]; | |
path.push(randomPos); | |
stack.push(randomPos); | |
map[currentIndex][ | |
getWallIndexFromDir(randomPos - currentIndex) | |
] = false; | |
map[randomPos][getWallIndexFromDir(currentIndex - randomPos)] = false; | |
currentIndex = randomPos; | |
} | |
} | |
function getWallIndexFromDir(dir) { | |
switch (dir) { | |
case 1: | |
return 1; | |
case -1: | |
return 3; | |
case mapWidth: | |
return 2; | |
case -mapWidth: | |
return 0; | |
default: | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment