Permalink
Browse files

weave mazes via Prim's

  • Loading branch information...
1 parent f4f365e commit 23acb8a0986797817d0ef96a35b5e247aa04b627 @jamis committed Mar 4, 2011
Showing with 28 additions and 9 deletions.
  1. +8 −0 examples/padding.html
  2. +8 −0 src/algorithms/prim.coffee
  3. +12 −9 src/maze.coffee
@@ -7,6 +7,7 @@
<script src="../lib/maze.js" type="text/javascript"></script>
<script src="../lib/widget.js" type="text/javascript"></script>
+ <script src="../lib/algorithms/prim.js" type="text/javascript"></script>
<script src="../lib/algorithms/growing_tree.js" type="text/javascript"></script>
<script type="text/javascript">
@@ -44,6 +45,13 @@
<p>The source code for these demos is freely available at <a href="http://github.com/jamis/csmazes">http://github.com/jamis/csmazes</a>.
<hr />
+ <h1>Prim's Algorithm</h1>
+ <div class="row">
+ <script type="text/javascript">Maze.createWidget("Prim", 5, 5, { "class": "small", "weave": true, "padded": true, "watch": false });</script>
+ <script type="text/javascript">Maze.createWidget("Prim", 15, 15, { "class": "medium", "watch": false, "weave": true, "padded": true, id: "prim_bigger", interval: 25 });</script>
+ </div>
+ <hr />
+
<h1>Growing Tree Algorithm</h1>
<p>
<input type="text" id="growing_tree_input" value="random:100" onchange="resetMazes('growingtree', 'growtree_bigger');" /><br />
@@ -59,6 +59,14 @@ class Maze.Algorithms.Prim extends Maze.Algorithm
nx = cell.x + Maze.Direction.dx[direction]
ny = cell.y + Maze.Direction.dy[direction]
+ if @maze.isWeave && @maze.isPerpendicular(nx, ny, direction)
+ nx2 = nx + Maze.Direction.dx[direction]
+ ny2 = ny + Maze.Direction.dy[direction]
+ if @isInside(nx2, ny2)
+ @performThruWeave nx, ny
+ @updateAt nx, ny
+ [nx, ny] = [nx2, ny2]
+
@maze.carve nx, ny, Maze.Direction.opposite[direction]
@updateAt nx, ny
View
@@ -69,16 +69,8 @@ class Maze.Algorithm
nx = thruX + Maze.Direction.dx[dir]
ny = thruY + Maze.Direction.dy[dir]
@maze.isValid(nx, ny) && @maze.isBlank(nx, ny)
-
- performWeave: (dir, fromX, fromY, callback) ->
- thruX = fromX + Maze.Direction.dx[dir]
- thruY = fromY + Maze.Direction.dy[dir]
- toX = thruX + Maze.Direction.dx[dir]
- toY = thruY + Maze.Direction.dy[dir]
-
- @maze.carve(fromX, fromY, dir)
- @maze.carve(toX, toY, Maze.Direction.opposite[dir])
+ performThruWeave: (thruX, thruY) ->
if @rand.nextBoolean()
@maze.carve(thruX, thruY, Maze.Direction.U)
else if @maze.isNorth(thruX, thruY)
@@ -88,6 +80,17 @@ class Maze.Algorithm
@maze.uncarve(thruX, thruY, Maze.Direction.E|Maze.Direction.W)
@maze.carve(thruX, thruY, Maze.Direction.N|Maze.Direction.S|Maze.Direction.U)
+ performWeave: (dir, fromX, fromY, callback) ->
+ thruX = fromX + Maze.Direction.dx[dir]
+ thruY = fromY + Maze.Direction.dy[dir]
+ toX = thruX + Maze.Direction.dx[dir]
+ toY = thruY + Maze.Direction.dy[dir]
+
+ @maze.carve(fromX, fromY, dir)
+ @maze.carve(toX, toY, Maze.Direction.opposite[dir])
+
+ @performThruWeave(thruX, thruY)
+
callback(toX, toY) if callback
@updateAt fromX, fromY

0 comments on commit 23acb8a

Please sign in to comment.