Class: DLA::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/dla.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#historyObject

Returns the value of attribute history.



9
10
11
# File 'lib/dla.rb', line 9

def history
  @history
end

#ixObject

Returns the value of attribute ix.



9
10
11
# File 'lib/dla.rb', line 9

def ix
  @ix
end

#iyObject

Returns the value of attribute iy.



9
10
11
# File 'lib/dla.rb', line 9

def iy
  @iy
end

#nodesObject

Returns the value of attribute nodes.



9
10
11
# File 'lib/dla.rb', line 9

def nodes
  @nodes
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/dla.rb', line 9

def parent
  @parent
end

#rootObject

Returns the value of attribute root.



9
10
11
# File 'lib/dla.rb', line 9

def root
  @root
end

#xObject

Returns the value of attribute x.



9
10
11
# File 'lib/dla.rb', line 9

def x
  @x
end

#xBoundObject

Returns the value of attribute xBound.



9
10
11
# File 'lib/dla.rb', line 9

def xBound
  @xBound
end

#yObject

Returns the value of attribute y.



9
10
11
# File 'lib/dla.rb', line 9

def y
  @y
end

#yBoundObject

Returns the value of attribute yBound.



9
10
11
# File 'lib/dla.rb', line 9

def yBound
  @yBound
end

Class Method Details

.root(x: nil, y: nil, xBound: 1280, yBound: 720) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/dla.rb', line 11

def self.root(x: nil, y: nil, xBound: 1280, yBound: 720)
  r = Node.new root: nil
  r.x = r.ix = x || xBound / 2
  r.y = r.iy = y || yBound / 2
  r.nodes << r
  r
end

Instance Method Details

#allTreeNodesObject



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

Raises:

  • (ArgumentError)


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

#eastObject



46
47
48
# File 'lib/dla.rb', line 46

def east
  @children[:east]
end

#free?(x, y) ⇒ Boolean

Returns:

  • (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

Raises:

  • (RuntimeError)


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

#northObject



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

#southObject



42
43
44
# File 'lib/dla.rb', line 42

def south
  @children[:south]
end

#to_sObject



107
108
109
# File 'lib/dla.rb', line 107

def to_s
  format('%d,%d(%d,%d)%s', @x, @y, @ix, @iy, @history)
end

#westObject



50
51
52
# File 'lib/dla.rb', line 50

def west
  @children[:west]
end