Skip to content

Commit

Permalink
feat: implement positions_downwards and positions_upwards
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Studer committed Dec 23, 2021
1 parent 08ef1de commit 26f0bff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@ def positions_to_the_left
("A".."H").first(index).map { |l| Position.new(l, number) }.reverse
end

# all positions to the left, sorted by closest
# all positions to the right, sorted by closest
def positions_to_the_right
index = ("A".."H").find_index(letter)
("A".."H").last(7 - index).map { |l| Position.new(l, number) }
end

# all positions up, sorted by closest
def positions_upwards
((number+1)..(8)).map { |n| Position.new(letter, n) }
end

# all positions down, sorted by closest
def positions_downwards
(1..(number-1)).map { |n| Position.new(letter, n) }.reverse
end

def ==(other)
letter == other.letter && number == other.number
end
Expand Down
8 changes: 8 additions & 0 deletions test/position_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ def test_positions_to_the_left
def test_positions_to_the_right
assert_equal Position.parse(%w[G4 H4]), Position.parse("F4").positions_to_the_right
end

def test_positions_upwards
assert_equal Position.parse(%w[G7 G8]), Position.parse("G6").positions_upwards
end

def test_positions_downwards
assert_equal Position.parse(%w[G2 G1]), Position.parse("G3").positions_downwards
end
end

0 comments on commit 26f0bff

Please sign in to comment.