diff --git a/Mars.py b/Mars.py
new file mode 100644
index 0000000000000000000000000000000000000000..cc638aaeeb47122fe42da207bd516a6610376828
--- /dev/null
+++ b/Mars.py
@@ -0,0 +1,31 @@
+from MarsRover import Rover
+from Plateau import Plateau
+
+class Mars(Rover):
+ def __init__(self, x, y, direction, plateau):
+ super().__init__(x, y, direction)
+ self.plateau = plateau
+ self.plateau.move(x, y)
+
+ def drive(self, cmd) -> str:
+ for command in cmd:
+ if command == 'F':
+ if self.plateau.obstacle_detection_system(self.x, self.y, self.direction):
+ self.move_forward()
+ else:
+ return f"Obstacle detected at {self.x}, {self.y}. Stopping movement."
+ elif command == 'B':
+ if self.plateau.obstacle_detection_system(self.x, self.y, self.direction):
+ self.move_backward()
+ else:
+ return f"Obstacle detected at {self.x}, {self.y}. Stopping movement."
+ elif command == 'L':
+ self.turn_left()
+ elif command == 'R':
+ self.turn_right()
+ else:
+ raise ValueError(f"Unknown command: {command}")
+ return f"Final position: {self.x}, {self.y}, facing {self.direction}."
+
+
+
diff --git a/MarsRover.py b/MarsRover.py
index d903d122e949e276556b59d2b4e4c70a4b67e164..8677861ef56724a07320cb15d76482b40e702fb4 100644
--- a/MarsRover.py
+++ b/MarsRover.py
@@ -76,4 +76,5 @@ class Rover:
else:
raise ValueError(f"Unknown command: {command}")
- return f"Current position: ({self.x}, {self.y}), facing {self.direction}"
\ No newline at end of file
+ return f"Current position: ({self.x}, {self.y}), facing {self.direction}"
+
\ No newline at end of file
diff --git a/Plateau.py b/Plateau.py
index 678a09952ff96eccf0ddbc91179afc75f4c8db9b..e9c75d6ff3e6762529444bd126baccec663bc2bd 100644
--- a/Plateau.py
+++ b/Plateau.py
@@ -1,26 +1,48 @@
class Plateau:
- def __init__(self, size_x:int, size_y:int):
+ def __init__(self, size_x:int, size_y:int, obstacles=None) -> None:
# Gesamtgröße des Plateaus
self.size_x = size_x
self.size_y = size_y
+ self.obstacles = obstacles if obstacles is not None else []
# Initialisiere das Plateau mit der angegebenen Größe
self.grid = [[None for _ in range(size_x)] for _ in range(size_y)]
-
- # Einfügen von Hindernissen
- self.obstacles = []
# Initialisierung der Rover-Position
self.rover_position = None
# Funktion zur Erkennung wo sich der Rover befindet
def move(self, x: int, y: int) -> None:
- self.rover_position = (x, y)
+ self.rover_position = (x, y)
+ print(f"Rover moved to position: {self.rover_position}")
# Funktion zur Erkennung von Hindernissen und Kanten des Plateaus
- def obstacle_detection_system(self, x: int, y: int) -> bool:
+ def obstacle_detection_system(self, x: int, y: int, direction: str) -> bool:
+ if direction == 'N':
+ y += 1
+ elif direction == 'S':
+ y -= 1
+ elif direction == 'E':
+ x += 1
+ elif direction == 'W':
+ x -= 1
+ elif direction == 'NE':
+ x += 1
+ y += 1
+ elif direction == 'NW':
+ x -= 1
+ y += 1
+ elif direction == 'SE':
+ x += 1
+ y -= 1
+ elif direction == 'SW':
+ x -= 1
+ y -= 1
+
if x < 0 or x >= self.size_x or y < 0 or y >= self.size_y:
return False
+
if (x, y) in self.obstacles:
return False
return True
+
diff --git a/__pycache__/MarsRover.cpython-312.pyc b/__pycache__/MarsRover.cpython-312.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..3ce2f34bb72ed1779ef6426739768f08cef9b847
Binary files /dev/null and b/__pycache__/MarsRover.cpython-312.pyc differ
diff --git a/__pycache__/Plateau.cpython-312.pyc b/__pycache__/Plateau.cpython-312.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..986dfdb773b6ec17b358aa2cbd922577b288cfd3
Binary files /dev/null and b/__pycache__/Plateau.cpython-312.pyc differ
diff --git a/rover.py b/rover.py
index 377b54105419ee2ebf77be91944ca4698147fbaf..75d1331b9664015bd2e06d1c8e7720a256f5b91d 100644
--- a/rover.py
+++ b/rover.py
@@ -78,12 +78,60 @@ 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 check_for_obstacle(self):
+ next_stepx, next_stepy = self.x, self.y
+ if self.direction == 'N':
+ next_stepy += 1
+ check_positions = [(next_stepx, next_stepy)]
+ elif self.direction == 'S':
+ next_stepy -= 1
+ check_positions = [(next_stepx, next_stepy)]
+ elif self.direction == 'E':
+ next_stepx += 1
+ check_positions = [(next_stepx, next_stepy)]
+ elif self.direction == 'W':
+ next_stepx -= 1
+ check_positions = [(next_stepx, next_stepy)]
+ elif self.direction == 'NE':
+ 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':
+ 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':
+ 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':
+ 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() and not self.check_plateau_bounds():