Maze solving: Difference between revisions
Content added Content deleted
Line 2,540: | Line 2,540: | ||
# last coordinate being the one that shall be visited next. |
# last coordinate being the one that shall be visited next. |
||
def solve |
def solve |
||
# Clean up. |
|||
⚫ | |||
⚫ | |||
# Enqueue start position. |
# Enqueue start position. |
||
@queue = [] |
|||
enqueue_cell([], @start_x, @start_y) |
|||
⚫ | |||
# Loop as long as there are cells to visit and no solution has |
# Loop as long as there are cells to visit and no solution has |
||
# been found yet. |
# been found yet. |
||
⚫ | |||
while !queue.empty? && !path |
|||
until path || @queue.empty? |
|||
path = solve_visit_cell |
|||
end |
end |
||
⚫ | |||
if path |
|||
⚫ | |||
⚫ | |||
⚫ | |||
@path[x][y] = true |
|||
⚫ | |||
⚫ | |||
⚫ | |||
puts "No solution found?!" |
|||
@path[y][x] = true |
|||
end |
end |
||
end |
end |
||
private |
private |
||
# Maze solving visiting method. |
# Maze solving visiting method. |
||
def solve_visit_cell |
def solve_visit_cell |
||
# Get the next path. |
# Get the next path. |
||
path = queue.shift |
path = @queue.shift |
||
# The cell to visit is the last entry in the path. |
# The cell to visit is the last entry in the path. |
||
x, y = path.last |
x, y = path.last |
||
# Have we reached the end yet? |
# Have we reached the end yet? |
||
if x == @end_x && y == @end_y |
return path if x == @end_x && y == @end_y |
||
⚫ | |||
⚫ | |||
⚫ | |||
# Mark cell as visited. |
# Mark cell as visited. |
||
@visited[ |
@visited[x][y] = true |
||
for dx, dy in DIRECTIONS |
|||
# Left |
|||
if dx.nonzero? |
|||
⚫ | |||
⚫ | |||
new_x = x + dx |
|||
⚫ | |||
enqueue_cell(path, new_x, y) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
enqueue_cell(path, x, new_y) |
|||
⚫ | |||
⚫ | |||
end |
end |
||
⚫ | |||
⚫ | |||
⚫ | |||
if move_valid?(new_x, y) && !@vertical_walls[y][x] |
|||
enqueue_cell queue, path, new_x, y |
|||
⚫ | |||
⚫ | |||
⚫ | |||
if move_valid?(x, new_y) && !@horizontal_walls[new_y][x] |
|||
enqueue_cell queue, path, x, new_y |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
enqueue_cell queue, path, x, new_y |
|||
⚫ | |||
⚫ | |||
return nil |
|||
end |
end |
||
# Enqueue a new coordinate to visit. |
# Enqueue a new coordinate to visit. |
||
def enqueue_cell( |
def enqueue_cell(path, x, y) |
||
# |
# Add new coordinates to the current path and enqueue the new path. |
||
@queue << path + [[x, y]] |
|||
⚫ | |||
path << [x, y] |
|||
queue << path |
|||
end |
end |
||
end |
end |
||
Line 2,622: | Line 2,609: | ||
maze = Maze.new 20, 10 |
maze = Maze.new 20, 10 |
||
maze.solve |
maze.solve |
||
maze.print |
maze.print</lang> |
||
</lang> |
|||
Example output: |
Example output: |
||
<pre> |
<pre> |
||
+---+---+---+---+---+---+---+---+---+---+---+---+--- |
+---+---+---+---+---+ +---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
||
| | | |
| * * | * | | * * | | | | |
||
+ +---+ |
+ + +---+---+---+ + + +---+---+ + + + +---+ + +---+ +---+ |
||
| |
| * | * * * * * | | * | * | | | | |
||
+ + |
+ +---+---+---+---+---+---+---+---+---+ + +---+---+ +---+---+---+---+ + |
||
| |
| * | * * | * * * | * * | | | | |
||
+ + +---+--- |
+ + +---+---+---+---+ + + +---+---+---+ + + + +---+---+---+ + |
||
| |
| * | | * * | * * | * * * * | | | | |
||
+ +---+--- |
+ +---+---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+---+ + + |
||
| | |
| * * * * | * * | | * | | | | | |
||
+---+--- |
+---+---+---+ + +---+---+ +---+ + +---+---+---+ +---+---+ + + + |
||
| |
| | * * | | * * | | | | | | | |
||
+ + +--- |
+ + +---+---+---+ +---+---+ +---+ + +---+ + +---+---+---+ +---+ |
||
| | | |
| | | * * | * * * | * * | | | | | | |
||
+---+ + + + +---+ + +---+ +---+---+ +---+ + +---+ +---+ + |
|||
| | | | |
| | * | * * | * * | * * | | | | | | | | |
||
+ + +---+--- |
+ + + +---+---+ +---+---+ +---+ + +---+---+ + + + + + + |
||
| | |
| | | * | | * | * * * | | | | | | | | |
||
+ + +---+ + |
+ +---+ +---+ + + +---+---+ +---+---+---+ +---+ + +---+---+ + |
||
| |
| * * | * * | | | |
||
+---+---+---+---+---+---+---+---+---+--- |
+---+ +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
||
</pre> |
</pre> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |