Skip to content

Commit

Permalink
refactor: migrated a client to use the new methods on Location.
Browse files Browse the repository at this point in the history
  • Loading branch information
hemalvarambhia committed Mar 27, 2024
1 parent 656a088 commit 6d16779
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ def initialize(x_domain:, y_domain:, obstacles: [])
def next_location_forwards(location:)
case location.direction
when 'N'
next_y = at_north_pole?(location.coordinates) ? @y_domain.begin : location.coordinates.y + 1
next_y = at_north_pole?(location.coordinates) ? @y_domain.begin : location.y + 1
Location.new(
coordinates: Coordinates.new(x: location.coordinates.x, y: next_y),
coordinates: Coordinates.new(x: location.x, y: next_y),
direction: location.direction
)
when 'E'
next_x = at_right_hand_edge?(location.coordinates) ? @x_domain.begin : location.coordinates.x + 1
next_x = at_right_hand_edge?(location.coordinates) ? @x_domain.begin : location.x + 1
Location.new(
coordinates: Coordinates.new(x: next_x, y: location.coordinates.y),
coordinates: Coordinates.new(x: next_x, y: location.y),
direction: location.direction
)
when 'S'
next_y = at_south_pole?(location.coordinates) ? @y_domain.end : location.coordinates.y - 1
next_y = at_south_pole?(location.coordinates) ? @y_domain.end : location.y - 1
Location.new(
coordinates: Coordinates.new(x: location.coordinates.x, y: next_y),
coordinates: Coordinates.new(x: location.x, y: next_y),
direction: location.direction
)
when 'W'
next_x = at_left_hand_edge?(location.coordinates) ? @x_domain.end : location.coordinates.x - 1
next_x = at_left_hand_edge?(location.coordinates) ? @x_domain.end : location.x - 1
Location.new(
coordinates: Coordinates.new(x: next_x, y: location.coordinates.y),
coordinates: Coordinates.new(x: next_x, y: location.y),
direction: location.direction
)
end
Expand All @@ -36,27 +36,27 @@ def next_location_forwards(location:)
def next_location_backwards(location:)
case location.direction
when 'N'
next_y = at_south_pole?(location.coordinates) ? @y_domain.end : location.coordinates.y - 1
next_y = at_south_pole?(location.coordinates) ? @y_domain.end : location.y - 1
Location.new(
coordinates: Coordinates.new(x: location.coordinates.x, y: next_y),
coordinates: Coordinates.new(x: location.x, y: next_y),
direction: location.direction
)
when 'E'
next_x = at_left_hand_edge?(location.coordinates) ? @x_domain.end : location.coordinates.x - 1
next_x = at_left_hand_edge?(location.coordinates) ? @x_domain.end : location.x - 1
Location.new(
coordinates: Coordinates.new(x: next_x, y: location.coordinates.y),
coordinates: Coordinates.new(x: next_x, y: location.y),
direction: location.direction
)
when 'S'
next_y = at_north_pole?(location.coordinates) ? @y_domain.begin : location.coordinates.y + 1
next_y = at_north_pole?(location.coordinates) ? @y_domain.begin : location.y + 1
Location.new(
coordinates: Coordinates.new(x: location.coordinates.x, y: next_y),
coordinates: Coordinates.new(x: location.x, y: next_y),
direction: location.direction
)
when 'W'
next_x = at_right_hand_edge?(location.coordinates) ? @x_domain.begin : location.coordinates.x + 1
next_x = at_right_hand_edge?(location.coordinates) ? @x_domain.begin : location.x + 1
Location.new(
coordinates: Coordinates.new(x: next_x, y: location.coordinates.y),
coordinates: Coordinates.new(x: next_x, y: location.y),
direction: location.direction
)
end
Expand Down

0 comments on commit 6d16779

Please sign in to comment.