Class: Amaze::Cell
- Inherits:
-
Object
show all
- Defined in:
- lib/amaze/cell.rb
Defined Under Namespace
Classes: Hex, Octo, Polar, Square
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(row, column) ⇒ Cell
Returns a new instance of Cell.
11
12
13
14
15
16
|
# File 'lib/amaze/cell.rb', line 11
def initialize row, column
@row = row
@column = column
@links = {}
end
|
Instance Attribute Details
#column ⇒ Object
The position of the cell in the grid
9
10
11
|
# File 'lib/amaze/cell.rb', line 9
def column
@column
end
|
#row ⇒ Object
The position of the cell in the grid
9
10
11
|
# File 'lib/amaze/cell.rb', line 9
def row
@row
end
|
Instance Method Details
#distances ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/amaze/cell.rb', line 43
def distances
distances = Amaze::Distances.new self
frontier = [self]
while frontier.any?
new_frontier = []
frontier.each do |cell|
cell.links.each do |linked|
next if distances[linked]
distances[linked] = distances[cell] + 1
new_frontier << linked
end
end
frontier = new_frontier
end
distances
end
|
#inspect ⇒ Object
35
36
37
|
# File 'lib/amaze/cell.rb', line 35
def inspect
"cell(#{row},#{column})"
end
|
#link(cell, bidi = true) ⇒ Object
22
23
24
25
|
# File 'lib/amaze/cell.rb', line 22
def link cell, bidi=true
@links[cell] = true
cell.link self, false if bidi
end
|
#linked?(cell) ⇒ Boolean
27
28
29
|
# File 'lib/amaze/cell.rb', line 27
def linked? cell
@links.key? cell
end
|
#linked_to?(direction) ⇒ Boolean
31
32
33
|
# File 'lib/amaze/cell.rb', line 31
def linked_to? direction
@links.key? self.send(direction)
end
|
#links ⇒ Object
18
19
20
|
# File 'lib/amaze/cell.rb', line 18
def links
@links.keys
end
|
#to_s ⇒ Object
39
40
41
|
# File 'lib/amaze/cell.rb', line 39
def to_s
"(#{row},#{column})"
end
|