Module: MazeBase

Included in:
MazeDFS, MazePrim
Defined in:
lib/willb-mazegen/maze.rb

Overview

common infrastructure

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cgObject (readonly)

Returns the value of attribute cg.



12
13
14
# File 'lib/willb-mazegen/maze.rb', line 12

def cg
  @cg
end

Instance Method Details

#closed_sides(cell) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/willb-mazegen/maze.rb', line 22

def closed_sides(cell)
  cx,cy = @cg.coords_for_cell(cell)
  positions = {:left=>[-1,0], :right=>[+1,0], :top=>[0,-1], :bottom=>[0,+1]}
  result = positions.inject([]) do |acc, pair| 
    dir = pair[0]
    dx,dy = pair[1]
    candidate_coords = [cx+dx,cy+dy]
    acc << dir if !(cg.in_graph(*candidate_coords)) || (!cg.edges_from(cell).map {|s,d| d}.include? @cg.cell_for_coords(*candidate_coords))
    acc
  end
  
  result -= [:top] if cell == @startcell
  result -= [:bottom] if cell == @endcell
  
  result
end

#initialize(x, y) ⇒ Object



6
7
8
9
10
# File 'lib/willb-mazegen/maze.rb', line 6

def initialize(x,y)
  @cg = CellGraph.new(x,y)
  @startcell = 0
  @endcell = (@cg.x * @cg.y) - 1
end

#sizeObject



14
15
16
# File 'lib/willb-mazegen/maze.rb', line 14

def size 
  [@cg.x,@cg.y]
end

#walls_for(cell) ⇒ Object



18
19
20
# File 'lib/willb-mazegen/maze.rb', line 18

def walls_for(cell)
  @cg.neighbors(cell).map {|nbc| [cell,nbc]} - @cg.edges_from(cell)
end