From 3aad6edc852a2c111ff1daa3eb86af9108f8cea7 Mon Sep 17 00:00:00 2001 From: gjeck Date: Fri, 8 Sep 2017 17:45:29 -0700 Subject: [PATCH] Add maze sparseness --- src/app/map.js | 2 +- src/app/maze.js | 42 ++++++++++++++++-------------------------- src/app/quadtree.js | 5 ++++- tests/quadtree.test.js | 4 ++-- 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/app/map.js b/src/app/map.js index 40d78ee..b8ab6d5 100644 --- a/src/app/map.js +++ b/src/app/map.js @@ -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) diff --git a/src/app/maze.js b/src/app/maze.js index 15b572a..93c4acb 100644 --- a/src/app/maze.js +++ b/src/app/maze.js @@ -2,10 +2,12 @@ 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) { @@ -13,7 +15,17 @@ function createMazeGenerator() { } 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 } @@ -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 } } diff --git a/src/app/quadtree.js b/src/app/quadtree.js index 601e666..438b5d7 100644 --- a/src/app/quadtree.js +++ b/src/app/quadtree.js @@ -120,4 +120,7 @@ function createQuadTree(spec) { return new QuadTree(maxItems, depth, bounds) } -export { createQuadTree as default } +export { + createQuadTree as default, + QuadTree +} diff --git a/tests/quadtree.test.js b/tests/quadtree.test.js index 46268c8..c2419c8 100644 --- a/tests/quadtree.test.js +++ b/tests/quadtree.test.js @@ -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,