-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstateSearchProgram.py
More file actions
188 lines (143 loc) · 5.97 KB
/
stateSearchProgram.py
File metadata and controls
188 lines (143 loc) · 5.97 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import time
def move_right(current_coodinate):
temp_state = { "x":current_coodinate['x'],"y": current_coodinate['y']}
y_position = temp_state['y'] + 1
temp_state['y'] = y_position
return temp_state
def move_left(current_coodinate):
temp_state = { "x":current_coodinate['x'],"y": current_coodinate['y']}
y_position = temp_state['y'] - 1
temp_state['y'] = y_position
return temp_state
def move_up(current_coodinate):
temp_state = { "x":current_coodinate['x'],"y": current_coodinate['y']}
x_position = temp_state['x'] - 1
temp_state['x'] = x_position
return temp_state
def move_down(current_coodinate):
temp_state = { "x":current_coodinate['x'],"y": current_coodinate['y']}
x_position = temp_state['x'] + 1
temp_state['x'] = x_position
return temp_state
#for now hard code bonds since its a 4x4 matrix maze checks if the state is out of bound or not
def checkBonds(current_coordinates):
if(0 <= current_coordinates['x'] <= 9) and (0 <= current_coordinates['y'] <= 9):
return True
return False
#checks if the state is a wall or not
def checkWallState(current_coordinates, maze):
x = current_coordinates['x']
y = current_coordinates['y']
if(maze[x][y] == '#'):
return True
return False
def checkVistedState(stack_path, current_coordinates):
if current_coordinates in stack_path:
return True
return False
def checkExistState(current_coordinates, maze):
x = current_coordinates['x']
y = current_coordinates['y']
if(maze[x][y] == 'E'):
return True
return False
def peek(stack):
return stack[-1]
def runMaze(maze,stack_path):
for position in stack_path:
current_position = position
for row in range(10):
for col in range(10):
if row == current_position['x'] and col == current_position['y']:
print("'S' ",end="")
else:
print(f"'{maze[row][col]}' ",end="")
print()
time.sleep(0.1)
print()
print()
maze = [
['#', '#', '#', '#', '#', '#', '#', '#', '#', '#'],
['S', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', '#'],
['#', ' ', '#', '#', ' ', '#', '#', ' ', '#', '#'],
['#', ' ', ' ', '#', ' ', ' ', '#', ' ', ' ', '#'],
['#', '#', ' ', '#', ' ', '#', '#', '#', ' ', '#'],
['#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', '#'],
['#', ' ', '#', '#', ' ', '#', ' ', '#', ' ', '#'],
['#', '#', '#', '#', '#', '#', 'E', '#', '#', '#']
]
#start co ordinates
currentCo_ordinates = {"x":1, "y":0}
#this is to store final path from start to exit
stack_path = []
#add current postion
stack_path.append(currentCo_ordinates)
#this is to store current state posible paths
stack_possible_transactions = []
#explore direct stack
explore_state = []
#mutliple route
multi_route = False
#for keeping how deep you went
root_count = []
#counting current branch
count_path = 0
#for now im working with a fixed maze as practice to create a more generic approach to finding a way out of
#any maze using state search technique
while True:
#check if we are in exist state
if(checkExistState(currentCo_ordinates,maze)):
if currentCo_ordinates not in stack_path:
stack_path.append(currentCo_ordinates)
break
#check which direction can you move
# bound are x should be 0 < x < 10 and 0 < y < 10 (bounds are hard coded for now)
#check the move if its valid and add it to possible_path stack
#its should be within maze bound and should not be a wall and it should not be a state thats been visited
#checking up direction
moveUpCheck = move_up(currentCo_ordinates)
if checkBonds(moveUpCheck) and not checkWallState(moveUpCheck,maze) and not checkVistedState(stack_path,moveUpCheck):
stack_possible_transactions.append(moveUpCheck)
#checking down direction
moveDownCheck = move_down(currentCo_ordinates)
if checkBonds(moveDownCheck) and not checkWallState(moveDownCheck,maze) and not checkVistedState(stack_path,moveDownCheck):
stack_possible_transactions.append(moveDownCheck)
#checking left direction
moveLeftCheck = move_left(currentCo_ordinates)
if checkBonds(moveLeftCheck) and not checkWallState(moveLeftCheck,maze) and not checkVistedState(stack_path,moveLeftCheck):
stack_possible_transactions.append(moveLeftCheck)
#checking right direction
moveRightCheck = move_right(currentCo_ordinates)
if checkBonds(moveRightCheck) and not checkWallState(moveRightCheck,maze) and not checkVistedState(stack_path,moveRightCheck):
stack_possible_transactions.append(moveRightCheck)
if(len(stack_possible_transactions) == 0):
for i in range(count_path):
if(len(stack_path) !=0):
stack_path.pop()
#set counth path to prevouse count
if len(root_count) != 0:
count_path += root_count.pop()-1
currentCo_ordinates = explore_state.pop()
stack_path.append(currentCo_ordinates)
continue
if(len(stack_possible_transactions) == 1):
if(multi_route):
count_path += 1
currentCo_ordinates = stack_possible_transactions.pop()
stack_path.append(currentCo_ordinates)
continue
if(len(stack_possible_transactions) >= 2):
multi_route = True
#add to root count and reset the count to 0 for the new branch
if count_path != 0:
root_count.append(count_path)
count_path = 1
currentCo_ordinates = stack_possible_transactions.pop()
stack_path.append(currentCo_ordinates)
explore_state.append(stack_possible_transactions.pop())
continue
print("exist path: ",stack_path)
#run maze
runMaze(maze, stack_path)