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

Implementing a MarsRover-class in order to give the Rover its functionality...

Implementing a MarsRover-class in order to give the Rover its functionality such as move_fw, move_bw, turn_r, turn_l and a drive method that takes string commands
parent f72bf22c
No related branches found
No related tags found
No related merge requests found
Pipeline #20427 passed
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
......@@ -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():
......
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