Skip to content

Commit

Permalink
add event notification for algorithms where it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis committed Jan 24, 2011
1 parent 8628036 commit ead5da3
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/algorithms/aldous_broder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ class Maze.Algorithms.AldousBroder extends Maze.Algorithm
@updateAt @x, @y
@remaining--
@state = 1
@carvedOnLastStep = true

runStep: ->
carved = false

if @remaining > 0
for dir in @rand.randomDirections()
nx = @x + Maze.Direction.dx[dir]
Expand All @@ -37,6 +40,7 @@ class Maze.Algorithms.AldousBroder extends Maze.Algorithm
@maze.carve x, y, dir
@maze.carve @x, @y, Maze.Direction.opposite[dir]
@remaining--
carved = true

if @remaining == 0
delete @x
Expand All @@ -47,6 +51,9 @@ class Maze.Algorithms.AldousBroder extends Maze.Algorithm

break

@eventAt @x, @y if carved != @carvedOnLastStep
@carvedOnLastStep = carved

return @remaining > 0

step: ->
Expand Down
5 changes: 5 additions & 0 deletions src/algorithms/backtracker.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Maze.Algorithms.RecursiveBacktracker extends Maze.Algorithm
@updateAt x, y
@stack.push x: x, y: y, dirs: @rand.randomDirections()
@state = @RUN
@carvedOnLastStep = true

runStep: ->
loop
Expand All @@ -48,12 +49,16 @@ class Maze.Algorithms.RecursiveBacktracker extends Maze.Algorithm

@maze.carve nx, ny, Maze.Direction.opposite[dir] | @STACK
@updateAt nx, ny
@eventAt nx, ny unless @carvedOnLastStep
@carvedOnLastStep = true
break

if current.dirs.length == 0
@maze.uncarve current.x, current.y, @STACK
@updateAt current.x, current.y
@eventAt current.x, current.y if @carvedOnLastStep
@stack.pop()
@carvedOnLastStep = false
break

@state = @DONE if @stack.length == 0
Expand Down
1 change: 1 addition & 0 deletions src/algorithms/binary_tree.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ class Maze.Algorithms.BinaryTree extends Maze.Algorithm
if @x >= @maze.width
@x = 0
@y++
@eventAt @x, @y

return @y < @maze.height
2 changes: 2 additions & 0 deletions src/algorithms/eller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Maze.Algorithms.Eller extends Maze.Algorithm
@mode = @VERTICAL
@next_state = @state.next()
@verticals = @computeVerticals()
@eventAt 0, @row

computeVerticals: ->
verts = []
Expand All @@ -88,6 +89,7 @@ class Maze.Algorithms.Eller extends Maze.Algorithm
@state = @next_state.populate()
@row += 1
@initializeRow()
@eventAt 0, @row

step: ->
switch @mode
Expand Down
1 change: 1 addition & 0 deletions src/algorithms/houston.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Maze.Algorithms.Houston extends Maze.Algorithm
delete @worker.x
delete @worker.y
@updateAt x, y
@eventAt x, y

# switch to wilsons and redefine the step method so it
# no longer watches the threshold.
Expand Down
2 changes: 2 additions & 0 deletions src/algorithms/hunt_and_kill.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Maze.Algorithms.HuntAndKill extends Maze.Algorithm
delete @x
delete @y
@updateAt x, y # remove highlight from current cell
@eventAt x, y
@y = 0
@callbackRow 0 # highlight the first row
@state = 2
Expand Down Expand Up @@ -75,6 +76,7 @@ class Maze.Algorithms.HuntAndKill extends Maze.Algorithm

# clear highlight in row (because we set @x) and update passages at @x, @y
@callbackRow @y
@eventAt nx, ny

return

Expand Down
1 change: 1 addition & 0 deletions src/algorithms/sidewinder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Maze.Algorithms.Sidewinder extends Maze.Algorithm
@maze.carve cell, @y-1, Maze.Direction.S
@updateAt cell, @y
@updateAt cell, @y-1
@eventAt @x, @y if @x - @runStart > 0
@runStart = @x + 1
else if @x+1 < @maze.width
@maze.carve @x, @y, Maze.Direction.E
Expand Down
2 changes: 2 additions & 0 deletions src/algorithms/wilson.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Maze.Algorithms.Wilson extends Maze.Algorithm
@x = @rand.nextInteger(@maze.width)
@y = @rand.nextInteger(@maze.height)
if @maze.isBlank(@x, @y)
@eventAt @x, @y
@state = 2
@start = x: @x, y: @y
@addVisit @x, @y
Expand All @@ -57,6 +58,7 @@ class Maze.Algorithms.Wilson extends Maze.Algorithm
@x = @start.x
@y = @start.y
@state = 3
@eventAt @x, @y

break

Expand Down

0 comments on commit ead5da3

Please sign in to comment.