-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve_maze.py
66 lines (56 loc) · 1.94 KB
/
solve_maze.py
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
"""
This uses the 'pc_maze.py' file as a module, which creates a maze with 16
'blocks'. This then shows two ways of using recursion to find the exit to
the maze. The first one, 'leads_to_exit()', outputs all the steps taken
in finding the route through the maze. The second one, 'find_the_exit()',
only outputs the path it took to find the way out.
The purpose of this exercise is to show using recursion in functions. It's
not something that should be done often, but can have some advantages when
used properly.
"""
from pc_maze import the_entrance, the_exit, connected
def leads_to_exit(comingfrom, cell, depth):
"""
This shows every step of the process of finding the correct
path. It indents each search as it goes to make it clear where
it has searched.
"""
indent = depth * 4 * " "
if cell == the_exit():
return True
for i in range(the_entrance(), the_exit() + 1):
if i == comingfrom:
continue
if not connected(cell, i):
continue
print(f"{indent} Check connection {cell} -> {i}")
if leads_to_exit(cell, i, depth + 1):
print(f"{indent} Path found: {cell} -> {i}")
return True
return False
if leads_to_exit(0, the_entrance(), 0):
print("Path found!")
else:
print("Path not found")
print()
def find_the_exit(comingfrom, cell):
"""
This shows the steps taken to find the exit as a list, it does
not show all the steps, only the solution.
"""
if cell == the_exit():
return "{}".format(the_exit())
for i in range(the_entrance(), the_exit() + 1):
if i == comingfrom:
continue
if not connected(cell, i):
continue
check = find_the_exit(cell, i)
if check != "":
return "{} -> {}".format(cell, check)
return ""
checked = find_the_exit(0, the_entrance())
if checked != "":
print(f"Path found! {checked}")
else:
print("Path not found")