Class: Maze::Generic
- Inherits:
-
Object
- Object
- Maze::Generic
- Defined in:
- lib/maze/generic.rb
Direct Known Subclasses
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
-
#algorithm ⇒ Object
readonly
Returns the value of attribute algorithm.
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #[](point) ⇒ Object
- #[]=(point, direction) ⇒ Object
- #connect(current_point, next_point, direction) ⇒ Object
- #connected?(point, direction) ⇒ Boolean
- #draw(format, output_file = nil) ⇒ Object
- #generate ⇒ Object
-
#initialize(options) ⇒ Generic
constructor
A new instance of Generic.
- #tessellation ⇒ Object
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() @width = [:width] || 10 @height = [:height] || 10 @grid = Array.new(height) { Array.new(width) } @algorithm = Algorithm::RecursiveBacktracker.new(self) end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
Returns the value of attribute algorithm.
7 8 9 |
# File 'lib/maze/generic.rb', line 7 def algorithm @algorithm end |
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
7 8 9 |
# File 'lib/maze/generic.rb', line 7 def grid @grid end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
7 8 9 |
# File 'lib/maze/generic.rb', line 7 def height @height end |
#width ⇒ Object (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
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 |
#generate ⇒ Object
25 26 27 |
# File 'lib/maze/generic.rb', line 25 def generate tap { algorithm.run } end |
#tessellation ⇒ Object
58 59 60 |
# File 'lib/maze/generic.rb', line 58 def tessellation self.class.name[/::(.*)$/, 1] end |