Skip to content
Snippets Groups Projects
Commit 627b102e authored by Daniel Rafeh's avatar Daniel Rafeh
Browse files

Implementation of new functions and new testcases - rover checks if an...

Implementation of new functions and new testcases - rover checks if an obstacle is in front of it to avoid colisions with them, create new testcases to check if it works properly
parent acaf9316
No related branches found
No related tags found
No related merge requests found
Pipeline #20390 passed
......@@ -33,6 +33,10 @@
# Wenn der Rover in Richtung Süden schaut und den Befehl "B" erhält obwohl ein Hindernis in dieser Richtung ist, bleibt der Rover stehen und die y-Koordinate ändert sich nicht.
# Wenn der Rover in Richtung Osten schaut und den Befehl "F" erhält obwohl ein Hindernis in dieser Richtung ist, bleibt der Rover stehen und die x-Koordinate ändert sich nicht.
# Wenn der Rover in Richtung Westen schaut und den Befehl "B" erhält obwohl ein Hindernis in dieser Richtung ist, bleibt der Rover stehen und die x-Koordinate ändert sich nicht.
# Wenn der Rover in Richtung Norden bei (3,3) schaut und den Befehl "FF" erhält obwohl ein Hindernis bei (3,5) ist, führt der Rover nur den ersten Befehl aus und bleibt bei (3,4) stehen.
# Wenn der Rover in Richtung Norden bei (3,4) schaut und den Befehl "FFRRFF" erhält obwohl ein Hindernis bei (3,5) ist, bleibt er bei (3,4) stehen.
# Wenn der Rover in Richtung Norden bei (3,4) schaut und den Befehl "RFF" erhält obwohl ein Hindernis bei (3,5) ist, bleibt er bei (3,4) stehen.
# Wenn der Rover in Richtung Norden bei (3,4) schaut und den Befehl "RRFLLFF" erhält obwohl ein Hindernis bei (3,5) ist, muss er bei (4,6) stehen bleiben.
# Plateau-Rand-Erkennung
# Wenn der Rover an der oberen Kante (y = 15) steht, Richtung Norden schaut und den Befehl "F" erhält, bleibt er stehen.
......@@ -73,55 +77,109 @@ class Plateau:
print(f"Rover moved to ({x}, {y})")
class Rover:
def __init__(self, x=0, y=0, direction='N'):
def __init__(self, x=0, y=0, direction='N', obstacles=None):
self.x = x
self.y = y
self.direction = direction
self.directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']
self.obstacles = obstacles if obstacles else []
def move_forward(self):
def check_for_obstacle(self):
next_stepx, next_stepy = self.x, self.y
if self.direction == 'N':
self.y += 1
next_stepy += 1
check_positions = [(next_stepx, next_stepy)]
elif self.direction == 'S':
self.y -= 1
next_stepy -= 1
check_positions = [(next_stepx, next_stepy)]
elif self.direction == 'E':
self.x += 1
next_stepx += 1
check_positions = [(next_stepx, next_stepy)]
elif self.direction == 'W':
self.x -= 1
next_stepx -= 1
check_positions = [(next_stepx, next_stepy)]
elif self.direction == 'NE':
self.x += 1
self.y += 1
next_stepx += 1
next_stepy += 1
check_positions = [(next_stepx, next_stepy),
(self.x + 1, self.y),
(self.x, self.y + 1)]
elif self.direction == 'NW':
self.x -= 1
self.y += 1
next_stepx -= 1
next_stepy += 1
check_positions = [(next_stepx, next_stepy),
(self.x + 1, self.y),
(self.x, self.y + 1)]
elif self.direction == 'SE':
self.x += 1
self.y -= 1
next_stepx += 1
next_stepy -= 1
check_positions = [(next_stepx, next_stepy),
(self.x + 1, self.y),
(self.x, self.y + 1)]
elif self.direction == 'SW':
self.x -= 1
self.y -= 1
next_stepx -= 1
next_stepy -= 1
check_positions = [(next_stepx, next_stepy),
(self.x + 1, self.y),
(self.x, self.y + 1)]
else:
check_positions = []
for pos in check_positions:
if pos in self.obstacles:
return True
return False
def move_forward(self):
if not self.check_for_obstacle():
if self.direction == 'N':
self.y += 1
elif self.direction == 'S':
self.y -= 1
elif self.direction == 'E':
self.x += 1
elif self.direction == 'W':
self.x -= 1
elif self.direction == 'NE':
self.x += 1
self.y += 1
elif self.direction == 'NW':
self.x -= 1
self.y += 1
elif self.direction == 'SE':
self.x += 1
self.y -= 1
elif self.direction == 'SW':
self.x -= 1
self.y -= 1
else:
print("Obstacle detected! Stopping movement.")
def move_backward(self):
if self.direction == 'N':
self.y -= 1
elif self.direction == 'S':
self.y += 1
elif self.direction == 'E':
self.x -= 1
elif self.direction == 'W':
self.x += 1
elif self.direction == 'NE':
self.x -= 1
self.y -= 1
elif self.direction == 'NW':
self.x += 1
self.y -= 1
elif self.direction == 'SE':
self.x -= 1
self.y += 1
elif self.direction == 'SW':
self.x += 1
self.y += 1
if not self.check_for_obstacle():
if self.direction == 'N':
self.y -= 1
elif self.direction == 'S':
self.y += 1
elif self.direction == 'E':
self.x -= 1
elif self.direction == 'W':
self.x += 1
elif self.direction == 'NE':
self.x -= 1
self.y -= 1
elif self.direction == 'NW':
self.x += 1
self.y -= 1
elif self.direction == 'SE':
self.x -= 1
self.y += 1
elif self.direction == 'SW':
self.x += 1
self.y += 1
else:
print("Obstacle detected! Stopping movement.")
def turn_left(self):
current_index = self.directions.index(self.direction)
......@@ -132,16 +190,40 @@ class Rover:
self.direction = self.directions[(current_index + 1) % len(self.directions)]
def drive(self, cmd: str) -> str:
for c in cmd:
i = 0
while i < len(cmd):
c = cmd[i]
if c == 'F':
if self.check_for_obstacle():
print("Obstacle detected! Stopping movement.")
break
self.move_forward()
elif c == 'B':
if self.check_for_obstacle():
print("Obstacle detected! Stopping movement.")
break
self.move_backward()
elif c == 'L':
self.turn_left()
elif c == 'R':
self.turn_right()
elif c == 'L' or c == 'R':
# Vorschau: nächste Bewegung nach Drehung
original_direction = self.direction
if c == 'L':
self.turn_left()
else:
self.turn_right()
# nur drehen, wenn danach kein Hindernis im Weg
if i + 1 < len(cmd) and cmd[i + 1] in ['F', 'B']:
if self.check_for_obstacle():
# Rückgängig machen!
self.direction = original_direction
print("Obstacle detected after turn preview! Cancelling turn and movement.")
break
# sonst normale Drehung ohne Folgebewegung
i += 1
class Mars:
def __init__(self, plateau:Plateau, rover:Rover):
......@@ -313,7 +395,62 @@ class TestRover(unittest.TestCase):
control.execute_command('BRRRFLF')
self.assertEqual((rover.x, rover.y), (3,5))
self.assertEqual(rover.direction, 'E')
# Test Obstacle Detection
def test_obstacle_detection_forward_north(self):
rover = Rover(3, 5, 'N')
control = MissionControl(rover)
rover.obstacles = [(3, 6)]
control.execute_command('F')
self.assertEqual((rover.x, rover.y), (3,5))
self.assertEqual(rover.direction, 'N')
def test_obstacle_detection_backward_south(self):
rover = Rover(3, 5, 'S')
control = MissionControl(rover)
rover.obstacles = [(3, 4)]
control.execute_command('B')
self.assertEqual((rover.x, rover.y), (3,5))
self.assertEqual(rover.direction, 'S')
def test_obstacle_detection_forward_east(self):
rover = Rover(3, 5, 'E')
control = MissionControl(rover)
rover.obstacles = [(4, 5)]
control.execute_command('F')
self.assertEqual((rover.x, rover.y), (3,5))
self.assertEqual(rover.direction, 'E')
def test_obstacle_detection_backward_west(self):
rover = Rover(3, 5, 'W')
control = MissionControl(rover)
rover.obstacles = [(2, 5)]
control.execute_command('B')
self.assertEqual((rover.x, rover.y), (3,5))
self.assertEqual(rover.direction, 'W')
# Test Obstacle Detection with Sequence
def test_obstacle_detection_sequence(self):
rover = Rover(3, 3, 'N')
control = MissionControl(rover)
rover.obstacles = [(3, 5)]
control.execute_command('FF')
self.assertEqual((rover.x, rover.y), (3,4))
self.assertEqual(rover.direction, 'N')
control.execute_command('FFRRFF')
self.assertEqual((rover.x, rover.y), (3,4))
self.assertEqual(rover.direction, 'N')
control.execute_command('RFF')
self.assertEqual((rover.x, rover.y), (3,4))
self.assertEqual(rover.direction, 'N')
control.execute_command('RRFLLFF')
self.assertEqual((rover.x, rover.y), (4,6))
self.assertEqual(rover.direction, 'N')
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment