diff --git a/Mars.py b/Mars.py deleted file mode 100644 index cc638aaeeb47122fe42da207bd516a6610376828..0000000000000000000000000000000000000000 --- a/Mars.py +++ /dev/null @@ -1,31 +0,0 @@ -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/__pycache__/Map.cpython-312.pyc b/__pycache__/Map.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..634325a8dc8d09fb955f4918fbb0338866a69141 Binary files /dev/null and b/__pycache__/Map.cpython-312.pyc differ diff --git a/__pycache__/Mars.cpython-312.pyc b/__pycache__/Mars.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3cddf50cab0e19dca2f7a1fcc0314481c6203e20 Binary files /dev/null and b/__pycache__/Mars.cpython-312.pyc differ diff --git a/__pycache__/MarsRover.cpython-312.pyc b/__pycache__/MarsRover.cpython-312.pyc index 3ce2f34bb72ed1779ef6426739768f08cef9b847..0b5bc9f53b6c7fd55a2933085b4890c5790eab68 100644 Binary files a/__pycache__/MarsRover.cpython-312.pyc and b/__pycache__/MarsRover.cpython-312.pyc differ diff --git a/__pycache__/Plateau.cpython-312.pyc b/__pycache__/Plateau.cpython-312.pyc index 986dfdb773b6ec17b358aa2cbd922577b288cfd3..c90c8b8878f00b6654ad2820b65d992365f6dee4 100644 Binary files a/__pycache__/Plateau.cpython-312.pyc and b/__pycache__/Plateau.cpython-312.pyc differ diff --git a/rover.py b/rover.py index 75d1331b9664015bd2e06d1c8e7720a256f5b91d..34c4d81bfe1d3ddd0f9189daf523b6d12418a4df 100644 --- a/rover.py +++ b/rover.py @@ -481,4 +481,5 @@ class TestRover(unittest.TestCase): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() + diff --git a/src/Mars.py b/src/Mars.py new file mode 100644 index 0000000000000000000000000000000000000000..b97387ee6159e452986632000a1bbd60d5727017 --- /dev/null +++ b/src/Mars.py @@ -0,0 +1,86 @@ +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: + executed_commands = [] + for command in cmd: + if command == 'F': + next_x, next_y = self.calculate_next_positions(self.x, self.y, self.direction) + if self.plateau.obstacle_detection_system(self.x, self.y, self.direction): + self.move_forward() + executed_commands += command + else: + return f"Obstacle detected at {next_x}, {next_y}. Stopping movement." + elif command == 'B': + prev_x, prev_y = self.calculate_next_positions(self.x, self.y, self.direction) + if self.plateau.obstacle_detection_system(self.x, self.y, self.direction): + self.move_backward() + executed_commands += command + else: + return f"Obstacle detected at {prev_x}, {prev_y}. Stopping movement." + elif command == 'L': + self.turn_left() + executed_commands += command + elif command == 'R': + self.turn_right() + executed_commands += command + else: + raise ValueError(f"Unknown command: {command}") + return f"Final position: {self.x}, {self.y}, facing {self.direction}." + + + def calculate_next_positions(self, x, y, direction): + 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 + return x, y + + def _calculate_prev_positions(self, x, y, direction): + 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 + return x, y + +plateau = Plateau(16, 16, [(3, 5), (4, 6)]) +mars = Mars(3, 6, 'N', plateau) +mars.drive("FFRF") \ No newline at end of file diff --git a/src/MarsMap.py b/src/MarsMap.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/MarsRover.py b/src/MarsRover.py similarity index 95% rename from MarsRover.py rename to src/MarsRover.py index 8677861ef56724a07320cb15d76482b40e702fb4..badc2f2946e3a324e0129c2b4ac374b36d0cb255 100644 --- a/MarsRover.py +++ b/src/MarsRover.py @@ -5,6 +5,7 @@ class Rover: self.y = y self.direction = direction self.directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'] + self.history = [] def turn_left(self): current_index = self.directions.index(self.direction) @@ -76,5 +77,5 @@ class Rover: else: raise ValueError(f"Unknown command: {command}") - return f"Current position: ({self.x}, {self.y}), facing {self.direction}" + return f"Rover position after movement: ({self.x}, {self.y}), facing {self.direction}" \ No newline at end of file diff --git a/src/MissionControl.py b/src/MissionControl.py new file mode 100644 index 0000000000000000000000000000000000000000..8cb2c66f2da29c1ed24dccf17f09a243cbfd7ef5 --- /dev/null +++ b/src/MissionControl.py @@ -0,0 +1,65 @@ +from MarsRover import Rover +from Mars import Mars +from Plateau import Plateau + +import re + +class Map: + def __init__(self): + self.obstacles = set() + self.rover_position = None + self.size_x = None + self.size_y = None + + def update_rover_position(self, x, y): + self.rover_position = (x, y) + + def add_obstacle(self, x, y): + self.obstacles.add((x, y)) + + def update_boundaries(self, x, y): + if self.size_x is None or x >= self.size_x: + self.size_x = x + 1 + if self.size_y is None or y >= self.size_y: + self.size_y = y + 1 + + if x < 0: + pass + if y < 0: + pass + + +class MissionControl: + def __init__(self, rover): + self.rover = rover + self.map = Map() + self.rover_position = (rover.x, rover.y) + self.rover_direction = rover.direction + self.map.update_rover_position(rover.x, rover.y) + + def send_command(self, command:str): + result = self.rover.drive(command) + print("Rove response:" + result) + + if result.startswith("Final position:"): + match = re.search(r"Final position: (\d+), (\d+), facing (\w+)", result) + if match: + x, y, direction = map(str.strip, match.groups()) + self.rover_position = (int(x), int(y)) + self.rover_direction = direction + self.map.update_rover_position(int(x), int(y)) + self.map.update_boundaries(int(x), int(y)) + + elif "Obstacle detected at" in result: + match = re.search(r"Obstacle detected at (\d+), (\d+)", result) + if match: + obstacle_x, obstacle_y = map(int, match.groups()) + self.map.add_obstacle(obstacle_x, obstacle_y) + + print(f"Mission Control: Rover position: {self.rover_position}, Obstacles: {self.map.obstacles}, Direction: {self.rover_direction}") + +plateau= Plateau(16, 16, [(3, 5), (4, 6)]) +rover = Mars(3, 6, 'N', plateau) +mission_control = MissionControl(rover) +mission_control.send_command("B") + diff --git a/Plateau.py b/src/Plateau.py similarity index 100% rename from Plateau.py rename to src/Plateau.py diff --git a/src/__pycache__/Map.cpython-312.pyc b/src/__pycache__/Map.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..74d6c4e3990a24af8545f47c09025b99acbc307f Binary files /dev/null and b/src/__pycache__/Map.cpython-312.pyc differ diff --git a/src/__pycache__/Mars.cpython-312.pyc b/src/__pycache__/Mars.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..edc9b489eb8f9280cecc83f6509e324bf2521582 Binary files /dev/null and b/src/__pycache__/Mars.cpython-312.pyc differ diff --git a/src/__pycache__/MarsMap.cpython-312.pyc b/src/__pycache__/MarsMap.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6ead88b8634e2fe2d147aeffe872c33ef9090d57 Binary files /dev/null and b/src/__pycache__/MarsMap.cpython-312.pyc differ diff --git a/src/__pycache__/MarsRover.cpython-312.pyc b/src/__pycache__/MarsRover.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a6b7242259072a3155611f429bdbc8f52965aca8 Binary files /dev/null and b/src/__pycache__/MarsRover.cpython-312.pyc differ diff --git a/src/__pycache__/Plateau.cpython-312.pyc b/src/__pycache__/Plateau.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1ef4b349085591b9c1d9f4698e71587994190de4 Binary files /dev/null and b/src/__pycache__/Plateau.cpython-312.pyc differ