Skip to content

Commit

Permalink
Elaborate: add position wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
fr2d9y2 authored and fr2d9y2 committed Apr 7, 2017
1 parent 94041c1 commit c3936b3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
8 changes: 4 additions & 4 deletions mars-rover/src/rover.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def moveBy(self, commandKey):
command(self, delta)

def move_forward(self, delta):
self.x += delta[0]
self.y += delta[1]
self.x = self.planet.wrap_x(self.x + delta[0])
self.y = self.planet.wrap_y(self.y + delta[1])

def move_backward(self, delta):
self.x -= delta[0]
self.y -= delta[1]
self.x = self.planet.wrap_x(self.x - delta[0])
self.y = self.planet.wrap_y(self.y - delta[1])

def move_right(self, delta):
self.orientation = delta[3]
Expand Down
46 changes: 46 additions & 0 deletions mars-rover/test/test_rover.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,49 @@ def test_simple_path(self):
assert_equal('E', r.orientation)
assert_equal(12, r.x)
assert_equal(23, r.y)

class TestWrapping:

def setup(self):
global wp
wp = planet.Planet(3, 3)

def test_wrap_east_forward(self):
r = wp.createRover(2, 0, 'E')
r.move(list("f"))
assert_equal(0, r.x)

def test_wrap_west_forward(self):
r = wp.createRover(0, 0, 'W')
r.move(list("f"))
assert_equal(2, r.x)

def test_wrap_north_forward(self):
r = wp.createRover(0, 2, 'N')
r.move(list("f"))
assert_equal(0, r.y)

def test_wrap_south_forward(self):
r = wp.createRover(0, 0, 'S')
r.move(list("f"))
assert_equal(2, r.y)

def test_wrap_east_backward(self):
r = wp.createRover(0, 0, 'E')
r.move(list('b'))
assert_equal(2, r.x)

def test_wrap_west_backward(self):
r = wp.createRover(2, 0, 'W')
r.move(list('b'))
assert_equal(0, r.x)

def test_wrap_north_backward(self):
r = wp.createRover(0, 0, 'N')
r.move(list('b'))
assert_equal(2, r.y)

def test_wrap_south_backward(self):
r = wp.createRover(0, 2, 'S')
r.move(list('b'))
assert_equal(0, r.y)

0 comments on commit c3936b3

Please sign in to comment.