Here is the example maze-solving program. It is divided into a few methods. The maze string uses characters to indicate walls, a start, line separators, and an end.
# The maze string, use "x" for a wall, 1 for start.
maze =
"""
xxx1.
x x .
x2x x.
xxx .
x x .
x xx
""";
def get_maze_lists(maze):
# Split on period.
lines_raw = maze.split(
".")
# Remove newlines.
lines_stripped = list(map(lambda line: line.strip(
"\n"), lines_raw))
# Result 2D array.
result = []
for line in lines_stripped:
# Create row.
row = []
for char in line:
if char ==
"x":
row.append(-1)
elif char ==
"1":
row.append(1)
elif char ==
"2":
row.append(-3)
else:
row.append(0)
# Add row to result.
result.append(row)
return result
def display(maze_lists):
# Display current maze progress.
for row in maze_lists:
line =
""
for column in row:
if column == -1:
line +=
"x"
elif column == 1:
line +=
"1"
elif column == -3:
line +=
"2"
elif column == 0:
line +=
" "
else:
line +=
"."
print(line)
def valid_pos(maze_lists, row, new_row, new_column):
# Determines if coordinates are within range.
if new_row < 0: return False
if new_column < 0: return False
if new_row >= len(maze_lists): return False
if new_column >= len(maze_lists[row]): return False
return True
def modify_path(maze_lists):
# All possible moves.
moves = [[-1, 0],
[0, -1], [0, 1],
[1, 0]]
# Loop over rows and columns.
for row in range(len(maze_lists)):
for column in range(len(maze_lists[row])):
value = maze_lists[row][column]
if value == -1:
# Do nothing on wall.
pass
elif value == -3:
# Do nothing on endpoint.
pass
elif value >= 1:
# For a reached square, add a step number.
for move in moves:
new_row = row + move[0]
new_column = column + move[1]
# Ensure in range.
if valid_pos(maze_lists, row, new_row, new_column):
test_value = maze_lists[new_row][new_column]
# Set move integer in new square.
if test_value == 0:
maze_lists[new_row][new_column] = value + 1
# A move was made.
return 0
elif test_value == -3:
# We are done if we can move to the endpoint.
return 1
# Nothing can be done.
return -1
# Initialize maze lists from string.
data = get_maze_lists(maze)
# Display initial map.
display(data)
# Walk through maze.
count = 0
while True:
value = input()
result = modify_path(data)
if result == 1:
print(
"DONE:", count,
"moves")
break
elif result == -1:
print(
"FAIL:", count,
"moves")
break
else:
display(data)
count += 1
xxx1
x x
x2x x
xxx
x x
x xx
xxx1
x x .
x2x x
xxx
x x
x xx
xxx1
x x ..
x2x x
xxx
x x
x xx
xxx1
x x...
x2x x
xxx
x x
x xx
xxx1
x x...
x2x. x
xxx
x x
x xx
xxx1
x x...
x2x..x
xxx
x x
x xx
xxx1
x x...
x2x..x
xxx.
x x
x xx
xxx1
x x...
x2x..x
xxx..
x x
x xx
xxx1
x x...
x2x..x
xxx..
x .x
x xx
xxx1
x x...
x2x..x
xxx...
x .x
x xx
xxx1
x x...
x2x..x
xxx...
x .x.
x xx
xxx1
x x...
x2x..x
xxx...
x ..x.
x xx
xxx1
x x...
x2x..x
xxx...
x...x.
x xx
xxx1
x x...
x2x..x
xxx...
x...x.
.x xx
xxx1
x x...
x2x..x
xxx...
x...x.
.x.xx
xxx1
x x...
x2x..x
xxx...
x...x.
..x.xx
xxx1
x x...
x2x..x
xxx...
x...x.
...x.xx
xxx1
x x...
x2x..x
xxx...
.x...x.
...x.xx
xxx1
x x...
x2x..x
.xxx...
.x...x.
...x.xx
xxx1
x x...
.x2x..x
.xxx...
.x...x.
...x.xx
xxx1
.x x...
.x2x..x
.xxx...
.x...x.
...x.xx
. xxx1
.x x...
.x2x..x
.xxx...
.x...x.
...x.xx
.. xxx1
.x x...
.x2x..x
.xxx...
.x...x.
...x.xx
...xxx1
.x x...
.x2x..x
.xxx...
.x...x.
...x.xx
...xxx1
.x.x...
.x2x..x
.xxx...
.x...x.
...x.xx
DONE: 24 moves