-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPathFinding.py
263 lines (222 loc) · 10.9 KB
/
PathFinding.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# WEC Competition - Rhys, Anh, Shawn
# Inputs
dimension = input()
botCoord = input()
userCoord = input()
obstacleArray = input()
itemCoord = input()
# set up variables
gotItem = False
usedArray = []
testBotCoords = []
numMoves = -1
botPath = ""
reachedObjective = False
# set up our variables into arrays
dimensionList = dimension.split(" ")
for i in range(0, len(dimensionList)):
dimensionList[i] = int(dimensionList[i])
botCoordList = botCoord.split(" ")
for i in range(0, len(botCoordList)):
botCoordList[i] = int(botCoordList[i])
userCoordList = userCoord.split(" ")
for i in range(0, len(userCoordList)):
userCoordList[i] = int(userCoordList[i])
obstacleArrayList = obstacleArray.split(" ")
if(obstacleArrayList[0] != ""):
for i in range(0, len(obstacleArrayList)):
obstacleArrayList[i] = int(obstacleArrayList[i])
itemCoordList = itemCoord.split(" ")
if(itemCoordList[0] != ""):
for i in range(0, len(itemCoordList)):
itemCoordList[i] = int(itemCoordList[i])
else:
gotItem = True
# function to output
def outputCoords(botPath, numMoves):
botPath = botPath[:-2]
print(botPath)
print(numMoves)
# function to check whether a move is legal
def checkMove(moveCoord, obstacleArray, gotItem):
for i in range(0, len(obstacleArray), 2):
# want to check if moveCoord is in obstacleArray
if(obstacleArray[0] == ""):
return True
if(moveCoord[0] == obstacleArray[i] and moveCoord[1] == obstacleArray[i+1]):
return False
if(gotItem == False):
if(moveCoord[0] == userCoordList[0] and moveCoord[1] == userCoordList[1]):
return False
if(moveCoord[0] == itemCoordList[0] and moveCoord[1] == itemCoordList[1]):
gotItem = True
return True
# these four functions describe how each move changes the coordinates. They also check to see if they are valid or not using the checkMove Function
def xMoveRight(testBotCoords, obstacleArrayList):
testBotCoords[0] += 1
if(checkMove(testBotCoords, obstacleArrayList, gotItem)):
obstacleArrayList += testBotCoords
return True
else:
testBotCoords[0] -= 1
return False
def xMoveLeft(testBotCoords, obstacleArrayList):
testBotCoords[0] -= 1
if(checkMove(testBotCoords, obstacleArrayList, gotItem)):
obstacleArrayList += testBotCoords
return True
else:
testBotCoords[0] += 1
return False
def yMoveDown(testBotCoords, obstacleArrayList):
testBotCoords[1] += 1
if(checkMove(testBotCoords, obstacleArrayList, gotItem)):
obstacleArrayList += testBotCoords
return True
else:
testBotCoords[1] -= 1
return False
def yMoveUp(testBotCoords, obstacleArrayList):
testBotCoords[1] -= 1
if(checkMove(testBotCoords, obstacleArrayList, gotItem)):
obstacleArrayList += testBotCoords
return True
else:
testBotCoords[1] += 1
return False
# main function, recursively calls itself
# First directs the bot to the item, then the user. Directly to the user if there is no item
def path_Finding(botPath, numMoves, botCoordList):
gotItem = False
testBotCoords = botCoordList
# updates the string and number of moves so the final output is correct
botPath += (str(botCoordList[0]) + " " + str(botCoordList[1]) + ", ")
numMoves += 1
# checks to see if gotItem is true
if(botCoordList[0] == itemCoordList[0] and botCoordList[1] == itemCoordList[1]):
gotItem = True
if(itemCoordList[0] == ""):
gotItem = True
# ells code which itme to go after based on if item is gotten
if(gotItem):
xDifference = int(botCoordList[0]) - int(userCoordList[0])
yDifference = int(botCoordList[1]) - int(userCoordList[1])
else:
xDifference = int(botCoordList[0]) - int(itemCoordList[0])
yDifference = int(botCoordList[1]) - int(itemCoordList[1])
# algorithm to see which way the bot should move, based on how far it is away from the item/person in the x and y direction
if(abs(xDifference) > abs(yDifference)):
if(xDifference > 0):
if(xMoveLeft(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yDifference > 0):
if(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveRight(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
else:
return False
else:
if(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveRight(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
if(xDifference < 0):
if(xMoveRight(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yDifference > 0):
if(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveLeft(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
else:
if(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveLeft(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(abs(yDifference) > abs(xDifference)):
if(yDifference > 0):
if(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xDifference > 0):
if(xMoveLeft(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveRight(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
else:
if(xMoveRight(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveLeft(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
if(yDifference < 0):
if(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xDifference > 0):
if(xMoveLeft(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveRight(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
else:
if(xMoveRight(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveLeft(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
else:
if(xDifference > 0):
if(xMoveLeft(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yDifference > 0):
if(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveRight(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
else:
return False
else:
if(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveRight(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
if(xDifference < 0):
if(xMoveRight(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yDifference > 0):
if(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveLeft(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
else:
if(yMoveDown(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(yMoveUp(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
elif(xMoveLeft(testBotCoords, obstacleArrayList) == True):
return(path_Finding(botPath, numMoves, botCoordList))
# adding to the final output
botPath += (str(botCoordList[0]) + " " + str(botCoordList[1]) + ", ")
print()
print(botPath[5:-7])
print(numMoves)
path_Finding(botPath, numMoves, botCoordList)