forked from amupxm/pythonMazeSolver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.py
More file actions
99 lines (87 loc) · 3.04 KB
/
Copy pathmaze.py
File metadata and controls
99 lines (87 loc) · 3.04 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
95
96
97
98
99
grid = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 , 1],
[1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1 , 1],
[1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0 , 1],
[1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0 , 1],
[1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0 , 1],
[1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1 , 1],
[1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 , 1],
[1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 , 1],
[1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0 , 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 3 , 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1]]
moves=['0']
stack=[]
rightWay=[]
position=['','']
def draw():
j=0
for i in range(0, 12, 1):
print(str(grid[i][j])+'|'+str(grid[i][j+1])+'|'+str(grid[i][j+2])+'|'+str(grid[i][j+3])+'|'+str(grid[i][j+4])+'|'+str(grid[i][j+5])+'|'+str(grid[i][j+6])+'|'+str(grid[i][j+7])+'|'+str(grid[i][j+8])+'|'+str(grid[i][j+9])+'|'+str(grid[i][j+10])+'|'+str(grid[i][j+11])+'|')
def isWin(x,y):
if grid[x][y]==3 :
return True
else:
return False
def isRightEmpty(x,y):
if grid[x][y+1]==0 or grid[x][y+1]==3:
return True
else:
return False
def isLeftEmpty(x,y):
if grid[x][y-1]==0 or grid[x][y-1]==3:
return True
else:
return False
def isTopEmpty(x,y):
if grid[x-1][y]==0 or grid[x-1][y]==3:
return True
else:
return False
def isDownEmpty(x,y):
if grid[x+1][y]==0 or grid[x+1][y]==3:
return True
else:
return False
def operation (x,y):
# رسم جدول
draw()
# اضافه کردن مختصات کنونی به انتهای استک
stack.append([x , y])
print('moved to ['+str(x)+']['+str(y)+']')
#برسی بردن
if isWin(x,y):
print (' you win the game with :'+moves[0]+'moves')
print ('the lenghs of right way :'+str(len(rightWay)))
print ('the right Way is :')
print (rightWay)
else:
moves[0] =str(int( moves[0])+1)
grid[x][y]= 'x'
if isRightEmpty(x,y):
rightWay.append('Right')
print ('right is empty')
operation(x,y+1)
elif isDownEmpty(x,y):
rightWay.append('Down')
print('Down is empty')
operation(x+1,y)
elif isLeftEmpty(x,y):
rightWay.append('Left')
print('Left is empty')
operation(x,y-1)
elif isTopEmpty(x,y):
rightWay.append('Top')
print('Top is empty')
operation(x-1,y)
else:
print('Way Is Wrong')
stack.pop()
rightWay.pop()
position=stack.pop()
print(position)
operation(position[0],position[1])
def start(x,y):
operation(x,y)
var1, var2 = [int(x) for x in input("Enter begennig place ").split()]
start(var1,var2)