Skip to content

Commit

Permalink
When the Mars rover is at the bottom edge, moving forward makes it
Browse files Browse the repository at this point in the history
reappear at the top edge.
  • Loading branch information
hemalvarambhia committed Oct 25, 2023
1 parent 61a6fd8 commit 0e5e80f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def next_coordinate_forwards(current_position:, direction:)
Coordinates.new(x: current_position.x + 1, y: current_position.y)
end
when 'S'
Coordinates.new(x: current_position.x, y: current_position.y - 1)
if current_position.y == @bottom_edge
Coordinates.new(x: current_position.x, y: @top_edge)
else
Coordinates.new(x: current_position.x, y: current_position.y - 1)
end
when 'W'
if at_left_hand_edge?(current_position)
Coordinates.new(x: @right_hand_edge, y: current_position.y)
Expand Down
9 changes: 8 additions & 1 deletion spec/moving_forwards_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@
expect(mars_rover).to be_located_at(bottom_edge)
end

it 'can move from the bottom edge of the planet and reappear at the top edge'
it 'can move from the bottom edge of the planet and reappear at the top edge' do
mars_rover = MarsRover.new(map: Map.new, starting_position: Coordinates.new(x: 5, y: 0), direction: 'S')

mars_rover.execute(['f'])

bottom_edge = Coordinates.new(x: 5, y: 10)
expect(mars_rover).to be_located_at(bottom_edge)
end
end
end

0 comments on commit 0e5e80f

Please sign in to comment.