Skip to content

Commit

Permalink
Refactor: extract the logic to an intention-revealing method.
Browse files Browse the repository at this point in the history
  • Loading branch information
hemalvarambhia committed Oct 25, 2023
1 parent 0e5e80f commit c1672e3
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(top_edge: 10, bottom_edge: 0, left_hand_edge: 0, right_hand_edge:
def next_coordinate_forwards(current_position:, direction:)
case direction
when 'N'
if current_position.y == @top_edge
if at_top_edge?(current_position)
Coordinates.new(x: current_position.x, y: @bottom_edge)
else
Coordinates.new(x: current_position.x, y: current_position.y + 1)
Expand All @@ -23,7 +23,7 @@ def next_coordinate_forwards(current_position:, direction:)
Coordinates.new(x: current_position.x + 1, y: current_position.y)
end
when 'S'
if current_position.y == @bottom_edge
if at_bottom_edge?(current_position)
Coordinates.new(x: current_position.x, y: @top_edge)
else
Coordinates.new(x: current_position.x, y: current_position.y - 1)
Expand Down Expand Up @@ -52,6 +52,14 @@ def next_coordinate_backwards(current_position:, direction:)

private

def at_top_edge?(current_position)
current_position.y == @top_edge
end

def at_bottom_edge?(current_position)
current_position.y == @bottom_edge
end

def at_left_hand_edge?(current_position)
current_position.x == @left_hand_edge
end
Expand Down

0 comments on commit c1672e3

Please sign in to comment.