Class: Mazinator::MazeGenerator
- Inherits:
-
Object
- Object
- Mazinator::MazeGenerator
- Defined in:
- lib/mazinator/maze_generator.rb
Instance Attribute Summary collapse
-
#cols ⇒ Object
readonly
Returns the value of attribute cols.
-
#current ⇒ Object
Returns the value of attribute current.
-
#maze ⇒ Object
readonly
Returns the value of attribute maze.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
-
#stack ⇒ Object
Returns the value of attribute stack.
-
#start ⇒ Object
Returns the value of attribute start.
-
#unvisited ⇒ Object
Returns the value of attribute unvisited.
-
#visited ⇒ Object
Returns the value of attribute visited.
Instance Method Summary collapse
- #generate ⇒ Object
- #get_left_neighbour(cell) ⇒ Object
- #get_lower_neighbour(cell) ⇒ Object
- #get_neighbours(cell) ⇒ Object
- #get_right_neighbour(cell) ⇒ Object
- #get_upper_neighbour(cell) ⇒ Object
-
#initialize(dimension) ⇒ MazeGenerator
constructor
A new instance of MazeGenerator.
- #make_visited(cell) ⇒ Object
- #visit ⇒ Object
- #wreck_wall(cell_a, cell_b) ⇒ Object
Constructor Details
#initialize(dimension) ⇒ MazeGenerator
Returns a new instance of MazeGenerator.
10 11 12 13 14 15 16 17 18 |
# File 'lib/mazinator/maze_generator.rb', line 10 def initialize(dimension) @stack = [] @visited=[] @unvisited = [] @current = nil @rows = dimension @cols = dimension @maze = Matrix.build(@rows, @cols) {|row, col| Mazinator::Cell.new(row, col) } end |
Instance Attribute Details
#cols ⇒ Object (readonly)
Returns the value of attribute cols.
8 9 10 |
# File 'lib/mazinator/maze_generator.rb', line 8 def cols @cols end |
#current ⇒ Object
Returns the value of attribute current.
7 8 9 |
# File 'lib/mazinator/maze_generator.rb', line 7 def current @current end |
#maze ⇒ Object (readonly)
Returns the value of attribute maze.
8 9 10 |
# File 'lib/mazinator/maze_generator.rb', line 8 def maze @maze end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
8 9 10 |
# File 'lib/mazinator/maze_generator.rb', line 8 def rows @rows end |
#stack ⇒ Object
Returns the value of attribute stack.
7 8 9 |
# File 'lib/mazinator/maze_generator.rb', line 7 def stack @stack end |
#start ⇒ Object
Returns the value of attribute start.
7 8 9 |
# File 'lib/mazinator/maze_generator.rb', line 7 def start @start end |
#unvisited ⇒ Object
Returns the value of attribute unvisited.
7 8 9 |
# File 'lib/mazinator/maze_generator.rb', line 7 def unvisited @unvisited end |
#visited ⇒ Object
Returns the value of attribute visited.
7 8 9 |
# File 'lib/mazinator/maze_generator.rb', line 7 def visited @visited end |
Instance Method Details
#generate ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mazinator/maze_generator.rb', line 20 def generate # set entry to the maze self.maze.first.walls[:left] = false # set exit from the maze self.maze[rows-1, cols-1].walls[:right] = false self.current = self.maze.first self.maze.each{ |cell| self.unvisited << cell } while self.unvisited.any? do self.visit end Mazinator::Maze.new(self.rows, self.cols, self.maze) end |
#get_left_neighbour(cell) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/mazinator/maze_generator.rb', line 100 def get_left_neighbour(cell) return nil if (cell.col - 1 < 0) neighbour = self.maze[cell.row, cell.col-1] return nil if self.visited.include?(neighbour) neighbour end |
#get_lower_neighbour(cell) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/mazinator/maze_generator.rb', line 121 def get_lower_neighbour(cell) return nil if (cell.row + 1 >= self.rows) neighbour = self.maze[cell.row+1, cell.col] return nil if self.visited.include?(neighbour) neighbour end |
#get_neighbours(cell) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/mazinator/maze_generator.rb', line 54 def get_neighbours(cell) nbrs = [] nbrs << self.get_left_neighbour(cell) nbrs << self.get_right_neighbour(cell) nbrs << self.get_upper_neighbour(cell) nbrs << self.get_lower_neighbour(cell) nbrs.compact.shuffle end |
#get_right_neighbour(cell) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/mazinator/maze_generator.rb', line 107 def get_right_neighbour(cell) return nil if (cell.col+ 1 >= self.cols) neighbour = self.maze[cell.row, cell.col+1] return nil if self.visited.include?(neighbour) neighbour end |
#get_upper_neighbour(cell) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/mazinator/maze_generator.rb', line 114 def get_upper_neighbour(cell) return nil if (cell.row - 1 < 0) neighbour = self.maze[cell.row-1, cell.col] return nil if self.visited.include?(neighbour) neighbour end |
#make_visited(cell) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/mazinator/maze_generator.rb', line 64 def make_visited(cell) i = self.unvisited.index(cell) # this cell has been visited, return now return false unless i self.visited << cell self.unvisited.delete(cell) end |
#visit ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mazinator/maze_generator.rb', line 35 def visit neighbours = self.get_neighbours(self.current) if neighbours.any? cell = neighbours.first self.stack << self.current wreck_wall(self.current, cell) self.current = cell make_visited(cell) elsif self.stack.any? cell = self.stack.pop self.current = cell else unvisited_len = self.unvisited.length cell = self.unvisited[rand(unvisited_len)] self.current = cell make_visited(cell) end end |
#wreck_wall(cell_a, cell_b) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mazinator/maze_generator.rb', line 73 def wreck_wall(cell_a, cell_b) if cell_a.row == cell_b.row if cell_a.col == (cell_b.col - 1) cell_a.walls[:right] = false cell_b.walls[:left] = false elsif cell_a.col == (cell_b.col + 1) cell_a.walls[:left] = false cell_b.walls[:right] = false else nil end elsif cell_a.col == cell_b.col if cell_a.row == (cell_b.row - 1) cell_a.walls[:down] = false cell_b.walls[:up] = false elsif cell_a.row == (cell_b.row + 1) cell_a.walls[:up] = false cell_b.walls[:down] = false else nil end else # passed random cells, do nothing nil end end |