rover: add backward method
This commit is contained in:
parent
c96f44332c
commit
af803b642a
|
@ -20,15 +20,15 @@ class Rover(BaseModel):
|
|||
planet_size: Vector = Vector(x=100, y=100)
|
||||
dir: Direction = Direction.NORTH
|
||||
|
||||
def forward(self):
|
||||
def _translate(self, value):
|
||||
if self.dir == Direction.NORTH:
|
||||
self.pos.y += 1
|
||||
self.pos.y += value
|
||||
elif self.dir == Direction.SOUTH:
|
||||
self.pos.y -= 1
|
||||
self.pos.y -= value
|
||||
elif self.dir == Direction.EAST:
|
||||
self.pos.x += 1
|
||||
self.pos.x += value
|
||||
elif self.dir == Direction.WEST:
|
||||
self.pos.x -= 1
|
||||
self.pos.x -= value
|
||||
|
||||
if self.pos.x < 0:
|
||||
self.pos.x += self.planet_size.x
|
||||
|
@ -37,3 +37,9 @@ class Rover(BaseModel):
|
|||
|
||||
self.pos.x %= self.planet_size.x
|
||||
self.pos.y %= self.planet_size.y
|
||||
|
||||
def forward(self):
|
||||
self._translate(1)
|
||||
|
||||
def backward(self):
|
||||
self._translate(-1)
|
||||
|
|
|
@ -92,3 +92,67 @@ def test_rover_can_wrap_over_east_west():
|
|||
)
|
||||
rov.forward()
|
||||
assert rov.pos == Vector(x=0, y=0)
|
||||
|
||||
|
||||
def test_rover_can_go_backward_north():
|
||||
rov = Rover(
|
||||
pos=Vector(x=0, y=1), planet_size=Vector(x=10, y=10), dir=Direction.NORTH
|
||||
)
|
||||
rov.backward()
|
||||
assert rov.pos == Vector(x=0, y=0)
|
||||
|
||||
|
||||
def test_rover_can_go_backward_south():
|
||||
rov = Rover(
|
||||
pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.SOUTH
|
||||
)
|
||||
rov.backward()
|
||||
assert rov.pos == Vector(x=0, y=1)
|
||||
|
||||
|
||||
def test_rover_can_go_backward_west():
|
||||
rov = Rover(
|
||||
pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.WEST
|
||||
)
|
||||
rov.backward()
|
||||
assert rov.pos == Vector(x=1, y=0)
|
||||
|
||||
|
||||
def test_rover_can_go_backward_east():
|
||||
rov = Rover(
|
||||
pos=Vector(x=1, y=0), planet_size=Vector(x=10, y=10), dir=Direction.EAST
|
||||
)
|
||||
rov.backward()
|
||||
assert rov.pos == Vector(x=0, y=0)
|
||||
|
||||
|
||||
def test_rover_can_go_backward_wrapping_under_north_south():
|
||||
rov = Rover(
|
||||
pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.NORTH
|
||||
)
|
||||
rov.backward()
|
||||
assert rov.pos == Vector(x=0, y=9)
|
||||
|
||||
|
||||
def test_rover_can_go_backward_wrapping_over_north_south():
|
||||
rov = Rover(
|
||||
pos=Vector(x=0, y=9), planet_size=Vector(x=10, y=10), dir=Direction.SOUTH
|
||||
)
|
||||
rov.backward()
|
||||
assert rov.pos == Vector(x=0, y=0)
|
||||
|
||||
|
||||
def test_rover_can_go_backward_wrapping_under_east_west():
|
||||
rov = Rover(
|
||||
pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.EAST
|
||||
)
|
||||
rov.backward()
|
||||
assert rov.pos == Vector(x=9, y=0)
|
||||
|
||||
|
||||
def test_rover_can_go_backward_wrapping_over_east_west():
|
||||
rov = Rover(
|
||||
pos=Vector(x=9, y=0), planet_size=Vector(x=10, y=10), dir=Direction.WEST
|
||||
)
|
||||
rov.backward()
|
||||
assert rov.pos == Vector(x=0, y=0)
|
||||
|
|
Loading…
Reference in a new issue