-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
80 lines (63 loc) · 2.56 KB
/
driver.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
from rover import Rover
from plateau import Plateau
import re
#Main menu
#Each infinite loop inside of it is a input checker
def menu() -> None:
inputError = "Wrong input. Please, try again.\n\n"
while True:
ans = input("Insert the limits of the grid:\n")
#checks the "digit space digit" format in user input
if re.match(r"\d+\s\d+$", ans):
limits = ans.split(" ", 1)
limitX, limitY = [int(x) for x in limits]
plat = Plateau(limitX, limitY)
break
else:
print(inputError)
while True:
printStr = "Insert respectely the x, y and orientation values of the rover:\n"
printStr += "If you want to stop adding rovers, just leave it in blank\n"
ans = input(printStr)
if ans == "":
break
#Checks the "Digit space digit space string" format in user input
if re.match(r"\d+\s\d+\s[a-zA-Z]$", ans):
roverData = ans.split(" ", 2)
x, y = [int(x) for x in [roverData[0], roverData[1]]]
o = roverData[2]
try:
plat.checkPt(x, y)
except Plateau.OutOfLimitsError:
print("\nERROR: The selected position is off limits\n")
continue
except Plateau.CollisionDetectedError:
print("\nERROR: The selected position is already occupied\n")
continue
if o not in Rover.Orientation._head:
print(inputError)
continue
while True:
ans = input("Insert the instruction string of the Rover:\n")
#Checks the sequence of instructions format
if re.match(r"[mrlMRL]+$", ans):
instructions = ans
Rover(plat, x, y, o, instructions)
break
else:
print(inputError)
else:
print(inputError)
for rover in Rover.lstObj:
try:
rover.execInst(plat)
print(f"{rover.x} {rover.y} {rover.Orientation.current}")
except Plateau.CollisionDetectedError:
print(f"ERROR: Colision detected in [{rover.x}, {rover.y}].")
print(f"Canceling movement of the{rover.name} {rover.id}.")
rover.cancelMov()
except Plateau.OutOfLimitsError:
print(f"ERROR: Movement off limits detected in [{rover.x}, {rover.y}].")
print(f"Canceling movement of the {rover.name} {rover.id}")
rover.cancelMov()
menu()