diff --git a/lib/position.rb b/lib/position.rb index c1cae5a..e280d53 100644 --- a/lib/position.rb +++ b/lib/position.rb @@ -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 diff --git a/test/position_test.rb b/test/position_test.rb index 963c2ef..22ccc62 100644 --- a/test/position_test.rb +++ b/test/position_test.rb @@ -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