Class: Mazer::Maze

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

Overview

The Maze

The Maze::Maze class holds a maze.

(see the readme for exemples)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height = nil) ⇒ Maze

Create’s new maze’s instance.

To build a square maze, let the height be nil.



17
18
19
20
# File 'lib/mazer/maze.rb', line 17

def initialize width, height = nil
  height ||= width
  @cells = Array.new(width) { |y| Array.new(height) { |x| 0 } }
end

Instance Attribute Details

#cellsObject (readonly)

Raw array of raw cells.



12
13
14
# File 'lib/mazer/maze.rb', line 12

def cells
  @cells
end

Instance Method Details

#[](x, y) ⇒ Object

Accesses a cell by its coordinates (or nil when coordinates are outside the maze).



23
24
25
# File 'lib/mazer/maze.rb', line 23

def [] x, y
  Cell.new(self, x, y) if y >= 0 and x >= 0 and y < height and x < width
end

#eachObject

Iterates on each cells of the maze, line by line, from left to right.



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

def each # :yields: cell
  width.times { |y| height.times { |x| yield Cell.new(self, x, y) } }
end

#heightObject

Height of the maze.



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

def height
  @cells.length
end

#widthObject

Width of the maze.



28
29
30
# File 'lib/mazer/maze.rb', line 28

def width
  @cells[0].length
end