Class: RecursiveMaze

Inherits:
Object
  • Object
show all
Defined in:
lib/my_own_maze/generator/recursive_maze.rb

Constant Summary collapse

DIRS =
{
  'l' => [-1, 0],
  'r' => [1, 0],
  'u' => [0, -1],
  'd' => [0, 1]
}

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ RecursiveMaze

Returns a new instance of RecursiveMaze.



10
11
12
13
14
15
16
17
# File 'lib/my_own_maze/generator/recursive_maze.rb', line 10

def initialize(width, height)
  @width = width
  @height = height
  @start_point = Point.new(0, 1)
  @end_point = nil
  @history = [Point.new(1, 1)]
  @points = [@start_point, @history[0]]
end

Instance Method Details

#generateObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/my_own_maze/generator/recursive_maze.rb', line 19

def generate
  while @history.size > 0
    point = @history.last
    result = false
    dirs = []
    while(!result && dirs.size != DIRS.keys.size)
      next_step = DIRS.keys.sample
      while dirs.include?(next_step)
        next_step = DIRS.keys.sample
      end
      dirs << next_step
      result = go(next_step, point)
    end
    unless result
      @history.pop
    end
  end
  @points << @end_point if @end_point
  Maze.new(@width, @height, @points, @start_point, @end_point)
end