Module: Checkers::Board::Moves

Included in:
Checkers::Board
Defined in:
lib/checkers/board/moves.rb

Instance Method Summary collapse

Instance Method Details

#basic_moves(row:, col:, player:) ⇒ Object



26
27
28
29
30
# File 'lib/checkers/board/moves.rb', line 26

def basic_moves(row:, col:, player:)
  possible_squares(row: row, col: col, player: player) do |squares|
    squares.filter_map { |square| Move.new([row, col], square) if move?(row: square[0], col: square[1]) }
  end
end

#find_available_moves(row:, col:, player:) ⇒ Object



6
7
8
9
10
11
# File 'lib/checkers/board/moves.rb', line 6

def find_available_moves(row:, col:, player:)
  jumps = jump_moves(row: row, col: col, player: player)
  return jumps if jumps.any?

  basic_moves(row: row, col: col, player: player)
end

#jump_moves(row:, col:, player:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/checkers/board/moves.rb', line 13

def jump_moves(row:, col:, player:)
  jump_moves = []
  adjacent_squares(row: row, col: col, player: player).each do |square|
    adjacent_row, adjacent_col = square
    next if [0, *player_pieces(player)].include?(@board[adjacent_row, adjacent_col])

    vector = [adjacent_row - row, adjacent_col - col]
    jump_square = [adjacent_row + vector[0], adjacent_col + vector[1]]
    jump_moves << JumpMove.new([row, col], jump_square) if move?(row: jump_square[0], col: jump_square[1])
  end
  jump_moves
end