Class: DLA::Node
- Inherits:
-
Object
- Object
- DLA::Node
- Defined in:
- lib/dla.rb
Instance Attribute Summary collapse
-
#history ⇒ Object
Returns the value of attribute history.
-
#ix ⇒ Object
Returns the value of attribute ix.
-
#iy ⇒ Object
Returns the value of attribute iy.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#root ⇒ Object
Returns the value of attribute root.
-
#x ⇒ Object
Returns the value of attribute x.
-
#xBound ⇒ Object
Returns the value of attribute xBound.
-
#y ⇒ Object
Returns the value of attribute y.
-
#yBound ⇒ Object
Returns the value of attribute yBound.
Class Method Summary collapse
Instance Method Summary collapse
- #allTreeNodes ⇒ Object
- #at(x, y) ⇒ Object
- #attach(node, position) ⇒ Object
- #east ⇒ Object
- #free?(x, y) ⇒ Boolean
-
#initialize(root:, xBound: 1280, yBound: 720) ⇒ Node
constructor
A new instance of Node.
- #move(direction = nil) ⇒ Object
- #north ⇒ Object
- #oppositeDirection(dir) ⇒ Object
- #randomFreePos(forcedX: nil, forcedY: nil) ⇒ Object
- #south ⇒ Object
- #to_s ⇒ Object
- #west ⇒ Object
Constructor Details
#initialize(root:, xBound: 1280, yBound: 720) ⇒ Node
Returns a new instance of Node.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dla.rb', line 19 def initialize(root:, xBound: 1280, yBound: 720) @root = root || self @xBound = xBound @yBound = yBound @nodes = [] @x, @y = randomFreePos @ix, @iy = @x, @y @children = {} @history = String.new end |
Instance Attribute Details
#history ⇒ Object
Returns the value of attribute history.
9 10 11 |
# File 'lib/dla.rb', line 9 def history @history end |
#ix ⇒ Object
Returns the value of attribute ix.
9 10 11 |
# File 'lib/dla.rb', line 9 def ix @ix end |
#iy ⇒ Object
Returns the value of attribute iy.
9 10 11 |
# File 'lib/dla.rb', line 9 def iy @iy end |
#nodes ⇒ Object
Returns the value of attribute nodes.
9 10 11 |
# File 'lib/dla.rb', line 9 def nodes @nodes end |
#parent ⇒ Object
Returns the value of attribute parent.
9 10 11 |
# File 'lib/dla.rb', line 9 def parent @parent end |
#root ⇒ Object
Returns the value of attribute root.
9 10 11 |
# File 'lib/dla.rb', line 9 def root @root end |
#x ⇒ Object
Returns the value of attribute x.
9 10 11 |
# File 'lib/dla.rb', line 9 def x @x end |
#xBound ⇒ Object
Returns the value of attribute xBound.
9 10 11 |
# File 'lib/dla.rb', line 9 def xBound @xBound end |
#y ⇒ Object
Returns the value of attribute y.
9 10 11 |
# File 'lib/dla.rb', line 9 def y @y end |
#yBound ⇒ Object
Returns the value of attribute yBound.
9 10 11 |
# File 'lib/dla.rb', line 9 def yBound @yBound end |
Class Method Details
Instance Method Details
#allTreeNodes ⇒ Object
54 55 56 |
# File 'lib/dla.rb', line 54 def allTreeNodes @root.nodes end |
#at(x, y) ⇒ Object
30 31 32 |
# File 'lib/dla.rb', line 30 def at(x, y) allTreeNodes.find { |n| n.x == x && n.y == y } end |
#attach(node, position) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/dla.rb', line 58 def attach(node, position) raise ArgumentError, 'position must be :north, :south, :east, :west' unless [:north, :south, :east, :west].include? position @children[position] = node node.parent = self raise ArgumentError, 'Node is already present' if allTreeNodes.include? node @root.nodes << node end |
#east ⇒ Object
46 47 48 |
# File 'lib/dla.rb', line 46 def east @children[:east] end |
#free?(x, y) ⇒ Boolean
34 35 36 |
# File 'lib/dla.rb', line 34 def free?(x, y) !allTreeNodes.any? { |n| n.x == x && n.y == y } end |
#move(direction = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/dla.rb', line 87 def move(direction = nil) raise RuntimeError if @parent raise ArgumentError unless (direction.nil? || [:north, :south, :east, :west].include?(direction)) dir = direction || [:north, :south, :east, :west].sample destV = { north: [0, 1], south: [0, -1], east: [1, 0], west: [-1, 0] }[dir] @history << dir.to_s[0] if (contact = @root.at(@x + destV[0], @y + destV[1])) contact.attach(self, oppositeDirection(dir)) else @x += destV[0] @y += destV[1] end end |
#north ⇒ Object
38 39 40 |
# File 'lib/dla.rb', line 38 def north @children[:north] end |
#oppositeDirection(dir) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/dla.rb', line 78 def oppositeDirection(dir) { north: :south, south: :north, east: :west, west: :east }[dir] end |
#randomFreePos(forcedX: nil, forcedY: nil) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/dla.rb', line 68 def randomFreePos(forcedX: nil, forcedY: nil) x = y = 0 loop do x = rand @xBound y = rand @yBound break unless @root.at x, y end [x, y] end |
#south ⇒ Object
42 43 44 |
# File 'lib/dla.rb', line 42 def south @children[:south] end |
#to_s ⇒ Object
107 108 109 |
# File 'lib/dla.rb', line 107 def to_s format('%d,%d(%d,%d)%s', @x, @y, @ix, @iy, @history) end |
#west ⇒ Object
50 51 52 |
# File 'lib/dla.rb', line 50 def west @children[:west] end |