Maze solving: Difference between revisions

Content deleted Content added
Line 2,540:
# last coordinate being the one that shall be visited next.
def solve
queue = []
path# =Clean nilup.
reset_visiting_state
 
end
# Enqueue start position.
enqueue_cell @queue, = [], @start_x, @start_y
enqueue_cell([], @start_x, @start_y)
 
end
# Loop as long as there are cells to visit and no solution has
# been found yet.
path = path.dupnil
while !queue.empty? && !path
until path = solve_visit_cell|| @queue.empty?
path = solve_visit_cell
end
end
 
#if Clean up.path
# Mark the cells that make up the shortest path.
reset_visiting_state
returnfor x, y in path
 
puts "No solution found?!" unless @path[x][y] = true
end
 
# Topelse
# Mark the cells that make up the shortest path.
for x, yputs in"No pathsolution found?!"
@path[y][x] = true
end
end
 
private
 
# Maze solving visiting method.
def solve_visit_cell(queue)
# Get the next path.
path = @queue.shift
# The cell to visit is the last entry in the path.
x, y = path.last
 
# Have we reached the end yet?
return path if x == @end_x && y == @end_y
# Yes, we have!
return path
end
 
# Mark cell as visited.
@visited[yx][xy] = true
 
for dx, dy in DIRECTIONS
# Left
new_x = xif - 1dx.nonzero?
# Yes,Left we/ have!Right
if move_valid?(new_x, y) && !@vertical_walls[y][new_x]
enqueue_cell queue, path, new_x, y= x + dx
if move_valid?(new_x, y) && !@vertical_walls[y] [x, new_x].min ][y]
enqueue_cell(path, new_x, y)
new_x = x + 1end
# Left else
# Top / Bottom
new_y = y + 1dy
if move_valid?(x, new_y) && !@horizontal_walls[yx][x [y, new_y].min ]
enqueue_cell(path, x, new_y)
new_y = y - 1end
# Right end
end
 
nil # No solution yet.
# Right
new_x = x + 1
if move_valid?(new_x, y) && !@vertical_walls[y][x]
enqueue_cell queue, path, new_x, y
end
 
# Top
new_y = y - 1
if move_valid?(x, new_y) && !@horizontal_walls[new_y][x]
enqueue_cell queue, path, x, new_y
end
 
# Bottom
new_y = y + 1
if move_valid?(x, new_y) && !@horizontal_walls[y][x]
enqueue_cell queue, path, x, new_y
end
 
# No solution yet.
return nil
end
 
# Enqueue a new coordinate to visit.
def enqueue_cell(queue, path, x, y)
# CopyAdd new coordinates to the current path, addand enqueue the new coordinates and enqueuepath.
#@queue the new<< path. + [[x, y]]
path = path.dup
path << [x, y]
queue << path
end
end
Line 2,622 ⟶ 2,609:
maze = Maze.new 20, 10
maze.solve
maze.print</lang>
 
</lang>
Example output:
<pre>
+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+
| * * | * | | o o o o o * * | | o o | o o o | |
+ + +---+ ---+---+ + + +---+---+--- + + + +---+ + +---+ +---+ +
| * | * | * | * * * | | o | | o | o* | * | | o o | | o | |
+ + ---+ ---+---+---+ ---+---+---+---+---+ + +---+ +---+ +---+ ---+---+---+ + + +---+
| * | | | | o o* * | o* o* | * | * * | | | | | o o | |
+ + +---+---+ +---+---+ + + +---+---+ ---+ +---+ + + +---+---+---+ +
| * | | | * * | * | o* | o* o* o* | * | | | | | o o o | o |
+ +---+---+ +---+---+ + ---+---+---+ +---+---+ +---+ +---+---+---+---+ + +
| * * * * | * * | | | o* | o | | o | | | o | o | o |
+---+---+ +---+ + + ---+---+ +---+ + +---+---+---+---+ +---+---+ + + +
| | | * * | | o* o* | o | o o | | | | o o | o | o |
+ + +---+ +---+---+--- +---+---+ +---+---+ + +---+--- + +---+---+---+ +---+
| | | * * | * * * | * * | | o | | o | | | o | |
+ +---+ +--- +--- +---+--- +---+ + +---+ + ---+---+ +---+ + +---+ +---+ +
| | * | * * | * * | * * | | | o o o | o o | | | o | |
+ + + +---+---+ + + +---+---+--- +---+ +---+ +---+---+ + +--- +--- + + +
| | | * | | * | * * * | o o o| o | o o | o | o | o | | | |
+ +---+ +---+ +--- + +---+ ---+ +---+---+---+ +---+ +---+--- +---+---+ +
| | * * | * * | o o| o | o o o | |
+---+ +---+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+---+
</pre>
 
 
=={{header|Tcl}}==