forked from bf4/book_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.rb
69 lines (57 loc) · 1.41 KB
/
cell.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#---
# Excerpted from "Mazes for Programmers",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/jbmaze for more book information.
#---
require 'distances'
class Cell
attr_reader :row, :column
attr_accessor :north, :south, :east, :west
def initialize(row, column)
@row, @column = row, column
@links = {}
end
def link(cell, bidi=true)
@links[cell] = true
cell.link(self, false) if bidi
self
end
def unlink(cell, bidi=true)
@links.delete(cell)
cell.unlink(self, false) if bidi
self
end
def links
@links.keys
end
def linked?(cell)
@links.key?(cell)
end
def neighbors
list = []
list << north if north
list << south if south
list << east if east
list << west if west
list
end
def distances
distances = 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
end