Class: Mazer::Cell
- Inherits:
-
Object
- Object
- Mazer::Cell
- Defined in:
- lib/mazer/cell.rb
Overview
Maze Cells
Cells are generated on the fly to provide wrappers accessing raw maze data in a more user-friendly way.
(see the README for examples)
Convenience methods are added for direction-related methods, for example :
- cell.north
-
> cell.relative :north
- cell.door_south
-
> cell.door :south
- cell.open_east
-
> cell.open :east
- cell.close_west
-
> cell.close :west
Constant Summary collapse
- CONVERSION =
Conversions from direction symbol to binary direction.
{ :north => 1, :west => 2, :south => 4, :east => 8 }
- DIRECTIONS =
Directions expressed in coordinate vectors.
{ :north => [0, -1], :west => [-1, 0], :south => [0, 1], :east => [1, 0] }
- OPPOSITION =
Directions opposed.
{ :north => :south, :west => :east, :south => :north, :east => :west }
Instance Attribute Summary collapse
-
#maze ⇒ Object
readonly
Cell’s maze.
-
#x ⇒ Object
readonly
Cell’s coordinates.
-
#y ⇒ Object
readonly
Cell’s coordinates.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#close(direction) ⇒ Object
Closes the door in specified direction.
-
#door(direction) ⇒ Object
Access the cell through specified door if there is such door.
-
#doors ⇒ Object
Access the list of this cell’s door (as an array of symboles).
-
#initialize(maze, x, y) ⇒ Cell
constructor
:nodoc:.
-
#open(direction) ⇒ Object
Opens the door in specified direction.
-
#relative(direction) ⇒ Object
Access the cell’s direct neighbour in specified direction.
Constructor Details
#initialize(maze, x, y) ⇒ Cell
:nodoc:
26 27 28 29 30 |
# File 'lib/mazer/cell.rb', line 26 def initialize maze, x, y # :nodoc: @maze = maze @x = x @y = y end |
Instance Attribute Details
#maze ⇒ Object (readonly)
Cell’s maze.
24 25 26 |
# File 'lib/mazer/cell.rb', line 24 def maze @maze end |
#x ⇒ Object (readonly)
Cell’s coordinates.
22 23 24 |
# File 'lib/mazer/cell.rb', line 22 def x @x end |
#y ⇒ Object (readonly)
Cell’s coordinates.
22 23 24 |
# File 'lib/mazer/cell.rb', line 22 def y @y end |
Instance Method Details
#==(other) ⇒ Object
71 72 73 |
# File 'lib/mazer/cell.rb', line 71 def == other other.maze == @maze and other.x == @x and other.y == @y end |
#close(direction) ⇒ Object
Closes the door in specified direction.
62 63 64 65 66 67 68 69 |
# File 'lib/mazer/cell.rb', line 62 def close direction if door(direction) self.bin = bin - (bin & CONVERSION[direction]) other_cell = relative(direction) other_cell.close OPPOSITION[direction] if other_cell nil end end |
#door(direction) ⇒ Object
Access the cell through specified door if there is such door.
39 40 41 42 |
# File 'lib/mazer/cell.rb', line 39 def door direction movement = DIRECTIONS[direction] ((bin & CONVERSION[direction]) > 0) and (@maze[@x + movement.first, @y + movement.last] or true) end |
#doors ⇒ Object
Access the list of this cell’s door (as an array of symboles).
45 46 47 |
# File 'lib/mazer/cell.rb', line 45 def doors CONVERSION.collect { |d, b| ((bin & b) > 0) ? d : nil }.compact end |
#open(direction) ⇒ Object
Opens the door in specified direction.
Returns the opened door’s cell or true when opening on the border.
52 53 54 55 56 57 58 59 |
# File 'lib/mazer/cell.rb', line 52 def open direction if not door(direction) self.bin = bin | CONVERSION[direction] other_cell = relative(direction) other_cell.open OPPOSITION[direction] if other_cell other_cell or true end end |
#relative(direction) ⇒ Object
Access the cell’s direct neighbour in specified direction.
33 34 35 36 |
# File 'lib/mazer/cell.rb', line 33 def relative direction x, y = DIRECTIONS[direction] @maze[@x + x, @y + y] end |