diff --git a/MarsRover.py b/MarsRover.py new file mode 100644 index 0000000000000000000000000000000000000000..d903d122e949e276556b59d2b4e4c70a4b67e164 --- /dev/null +++ b/MarsRover.py @@ -0,0 +1,79 @@ +class Rover: + + def __init__(self, x, y, direction): + self.x = x + self.y = y + self.direction = direction + self.directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'] + + def turn_left(self): + current_index = self.directions.index(self.direction) + self.direction = self.directions[(current_index - 1) % len(self.directions)] + return 'This is the new direction: ' + self.direction + + def turn_right(self): + current_index = self.directions.index(self.direction) + self.direction = self.directions[(current_index + 1) % len(self.directions)] + return 'This is the new direction: ' + self.direction + + def move_forward(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 + + return 'This is the new position: ' + str(self.x) + ',' + str(self.y) + + 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 + return 'This is the new position: ' + str(self.x) + ',' + str(self.y) + + def drive(self, cmd:str) ->str: + for command in cmd: + if command == 'F': + self.move_forward() + elif command == 'B': + self.move_backward() + elif command == 'L': + self.turn_left() + elif command == 'R': + self.turn_right() + else: + raise ValueError(f"Unknown command: {command}") + + return f"Current position: ({self.x}, {self.y}), facing {self.direction}" \ No newline at end of file diff --git a/rover.py b/rover.py index 75d1331b9664015bd2e06d1c8e7720a256f5b91d..377b54105419ee2ebf77be91944ca4698147fbaf 100644 --- a/rover.py +++ b/rover.py @@ -78,60 +78,12 @@ class Plateau: print(f"Rover moved to ({x}, {y})") class Rover: - def __init__(self, x=0, y=0, direction='N', obstacles=None): + def __init__(self, x=0, y=0, direction='N'): 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():