Last active
November 9, 2023 07:49
-
-
Save jacmkno/4be637d2e79e8981fc6bcc26287fd27c to your computer and use it in GitHub Desktop.
Random 1/0 Maze matrix
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
class RandomMazeGenerator { | |
constructor(rows, cols) { | |
this.rows = rows; | |
this.cols = cols; | |
this.maze = this.generateRandomMaze(); | |
} | |
generateRandomMaze() { | |
const maze = new Array(this.rows); | |
for (let row = 0; row < this.rows; row++) { | |
maze[row] = new Array(this.cols); | |
for (let col = 0; col < this.cols; col++) { | |
// Assign a random value of 0 or 1 to represent open path or wall | |
maze[row][col] = Math.random() < 0.5 ? 0 : 1; | |
} | |
} | |
return maze; | |
} | |
renderMaze() { | |
const wallChar = '#'; | |
const openPathChar = ' '; | |
let renderedMaze = ''; | |
for (let row = 0; row < this.rows; row++) { | |
for (let col = 0; col < this.cols; col++) { | |
const cell = this.maze[row][col]; | |
const cellChar = cell === 1 ? wallChar : openPathChar; | |
renderedMaze += cellChar; | |
} | |
renderedMaze += '\n'; // Move to the next row | |
} | |
console.log(renderedMaze); | |
} | |
} | |
// Example usage: | |
const rows = 5; | |
const cols = 10; | |
const mazeGenerator = new RandomMazeGenerator(rows, cols); | |
mazeGenerator.renderMaze(); |
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
const require = (()=>{ | |
const loadedScripts = new Set(); | |
const loadingScripts = new Map(); | |
const load = (src, callback) => { | |
if (loadedScripts.has(src)) { | |
callback(); | |
return; | |
} | |
if (loadingScripts.has(src)) { | |
loadingScripts.get(src).push(callback); | |
return; | |
} | |
loadingScripts.set(src, [callback]); | |
const script = (() => { | |
let el; | |
if((/\.css$/i).test(src)){ | |
el = document.createElement('link'); | |
el.rel = 'stylesheet'; | |
el.href = src; | |
}else{ | |
el = document.createElement('script'); | |
el.type = 'text/javascript'; | |
el.src = src; | |
} | |
return el; | |
})(); | |
script.onload = () => { | |
setTimeout(()=>{ | |
loadedScripts.add(src); | |
const callbacks = loadingScripts.get(src); | |
for (const cb of callbacks) { | |
cb(); | |
} | |
loadingScripts.delete(src); | |
}, 50); | |
}; | |
document.head.appendChild(script); | |
}; | |
return (srcL) => Promise.all( | |
(Array.isArray(srcL)?srcL:[srcL]) | |
.map(src => { | |
if (typeof(src) == 'function'){ | |
const F = src; | |
src = new Promise(r => { | |
const tIter = () => { | |
const v = F(); | |
if(v) return r(v) | |
setTimeout(tIter, 300); | |
} | |
tIter(); | |
}); | |
} | |
return (src instanceof Promise) ? src | |
: new Promise(r => load(src, r)) | |
}) | |
); | |
})(); |
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
// Assumes you have included the Three.js library and the PointerLockControls.js | |
async function renderMaze(mazeMatrix) { | |
await require('//unpkg.com/three'); | |
const scene = new THREE.Scene(); | |
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); | |
const renderer = new THREE.WebGLRenderer(); | |
renderer.setSize(window.innerWidth, window.innerHeight); | |
document.body.appendChild(renderer.domElement); | |
// Helper function to create walls | |
const buildWall = (x, z) => { | |
const geometry = new THREE.BoxGeometry(); | |
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); | |
const wall = new THREE.Mesh(geometry, material); | |
wall.position.set(x, 0, z); | |
scene.add(wall); | |
}; | |
// Loop through the matrix and build walls | |
for (let i = 0; i < mazeMatrix.length; i++) { | |
for (let j = 0; j < mazeMatrix[i].length; j++) { | |
if (mazeMatrix[i][j] === 1) { | |
buildWall(i, j); | |
} | |
} | |
} | |
// Camera controls for walking through the maze | |
const controls = new THREE.PointerLockControls(camera, renderer.domElement); | |
document.addEventListener('click', () => controls.lock(), false); | |
// Animation loop | |
const animate = () => { | |
requestAnimationFrame(animate); | |
// Update controls if needed | |
controls.update(); | |
renderer.render(scene, camera); | |
}; | |
animate(); | |
} | |
// Example matrix: 1s are walls, 0s are paths | |
const mazeMatrix = [ | |
[1, 1, 1, 1, 1], | |
[1, 0, 0, 0, 1], | |
[1, 0, 1, 0, 1], | |
[1, 0, 1, 0, 0], | |
[1, 1, 1, 1, 1] | |
]; | |
renderMaze(mazeMatrix); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment