-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.rb
57 lines (44 loc) · 1.5 KB
/
path.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
require_relative "./tree.rb"
require_relative "./node.rb"
def select_best_active active
active.min_by(&:distance_to_end)
end
def process node, current
node.activate
node.set_distance_to_start START.r, START.c
node.set_distance_to_end FINISH.r, FINISH.c
node.prev = current
end
SEED = [
[" "," "," "," "," "," "].map.with_index { |v, c| Node.new(r: 0, c: c, v: v)},
[" ","*","*"," ","*"," "].map.with_index { |v, c| Node.new(r: 1, c: c, v: v)},
[" "," "," "," ","*"," "].map.with_index { |v, c| Node.new(r: 2, c: c, v: v)},
[" "," ","*","*","*"," "].map.with_index { |v, c| Node.new(r: 3, c: c, v: v)},
[" ","-","*"," "," "," "].map.with_index { |v, c| Node.new(r: 4, c: c, v: v)},
["B"," ","*"," "," ","A"].map.with_index { |v, c| Node.new(r: 5, c: c, v: v)}
]
no_solution = false
tree = Tree.new(SEED)
puts "Processing maze:"
tree.print
# set distance should be in the node processing
START = tree.get_node_from_value "A"
FINISH = tree.get_node_from_value "B"
current = START
current.activate
loop do
current.lock!
tree.nearby(current).each {|node| process(node, current)}
no_solution = true if tree.active == []
break if current === FINISH || no_solution
current = select_best_active(tree.active)
end
until current.prev.nil? || no_solution do
current.v = "•" if current.v == " "
current = current.prev
end
puts "Solution found:"
tree.print unless no_solution
puts "Hey, don't try to trick me. This maze has no solution!" if no_solution
puts "Processed nodes:"
tree.print_processed