Class: Maze::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/generic.rb

Direct Known Subclasses

Orthogonal, Sigma

Constant Summary collapse

N =
:north
S =
:south
E =
:east
W =
:west
NE =
:north_east
NW =
:north_west
SE =
:south_east
SW =
:south_west

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Generic

Returns a new instance of Generic.



18
19
20
21
22
23
# File 'lib/maze/generic.rb', line 18

def initialize(options)
  @width     = options[:width]  || 10
  @height    = options[:height] || 10
  @grid      = Array.new(height) { Array.new(width) }
  @algorithm = Algorithm::RecursiveBacktracker.new(self)
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



7
8
9
# File 'lib/maze/generic.rb', line 7

def algorithm
  @algorithm
end

#gridObject (readonly)

Returns the value of attribute grid.



7
8
9
# File 'lib/maze/generic.rb', line 7

def grid
  @grid
end

#heightObject (readonly)

Returns the value of attribute height.



7
8
9
# File 'lib/maze/generic.rb', line 7

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



7
8
9
# File 'lib/maze/generic.rb', line 7

def width
  @width
end

Instance Method Details

#[](point) ⇒ Object



43
44
45
# File 'lib/maze/generic.rb', line 43

def [](point)
  grid[point.y][point.x]
end

#[]=(point, direction) ⇒ Object



38
39
40
41
# File 'lib/maze/generic.rb', line 38

def []=(point, direction)
  grid[point.y][point.x] ||= []
  grid[point.y][point.x] << direction
end

#connect(current_point, next_point, direction) ⇒ Object



33
34
35
36
# File 'lib/maze/generic.rb', line 33

def connect(current_point, next_point, direction)
  self[current_point] = direction
  self[next_point]    = opposite(direction)
end

#connected?(point, direction) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/maze/generic.rb', line 29

def connected?(point, direction)
  self[point].include?(direction)
end

#draw(format, output_file = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/maze/generic.rb', line 47

def draw(format, output_file = nil)
  format     = format.capitalize.to_s
  class_name = Formatter.const_get(format).const_get(tessellation)

  unless output_file
    class_name.new(self).draw
  else
    class_name.new(self).draw(output_file)
  end
end

#generateObject



25
26
27
# File 'lib/maze/generic.rb', line 25

def generate
  tap { algorithm.run }
end

#tessellationObject



58
59
60
# File 'lib/maze/generic.rb', line 58

def tessellation
  self.class.name[/::(.*)$/, 1]
end