Skip to content

Commit

Permalink
Elaborate: add left/right movement
Browse files Browse the repository at this point in the history
  • Loading branch information
fr2d9y2 authored and fr2d9y2 committed Apr 6, 2017
1 parent f13e4f3 commit cb7d1d7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
19 changes: 14 additions & 5 deletions mars-rover/src/rover.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,25 @@ def move_backward(self, delta):
self.x -= delta[0]
self.y -= delta[1]

def move_right(self, delta):
self.orientation = delta[3]

def move_left(self, delta):
self.orientation = delta[2]

# commands to dispatch for each move key
commands = {
'f': move_forward,
'b': move_backward
'b': move_backward,
'r': move_right,
'l': move_left
}

# x,y deltas to move forward for each orientation
# compoents are dx, dy, leftOrientation, rightOrientation
deltas = {
'N': [ +0, +1 ],
'S': [ +0, -1 ],
'E': [ +1, +0 ],
'W': [ -1, +0 ]
'N': [ +0, +1, 'W', 'E' ],
'S': [ +0, -1, 'E', 'W' ],
'E': [ +1, +0, 'N', 'S' ],
'W': [ -1, +0, 'S', 'N' ]
}
32 changes: 32 additions & 0 deletions mars-rover/test/test_rover.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def test_move_b(self):
assert_equal(10, r.x)
assert_equal(20, r.y)

def test_move_right(self):
r.move(list('r'))
assert_equal('E', r.orientation)

def test_move_left(self):
r.move(list('l'));
assert_equals('W', r.orientation)

class TestMovesOrientationSouth:

def setup(self):
Expand All @@ -49,6 +57,14 @@ def test_move_backward(self):
assert_equal(10, r.x)
assert_equal(22, r.y)

def test_move_right(self):
r.move(list('r'))
assert_equal('W', r.orientation)

def test_move_left(self):
r.move(list('l'));
assert_equals('E', r.orientation)

# would love use use test generators!
class TestMovesOrientationEast:

Expand All @@ -66,6 +82,14 @@ def test_move_backward(self):
assert_equal(9, r.x)
assert_equal(21, r.y)

def test_move_right(self):
r.move(list('r'))
assert_equal('S', r.orientation)

def test_move_left(self):
r.move(list('l'));
assert_equals('N', r.orientation)

class TestMovesOrientationWest:

def setup(self):
Expand All @@ -81,3 +105,11 @@ def test_move_backward(self):
r.move(list("b"))
assert_equal(11, r.x)
assert_equal(21, r.y)

def test_move_right(self):
r.move(list('r'))
assert_equal('N', r.orientation)

def test_move_left(self):
r.move(list('l'));
assert_equals('S', r.orientation)

0 comments on commit cb7d1d7

Please sign in to comment.