forked from spatil47/car
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
115 lines (76 loc) · 3.91 KB
/
ai.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
class queue(object):
def __init__(self, y, x):
self.nodes = [[[y,x],[]]]
def take(self):
return self.nodes.pop()
def put(self, node):
#very important to put the element at the beginning of the list here, not append
self.nodes.insert(0,node)
def length(self):
return len(self.nodes)
class PathCalc(object):
def __init__(self):
self.mapArr = []
#this takes the current known map and a goal location (tuple of x,y)
#the goal location may be overloaded in the future to allow a relative goal instead of an absolute one
#(for example, go 10 meters to the left of your current location)
def calculateBestPath(self, x, y, currentMap):
self.mapArr = currentMap
#make something which keeps track of the possible paths
q = queue(y,x)
#set yourself as a blocked spot to avoid backtracking
self.mapArr[y][x] = 1
while q.length() != 0:
#grab the next space to be tested
current = q.take()
cy = current[0][0]
cx = current[0][1]
path = current[1]
#check each position around the current position to see if it is viable
#up
if (cy > 0) and (self.mapArr[cy-1][cx] != 1):
#make a new possibility for each direction
possiblePath = list(path)
#you have arrived, and found the best path.
if self.mapArr[cy-1][cx] == 3:
#don't forget to append the last move before returning
possiblePath.append('up')
return possiblePath
else:
#set the open space to a 'blocked' status. Ensures that no looping happens
self.mapArr[cy-1][cx] = 1
#set the current space as a path possibility. it will be checked in the next loop
possiblePath.append('up')
q.put([[cy-1,cx],possiblePath])
#down
if (cy+1 < len(self.mapArr)) and (self.mapArr[cy+1][cx] != 1):
possiblePath = list(path)
if self.mapArr[cy+1][cx] == 3:
possiblePath.append('down')
return possiblePath
else:
self.mapArr[cy+1][cx] = 1
possiblePath.append('down')
q.put([[cy+1,cx],possiblePath])
#left
if (cx > 0) and (self.mapArr[cy][cx-1] != 1):
possiblePath = list(path)
if self.mapArr[cy][cx-1] == 3:
possiblePath.append('left')
return possiblePath
else:
self.mapArr[cy][cx-1] = 1
possiblePath.append('left')
q.put([[cy,cx-1],possiblePath])
#right
if (cx+1 < len(self.mapArr[0])) and (self.mapArr[cy][cx+1] != 1):
possiblePath = list(path)
if self.mapArr[cy][cx+1] == 3:
possiblePath.append('right')
return possiblePath
else:
self.mapArr[cy][cx+1] = 1
possiblePath.append('right')
q.put([[cy,cx+1],possiblePath])
#if the program gets to this point without returning, it means that no path to the goal has been found
return False