Class: Maze
- Inherits:
-
Object
- Object
- Maze
- Defined in:
- lib/maze/maze.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#height_full ⇒ Object
readonly
Returns the value of attribute height_full.
-
#matrix ⇒ Object
readonly
Returns the value of attribute matrix.
-
#mirrored ⇒ Object
readonly
Returns the value of attribute mirrored.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#width_full ⇒ Object
readonly
Returns the value of attribute width_full.
Instance Method Summary collapse
- #cell(x, y) ⇒ Object
- #generate ⇒ Object
- #hash ⇒ Object
-
#initialize(width, height) ⇒ Maze
constructor
A new instance of Maze.
- #inspect ⇒ Object
- #print ⇒ Object
- #value(i, j) ⇒ Object
- #xy_to_ij(x, y) ⇒ Object
Constructor Details
#initialize(width, height) ⇒ Maze
Returns a new instance of Maze.
33 34 35 36 37 38 39 |
# File 'lib/maze/maze.rb', line 33 def initialize(width, height) @mirrored = false @height, @width = height.to_i, width.to_i @height_full, @width_full = (1 + 2*@height), (1 + 2*@width) initialize_matrix @hash = "#{@width}#{@height}".to_i # optimized pre-computed hash end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
31 32 33 |
# File 'lib/maze/maze.rb', line 31 def height @height end |
#height_full ⇒ Object (readonly)
Returns the value of attribute height_full.
31 32 33 |
# File 'lib/maze/maze.rb', line 31 def height_full @height_full end |
#matrix ⇒ Object (readonly)
Returns the value of attribute matrix.
31 32 33 |
# File 'lib/maze/maze.rb', line 31 def matrix @matrix end |
#mirrored ⇒ Object (readonly)
Returns the value of attribute mirrored.
31 32 33 |
# File 'lib/maze/maze.rb', line 31 def mirrored @mirrored end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
31 32 33 |
# File 'lib/maze/maze.rb', line 31 def width @width end |
#width_full ⇒ Object (readonly)
Returns the value of attribute width_full.
31 32 33 |
# File 'lib/maze/maze.rb', line 31 def width_full @width_full end |
Instance Method Details
#cell(x, y) ⇒ Object
52 53 54 |
# File 'lib/maze/maze.rb', line 52 def cell(x, y) MazeCell.new(self, x, y) end |
#generate ⇒ Object
63 64 65 |
# File 'lib/maze/maze.rb', line 63 def generate self end |
#hash ⇒ Object
67 68 69 |
# File 'lib/maze/maze.rb', line 67 def hash @hash end |
#inspect ⇒ Object
71 72 73 |
# File 'lib/maze/maze.rb', line 71 def inspect "#<#{self.class}: @width=#{@width}, @height=#{@height}>" end |
#print ⇒ Object
41 42 43 44 45 46 |
# File 'lib/maze/maze.rb', line 41 def print @matrix.each do |row| puts row.join(' ').gsub(/0/, ' ').gsub(/1/, '#').gsub(/2/, 'X') end self end |
#value(i, j) ⇒ Object
48 49 50 |
# File 'lib/maze/maze.rb', line 48 def value(i, j) @matrix[i][j] end |
#xy_to_ij(x, y) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/maze/maze.rb', line 56 def xy_to_ij(x, y) x, y = x % @width, y % @height if @mirrored i = 1 + (2 * y) j = 1 + (2 * x) [i, j] end |