Module: SlidePattern

Included in:
Bishop, Pawn, Queen, Rook
Defined in:
lib/sapphire-chess/movement_rules/move_slide_pattern.rb

Instance Method Summary collapse

Instance Method Details

#available_movesObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sapphire-chess/movement_rules/move_slide_pattern.rb', line 2

def available_moves
  move_directions.each_with_object([]) do |(row_direction, column_direction), moves|
    current_row, current_column = location

    loop do
      current_row += row_direction
      current_column += column_direction
      possible_location = [current_row, current_column]

      break unless board.within_limits?(possible_location)
      break if friend_in?(possible_location)

      moves << possible_location if board.empty_square?(possible_location)

      if enemy_in?(possible_location)
        moves << possible_location
        break
      end
    end
  end
end