-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathmaze.py
56 lines (53 loc) · 1.48 KB
/
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
class Stack():
""" This class purpose is to append data to self.elems """
def __init__(self):
self.elems = [ ]
def is_empty(self):
return self.elems == [ ]
def top(self):
if self.elems == [ ]:
raise StackUnderflow
return self.elems[len(self.elems)-1]
def push(self, elem):
self.elems.append(elem)
def pop(self):
if self.elems == [ ]:
raise StackUnderflow
return self.elems.pop()
class element():
""" This class purpose is to initialize data and assigning them """
def __init__(self,x,y,status):
self.x = x
self.y = y
self.s = status
directions = ((0,1),(1,0),(0,-1),(-1,0))
maze = [[1,1,1,1,1,1],[1,0,1,0,1,1],[1,0,0,0,0,1],[1,0,1,0,1,1],[1,1,1,1,1,1]]
def mazePath (maze,x,y,end_x,end_y):
""" This function runs the algorithm of the maze.py """
st = Stack()
maze[x][y] = 2
ele = element(x,y,-1)
st.push(ele)
while not st.is_empty():
top = st.top()
#print ("(%d,%d)"%(top.x,top.y))
while top.s < 3:
top = st.top()
top.s += 1
new_x, new_y = top.x + directions[top.s][0], top.y + directions[top.s][1]
if (new_x, new_y) == (end_x, end_y):
print (new_x," ",new_y)
while not st.is_empty() :
ele = st.pop()
print (ele.x," ",ele.y)
return True
if maze[new_x][new_y] == 0 :
newele = element(new_x,new_y,-1)
maze[new_x][new_y] = 2
st.push(newele)
continue
st.pop()
print ("No way")
return False
# This will run the whole script
mazePath(maze,1,1,3,3)