diff --git a/lib/map.rb b/lib/map.rb index 191ca289..7ab1a6e6 100644 --- a/lib/map.rb +++ b/lib/map.rb @@ -61,7 +61,7 @@ def next_location_backwards(location:) end def located_at_north_pole?(coordinates) - coordinates == Coordinates.new(x: 0, y: 9) + @x_domain.map { |x| Coordinates.new(x: x, y: 9) }.include?(coordinates) end private diff --git a/lib/mars_rover.rb b/lib/mars_rover.rb index 79c5e150..979d005c 100644 --- a/lib/mars_rover.rb +++ b/lib/mars_rover.rb @@ -15,7 +15,7 @@ def execute(commands) when 'f' new_location = forwards(location) if @map.located_at_north_pole? new_location.coordinates - Location.new(coordinates: Coordinates.new(x: 0 + 18, y: 8), direction: 'S') + Location.new(coordinates: Coordinates.new(x: new_location.coordinates.x + 18, y: 8), direction: 'S') else new_location end diff --git a/spec/moving_forwards_spec.rb b/spec/moving_forwards_spec.rb index fd84ff21..6d5b1350 100644 --- a/spec/moving_forwards_spec.rb +++ b/spec/moving_forwards_spec.rb @@ -139,6 +139,14 @@ def self.mars_rover(map: Map.new(x_domain: (0..10), y_domain: (0..10)), located_ expect(mars_rover).to be_facing('S') end + it 'can move to the north pole from any longitude' do + mars_rover = a_mars_rover(located_at: Location.new(coordinates: Coordinates.new(x: 1, y: 8), direction: 'N')) + + mars_rover.execute(['f']) + + expect(mars_rover).to be_located_at(Coordinates.new(x: 19, y: 8)) + end + it 'can move to the south pole of the planet' it 'faces north upon reaching the south pole'