forked from candycane124/RobotBuddy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgridBot.py
More file actions
154 lines (138 loc) · 3.58 KB
/
gridBot.py
File metadata and controls
154 lines (138 loc) · 3.58 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
'''
GPS Buddy Path Finding
2023-11-30
'''
def get_options(graph, pos):
'''
Checks if adjacent blocks to pos are open (no obstacle).
Returns list in the order [up, right, down, left], with the [x,y] location of the
block in that direction, or -1 if there is an obstacle.
'''
results = []
n = len(graph)
#look around
options = [0,0,0,0]
if pos[0]-1 >= 0 and graph[pos[0]-1][pos[1]]: #up
options[0] = 1
if pos[0]+1 < n and graph[pos[0]+1][pos[1]]: #down
options[2] = 1
if pos[1]-1 >= 0 and graph[pos[0]][pos[1]-1]: #right
options[3] = 1
if pos[1]+1 < n and graph[pos[0]][pos[1]+1]: #left
options[1] = 1
#add options
if options[0]:
results.append([pos[0]-1,pos[1]])
else:
results.append(-1)
if options[1]:
results.append([pos[0],pos[1]+1])
else:
results.append(-1)
if options[2]:
results.append([pos[0]+1,pos[1]])
else:
results.append(-1)
if options[3]:
results.append([pos[0],pos[1]-1])
else:
results.append(-1)
return results
def bfs_robot(graph, start, end):
'''
Perform a breadth-first search on the given graph to find the shortest
path from the start position to the end position.
Returns a list representing the moves to reach the end position
(0 - up, 1 - right, 2 - down, 3 - left)
or [-1] if no path is found.
'''
if end == start:
return []
visited = []
queue = [(start, [])]
while queue:
current, path = queue.pop(0)
visited.append(current)
options = get_options(graph, current)
for next in options:
if next != -1:
if next == end:
return path + [options.index(next)]
if next not in visited:
queue.append((next, path + [options.index(next)]))
visited.append(next)
return [-1]
def compact(a):
compacted = []
step = [a[0],1]
for i in range(1,len(a)):
if a[i] == step[0]:
step[1] += 1
else:
compacted.append(step)
step = [a[i],1]
compacted.append(step)
return compacted
def forward(t):
pass
def turn(turnRight):
pass
def backward(t):
pass
grid = [
[1,1,1,0,1,1],
[1,1,0,1,1,1],
[1,1,1,1,0,1],
[0,1,0,1,0,1],
[0,1,1,1,1,1],
[1,1,1,0,1,1]
]
size = len(grid)
# starting position
try:
pos = list(map(int,input("Starting coordinates: ").split(",")))
if pos[0]<0 or pos[0]>=size or pos[1]<0 or pos[0]>=size:
print("Out of range, starting at (0,0)")
pos = [0,0]
except Exception:
print("Invalid input, starting at (0,0)")
pos = [0,0]
#main
tile = 1 #amt of time to run for one tile
running = True
while running:
try:
newPos = input("Destination: ")
if newPos.strip() == "exit":
running = False
else:
newPos = newPos.split(",")
newPos = [int(newPos[0]), int(newPos[1])]
if newPos[0]<0 or newPos[0]>=size or newPos[1]<0 or newPos[1]>=size:
print("Coordinate not in range.")
else:
print("calculating path...")
instructions = bfs_robot(grid, pos, newPos)
instructions = compact(instructions)
print(instructions)
for step in instructions:
if step[0] == 0:
forward(tile*step[1])
elif step[0] == 1:
turn(True)
forward(tile*step[1])
turn(False)
elif step[0] == 2:
backward(tile*step[1])
elif step[0] == 3:
turn(False)
forward(tile*step[1])
turn(True)
elif step[0] == -1:
print("Unable to reach destination.")
break
else:
pos = newPos
except ValueError:
print("Invalid input.")
print("Program ended.")