Эх сурвалжийг харах

Add north bound movements and reporting

Oz N Tiram 4 жил өмнө
parent
commit
d0adbd9016

+ 15 - 0
src/rover/rover.py

@@ -27,3 +27,18 @@ class Rover:
         for item in command:
             if item == 'F' and self.location.heading == 'E':
                 self.location.x += 1
+
+            if item == 'F' and self.location.heading == 'N':
+                self.location.y += 1
+            # ... TODO: add all directions and check constraints
+            # if going over the borders calculate roll-over ...
+            # e.g, if after a few moves X location is E is 11 it should be
+            # reported and saved as W -1.
+
+        # separate moving from reporting
+        self._report()
+
+    def _report(self):
+        """naive implementation that prints to stdout, can be changed to
+        send a radio message"""
+        print(f"Location ({self.location.x}, {self.location.y}) Heading {self.location.heading}")

+ 6 - 0
tests/test_rover.py

@@ -27,3 +27,9 @@ def test_can_move_north():
     rover = Rover(x=1, y=2, heading='N')
     rover.move('FFF')
     assert rover.location.y == 5
+
+def test_rover_reports_location(capsys):
+    rover = Rover(x=1, y=2, heading='N')
+    rover.move('FFF')
+    captured = capsys.readouterr()
+    captured.out.split() == 'Location (5, 2) Heading N'