blob: e5fd87ed529481198baf8c8e2a053a14a7447a48 (
plain)
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
|
require_relative 'pos'
require_relative 'maze'
class MazeSolver
attr_reader :maze
attr_reader :stack
def initialize(maze, start_pos, end_pos)
@maze = maze
@start_pos = start_pos
@end_pos = end_pos
@stack = [start_pos]
@visitedTiles = Array.new(@maze.width) { Array.new(@maze.height) { false } }
end
def solve!()
if ENV["DEBUG"] == "visual"
print "\e[?1049h" # Save the state of the terminal
print "\e[2J" # Clear the screen
print "\e[0;0H" # Move the cursor to 0, 0
print "\n [ \e[1;35mSolving maze...\e[0m ]\n\n" # Print some nice graphics
print "\e[s" # Save the cursor position
end
while @stack.last != @end_pos
step()
if ENV["DEBUG"] == "visual"
print "\e[u" # Restore the cursor position
print @maze.to_s(" ", @stack)
print "\n\n"
puts " Stack size: #{@stack.length}"
end
end
if ENV["DEBUG"] == "visual"
print "\e[?1049l" # Restore the state of the terminal
end
end
def step()
current_tile = @stack.last
neighbors = @maze.open_neighbors(current_tile)
neighbors.select! do |neighbor|
@visitedTiles[neighbor.x][neighbor.y] == false
end
if neighbors.empty?
@stack.pop()
puts "Popping to #{stack.last}" if ENV["DEBUG"] == "log"
else
randomNeighbor = neighbors.sample
@stack.push(randomNeighbor)
puts "Pushing to #{stack.last}" if ENV["DEBUG"] == "log"
@visitedTiles[randomNeighbor.x][randomNeighbor.y] = true
end
end
end
|