-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_solver.rb
141 lines (120 loc) · 3.25 KB
/
maze_solver.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class MazeSolver
attr_accessor :maze, :traveled_path, :visited_nodes, :node_queue, :goal_reached
def initialize(maze)
@maze = maze
@traveled_path = []
@visited_nodes = []
@node_queue = []
@goal_reached = false
end
def maze_array
maze_array = []
@maze.split("\n").each do |line|
temp_array = []
line.strip.each_char do |c|
temp_array << c
end
maze_array << temp_array
end
maze_array
end
def get_starting_node
maze_array.each_with_index do |row, row_index|
row.each_with_index do |column, col_index|
return [col_index, row_index] if column == "→"
end
end
end
def neighbors(node)
col_index = node[0]
row_index = node[1]
indicies = [
[col_index-1, row_index],
[col_index+1, row_index],
[col_index, row_index-1],
[col_index, row_index+1]
]
raw_one = maze_array[row_index-1][col_index] if maze_array[row_index-1]
raw_two = maze_array[row_index+1][col_index] if maze_array[row_index+1]
raw_neighbors_list = [
maze_array[row_index][col_index-1],
maze_array[row_index][col_index+1],
raw_one,
raw_two
]
possible_neighbors = raw_neighbors_list.each_with_index.map do |item, i|
indicies[i] if !item.nil? && item != "#"
end.compact
possible_neighbors
end
def setup
self.traveled_path << get_starting_node
self.visited_nodes << get_starting_node
self.node_queue << get_starting_node
end
def visited(node)
self.visited_nodes.include?(node)
end
def goal(node)
col_index = node[0]
row_index = node[1]
maze_array[row_index][col_index] == "@"
end
def add_to_arrays(node)
self.visited_nodes << node
self.node_queue << node
end
def solution_path
solution = []
starting_child = self.traveled_path.last[1]
starting_parent = self.traveled_path.last[0]
solution.unshift(starting_child)
current_child = starting_parent
self.traveled_path.reverse.each_with_index do |pair,i|
if pair[1] == current_child
solution.unshift(pair[1])
current_child = pair[0]
end
end
solution.unshift(current_child)
end
def add_to_path(parent, child)
self.traveled_path << [parent, child]
end
def solve
setup
while !self.node_queue.empty?
node = node_queue.shift
neighbors(node).each do |neighbor|
if !visited(neighbor)
add_to_path(node, neighbor)
add_to_arrays(neighbor)
if goal(neighbor)
self.goal_reached = true
return solution_path
end
end
end
end
if self.goal_reached == false
raise "You will never get out! Muahahahahaha!"
end
end
def print_maze(maze)
printed_maze_str = ""
maze.each do |row|
row.each do |col|
printed_maze_str << col
end
printed_maze_str << "\n"
end
puts printed_maze_str.strip
end
def display_solution_path
copied_maze = maze_array
solution_path[1..-2].each do |coord|
copied_maze[coord[1]][coord[0]] = "."
end
print_maze(copied_maze)
end
end