-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMazeSolver.py
More file actions
94 lines (83 loc) · 3.75 KB
/
MazeSolver.py
File metadata and controls
94 lines (83 loc) · 3.75 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#PYTHON_MAZE_SOLVER
ASCII_MAZE1 = """
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| | | |
+ +--+ + +--+--+ +--+--+--+--+--+--+--+--+--+ +--+--+--+ +
| | | | | | | | |
+--+ + +--+ +--+--+ + + + + +--+--+--+ +--+ + +--+--+
| | | | | | | | | | | | |
+ +--+ + +--+ +--+--+--+--+--+--+ +--+--+ +--+--+--+ +--+
| | | | | | | | | | | | |
+ +--+--+ +--+ + +--+--+--+ E + + + +--+ + +--+--+ +
| | | | | | | | | | | |
+ +--+ + + +--+--+ +--+ + +--+--+--+--+ + +--+--+--+--+
| | | | | | | | | | |
+ + +--+ +--+--+--+--+ + +--+--+--+--+ + +--+ +--+ +--+
| | | | | | | | | |
+ + + +--+--+--+--+--+--+--+ +--+--+ +- +--+ + + +--+ +
| | | | | | | | | |
+ +--+--+ + + + +--+ +--+--+ +--+--+ + + +--+ + +--+
| | | | | | | | | | | | | |
+ + +--+--+ + +--+ + +--+--+--+ +--+--+--+ + + +--+ +
| | | | | | | | | | | | |
+ + + +--+--+--+--+ +--+ + +--+--+--+--+ + +--+--+--+ +
| | | | | | | | | |
+ + +--+--+--+--+--+--+--+--+--+ +--+--+ +--+--+--+ +--+ +
| | | | | | | | | | | |
+--+--+--+ + + + + +--+ +--+ +--+ +--+ +--+--+ + +--+
| | | | | | | | | | | | | | |
+ +--+ +--+ + + + +--+--+--+--+--+ + +--+ + + +--+--+
| | | | | | | | | | |
+--+--+--+ +--+ + +--+--+--+--+--+ + + +--+--+--+--+--+ +
S | | | | +
+-----+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
"""
ASCII_MAZE2 = """
___________________________________
| _____ | | ___ | ___ ___ | | | |
| | | |_| |__ | |_| __|____ | | | |
| | | |_________|__ |______ |___|_| |
| |_| | _______ |______ | | ____|
| ___ | |____ | |______ | |_| |____ |
|___|_|____ | | ___ | |________ | E
| ________| | |__ | |______ | | | |
| | | ________| | __|____ | | | __| |
|_| |__ | | __|__ | ____| | |_| __|
| ____| | |____ | |__ | |__ |__ |
S |_______|_______|___|___|___|_____|
"""
ASCII_MAZE3 = """
"""
ASCII_MAZE4 = """
"""
ASCII_MAZE5 = """
"""
PATH, START, EXIT, VISITED, SOLUTION = " SE.#"
class Maze():
def __init__(self, ascii_maze):
self.maze = [list(row) for row in ascii_maze.splitlines()]
self.start_y = [row.count(START) for row in self.maze].index(1)
self.start_x = self.maze[self.start_y].index(START)
def __repr__(self):
return "\n".join("".join(row) for row in self.maze)
def solve(self, x = None, y = None):
if x == None:
x, y = self.start_x, self.start_y
if x>63 or x<0 or y<0 or y>31:
return False
#print(self.maze[y][x])
if self.maze[y][x] in (PATH, START):
self.maze[y][x] = VISITED
if( self.solve(x+1,y) or self.solve(x-1,y) or self.solve(x,y+1) or self.solve(x,y-1) ):
self.maze[y][x] = SOLUTION
return True
elif self.maze[y][x] == EXIT:
return True
return False
if __name__ == "__main__":
maze = Maze(ASCII_MAZE1)
if (maze.solve()):
print (maze)
else:
print (maze)
print("No Solution Found For Given Start and End of maze 1")