Skip to content

Commit

Permalink
Refactor/Move Method: moved a computation to a more sensible place.
Browse files Browse the repository at this point in the history
  • Loading branch information
hemalvarambhia committed Oct 31, 2023
1 parent f06d5b4 commit 57e6a68
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
16 changes: 12 additions & 4 deletions lib/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ def initialize(coordinates:, direction:)
end

def forwards
Location.new(
coordinates: Coordinates.new(x: coordinates.x, y: (coordinates.y + 1)),
direction: direction
)
case direction
when 'N'
Location.new(
coordinates: Coordinates.new(x: coordinates.x, y: (coordinates.y + 1)),
direction: direction
)
when 'S'
Location.new(
coordinates: Coordinates.new(x: coordinates.x, y: (coordinates.y - 1)),
direction: direction
)
end
end

def rotate_left
Expand Down
19 changes: 10 additions & 9 deletions lib/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,8 @@ def next_location_forwards(location:)
direction: location.direction
)
when 'S'
new_location = Location.new(
coordinates: Coordinates.new(x: location.coordinates.x, y: (location.coordinates.y - 1)),
direction: location.direction
)
if located_at_south_pole? new_location.coordinates
Location.new(coordinates: Coordinates.new(x: 18, y: -8), direction: 'N')
else
new_location
end
new_location = location.forwards
corrected_for_south_pole(new_location)
when 'W'
next_x = at_left_hand_edge?(location.coordinates) ? @x_domain.end : location.coordinates.x - 1
Location.new(
Expand Down Expand Up @@ -65,6 +58,14 @@ def next_location_backwards(location:)

private

def corrected_for_south_pole(location)
if located_at_south_pole? location.coordinates
Location.new(coordinates: Coordinates.new(x: 18, y: -8), direction: 'N')
else
location
end
end

def corrected_for_north_pole(location)
if at_north_pole? location.coordinates
Location.south_facing(coordinates: Coordinates.new(x: location.coordinates.x + 18, y: 8))
Expand Down

0 comments on commit 57e6a68

Please sign in to comment.