-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathplanning.py
More file actions
159 lines (124 loc) · 4.89 KB
/
pathplanning.py
File metadata and controls
159 lines (124 loc) · 4.89 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
from pynput import keyboard
import random
import time
class Pathplanning:
def __init__(self):
self.width = 7
self.height = 7
self.robot_width = 0
self.robot_height = 0
self.regenerate = 0
self.robot_start = (self.robot_height, self.robot_width)
self.robot_end = (self.height - 1, self.width - 1)
self.listener = None
self.map = None
self.path = []
self.build_map()
def build_map(self):
self.map = []
for i in range(self.height):
inner_grid = []
for j in range(self.width):
r = random.random()
if r < 0.4:
inner_grid.append('#')
else:
inner_grid.append('.')
self.map.append(inner_grid)
# Start and Goal Positions
self.map[self.robot_height][self.robot_width] = 'S'
self.map[self.height - 1][self.width - 1] = 'G'
if not self.is_solvable(self.robot_start, self.robot_end):
self.build_map()
self.regenerate += 1
# For how many times the map needed to regenerate due to probability
# print(self.regenerate)
def is_solvable(self, start, end):
self.path = []
came_from = {}
visited = []
to_visit = [start]
while to_visit:
current = to_visit.pop(0)
visited.append(current)
if current == end:
# Path Finding
while current != start:
self.path.append(current)
current = came_from[current]
self.path.append(start)
self.path.reverse()
print(self.path)
return True
UP = (current[0] - 1, current[1])
RIGHT = (current[0], current[1] + 1)
DOWN = (current[0] + 1, current[1])
LEFT = (current[0], current[1] - 1)
# DIRECTIONS are in the order: UP, RIGHT, DOWN, LEFT
DIRECTIONS = [UP, RIGHT, DOWN, LEFT]
for direction in DIRECTIONS:
if 0 <= direction[0] <= self.height - 1 and 0 <= direction[1] <= self.width - 1 and self.map[direction[0]][direction[1]] != '#':
if direction not in visited:
to_visit.append(direction)
came_from[direction] = current
return False
def print_map(self):
print()
for index, row in enumerate(self.map):
display_row = list(row)
if index == self.robot_height:
display_row[self.robot_width] = 'R'
print(index, display_row)
def robot_traversal(self):
self.print_map() # Initial draw
for points in self.path:
self.robot_height = points[0]
self.robot_width = points[1]
self.print_map()
time.sleep(0.3)
print("Done!")
# For player traversal (for fun)
def run(self):
self.print_map() # Initial draw
# This creates the "Listener" loop for you
with keyboard.Listener(on_press=self.robot_movement) as self.listener:
self.listener.join()
def robot_movement(self, key):
# UP
if key == keyboard.Key.up and self.robot_height > 0 and self.map[self.robot_height - 1][self.robot_width] != '#':
self.robot_height -= 1
# DOWN
elif key == keyboard.Key.down and self.robot_height < self.height - 1 and self.map[self.robot_height + 1][self.robot_width] != '#':
self.robot_height += 1
# LEFT
elif key == keyboard.Key.left and self.robot_width > 0 and self.map[self.robot_height][self.robot_width - 1] != '#':
self.robot_width -= 1
# RIGHT
elif key == keyboard.Key.right and self.robot_width < self.width - 1 and self.map[self.robot_height][self.robot_width + 1] != '#':
self.robot_width += 1
# QUIT KEY
elif key == keyboard.Key.esc:
self.listener.stop()
return
self.print_map()
def menu(self):
print("Welcome to Pathplanning!")
print("1. Manual Mode")
print("2. Auto Solve")
print(f"Please Select One Option: ", end='')
choice = input()
while choice != "1" or choice != "2":
if choice == "1":
self.run()
elif choice == "2":
self.robot_traversal()
return
print()
print("Please select one:")
print("1. Manual Mode")
print("2. Auto Solve")
print(f"Please Select One Option: ", end='')
choice = int(input())
if __name__ == "__main__":
P = Pathplanning()
P.robot_traversal()