Class: Mazer::Maze
- Inherits:
-
Object
- Object
- Mazer::Maze
- 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
-
#cells ⇒ Object
readonly
Raw array of raw cells.
Instance Method Summary collapse
-
#[](x, y) ⇒ Object
Accesses a cell by its coordinates (or nil when coordinates are outside the maze).
-
#each ⇒ Object
Iterates on each cells of the maze, line by line, from left to right.
-
#height ⇒ Object
Height of the maze.
-
#initialize(width, height = nil) ⇒ Maze
constructor
Create’s new maze’s instance.
-
#width ⇒ Object
Width of the maze.
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
#cells ⇒ Object (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 |
#each ⇒ Object
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 |
#height ⇒ Object
Height of the maze.
33 34 35 |
# File 'lib/mazer/maze.rb', line 33 def height @cells.length end |
#width ⇒ Object
Width of the maze.
28 29 30 |
# File 'lib/mazer/maze.rb', line 28 def width @cells[0].length end |