Browse Source

Rover can now get a specified location to land

Or ... it can simply land at a random location ...
Oz N Tiram 4 năm trước cách đây
mục cha
commit
a1ef0dca82
2 tập tin đã thay đổi với 11 bổ sung3 xóa
  1. 10 3
      src/rover/rover.py
  2. 1 0
      tests/test_rover.py

+ 10 - 3
src/rover/rover.py

@@ -1,3 +1,5 @@
+import random
+import warnings
 
 DIRECTIONS = ['N', 'E', 'S', 'W']
 
@@ -12,6 +14,11 @@ class Location:
 class Rover:
 
     def __init__(self, x=None, y=None, direction=None):
-        self.location = Location(2, 7, 'E')
-        self.location.x = 2
-        self.location.y = 7
+        if all((x, y, direction)):
+            self.location = Location(x, y, direction)
+        else:
+             warnings.warn("Landing at a random place")
+             self.location = Location(random.randint(-10, 10),
+                                      random.randint(-10, 10),
+                                      DIRECTIONS[random.randint(0,
+                                                 len(DIRECTIONS) - 1)])

+ 1 - 0
tests/test_rover.py

@@ -14,3 +14,4 @@ def test_has_location_and_direction():
 def test_can_land_at_specified_location():
     rover = Rover(x=1, y=2, direction='E')
     assert rover.location.x == 1
+    assert rover.location.direction == 'E'