Skip to content

Commit

Permalink
Add maze sparseness
Browse files Browse the repository at this point in the history
  • Loading branch information
gjeck committed Sep 9, 2017
1 parent 65ea495 commit 3aad6ed
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/app/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function createMap(spec) {
const tileSize = s.tileSize || 200
const exit = s.exit || { row: randomIntInRange(0, rows), col: cols - 1 }

maze[exit.row][exit.col] = 0
maze[exit.row][exit.col] = Direction.none

const wallDimension = () => {
return Math.ceil(tileSize / 8)
Expand Down
42 changes: 16 additions & 26 deletions src/app/maze.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ import { shuffle } from './utils'
import Direction from './direction'

function createMazeGenerator() {
const generate = (rows, cols, px, py) => {
let x = px || 0
let y = py || 0
const generate = (rows, cols, px, py, sparseness) => {
const x = px || 0
const y = py || 0
const s = sparseness || 0.09
const grid = []

for (let i = 0; i < rows; ++i) {
let cells = []
for (let j = 0; j < cols; ++j) {
cells.push(0)
}
grid.push(cells)
}

carve(x, y, grid)

for (let i = 1; i < rows - 1; ++i) {
for (let j = 1; j < cols - 1; ++j) {
if (Math.random() < s) {
grid[i][j] = Direction.none
}
}
}

return grid
}

Expand All @@ -35,30 +47,8 @@ function createMazeGenerator() {
})
}

const asString = (grid) => {
let s = ' '
for (let i = 0; i < grid.length * 2 - 1; ++i) {
s += '_'
}
s += '\n'
for (let y = 0; y < grid.length; ++y) {
s += '|'
for (let x = 0; x < grid.length; ++x) {
s += ((grid[y][x] & Direction.s) !== 0) ? ' ' : '_'
if ((grid[y][x] & Direction.e) !== 0) {
s += (((grid[y][x] | grid[y][x + 1]) & Direction.s) !== 0) ? ' ' : '_'
} else {
s += '|'
}
}
s += '\n'
}
return s
}

return {
generate: generate,
asString: asString
generate: generate
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/app/quadtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@ function createQuadTree(spec) {
return new QuadTree(maxItems, depth, bounds)
}

export { createQuadTree as default }
export {
createQuadTree as default,
QuadTree
}
4 changes: 2 additions & 2 deletions tests/quadtree.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import QuadTree from '../src/app/quadtree'
import createQuadTree from '../src/app/quadtree'
import createBoundingRect from '../src/app/bounding_rect'

describe('a quadtree', () => {
let quadtree
beforeEach(() => {
quadtree = QuadTree({
quadtree = createQuadTree({
maxItems: 4,
x: 0,
y: 0,
Expand Down

0 comments on commit 3aad6ed

Please sign in to comment.