Skip to content

Commit

Permalink
Implement find_rook
Browse files Browse the repository at this point in the history
  • Loading branch information
Everesh committed Jun 15, 2024
1 parent 847486b commit 827feec
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,29 @@ def find_knight(origin, active_player)
end

def find_rook(origin, active_player)
moves = [[[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0]],
[[0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7]],
[[-1, 0], [-2, 0], [-3, 0], [-4, 0], [-5, 0], [-6, 0], [-7, 0]],
[[0, -1], [0, -2], [0, -3], [0, -4], [0, -5], [0, -6], [0, -7]]]

# TO DO
moves.each do |direction|
direction.each do |move|
break if target[0] + move[0] < 0 || target[0] + move[0] > 7 || target[1] + move[1] < 0 || target[1] + move[1] > 7

candidate = board[target[0] + move[0]][target[1] + move[1]]

next if candidate == ' '

break unless candidate.is_a?(Rook) && candidate.color == active_player

next unless (origin[0].nil? || origin[0] == move[0]) && (origin[1].nil? || origin[1] == move[1])

return [target[0] + move[0], target[1] + move[1]]
end
end

puts '## Failed to find the Rook'
raise StandardError
end

def find_pawn(origin, active_player)
Expand Down

0 comments on commit 827feec

Please sign in to comment.