-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmazesolver.rb
More file actions
131 lines (108 loc) · 2.85 KB
/
mazesolver.rb
File metadata and controls
131 lines (108 loc) · 2.85 KB
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
require 'pry'
require 'colorize'
require_relative 'mazegenerator'
class MazeSolver
attr_accessor :traveled_path, :node_queue, :visited_nodes, :maze, :solution_path, :matrix
def initialize (num)
maze= MazeGenerator.new(num)
@maze = maze.to_s
@traveled_path = [ ]
@node_queue = [ ]
@visited_nodes = [ ]
@possible_nodes = [ ]
@solution_path = [ ]
end
def matrix
@matrix = @maze.split("\n").map do |row|
row.strip.split("")
end
end
def find_start
self.matrix.each_index do |y|
x = self.matrix[y].index("→")
return [x,y] if x
end
end
def find_end
self.matrix.each_index do |y|
x = self.matrix[y].index( "@" )
return [x,y] if x
end
end
def valid?(move)
find_char(move) != "█"
end
def goleft(move)
return [move[0]-1,move[1]]
end
def goright(move)
return [move[0]+1,move[1]]
end
def goup(move)
return [move[0],move[1]+1]
end
def godown(move)
return [move[0],move[1]-1]
end
def find_char(move)
return self.matrix[move[1]][move[0]]
end
def solve
@node_queue << self.find_start
@traveled_path << [ self.find_start, self.find_start ]
@visited_nodes << self.find_start
found_exit = false
while !@node_queue.empty? && !found_exit
move = @node_queue.shift
positions = [goright(move), goup(move), godown(move), goleft(move)]
if positions.include?(self.find_end)
@traveled_path << [ self.find_end, move ]
found_exit = true
else
positions.each do |next_move|
if valid?(next_move) && !@visited_nodes.include?(next_move)
@visited_nodes << next_move
if find_char(next_move) != "█"
@node_queue << next_move
@traveled_path << [ next_move, move ]
end
end
end
end
end
self.solution_path
self.display_solution_path
end
def solution_path
move_pair = @traveled_path.pop
@solution_path << move_pair[0]
@traveled_path.reverse!
while move_pair[0] != self.find_start
@traveled_path.each do |prev_pair|
if prev_pair[0] == move_pair[1]
@solution_path << prev_pair[0]
move_pair = prev_pair
break
end
end
end
@solution_path.reverse
end
def display_solution_path
maze_array = self.matrix
self.solution_path.each do |position|
maze_array[position[1]][position[0]] = "≡".colorize( :background => :red)
end
maze_array[self.find_start[1]][self.find_start[0]] = "→"
maze_array[self.find_end[1]][self.find_end[0]] = "@"
flat_maze = ""
maze_array.each do |maze_row|
flat_row = maze_row.join("")
flat_maze += flat_row + "\n"
end
flat_maze = flat_maze.chomp
puts flat_maze.colorize(:blue)
end
end
maze = MazeSolver.new(50)
maze.solve