Skip to content

Commit

Permalink
Implement find_queen
Browse files Browse the repository at this point in the history
  • Loading branch information
Everesh committed Jun 15, 2024
1 parent f297113 commit 85a97e8
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def legal_move?(algebraic_notation, active_player, history)
end
return false if capture && (board[target[0]][target[1]] == ' ' || board[target[0]][target[1]].color == active_player || !en_passant?(active_player, history))

return false if king_would_be_in_check? || (!capture && board[target[0]][target[1]] == ' ')
return false if king_would_be_in_check? || (capture && board[target[0]][target[1]] == ' ')

if enemy_would_be_in_check?
if would_conclude?
Expand Down Expand Up @@ -181,9 +181,33 @@ def find_king(origin, active_player)
end

def find_queen(origin, active_player)
moves = [[[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0]],
[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7]],
[[0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7]],
[[-1, 1], [-2, 2], [-3, 3], [-4, 4], [-5, 5], [-6, 6], [-7, 7]],
[[-1, 0], [-2, 0], [-3, 0], [-4, 0], [-5, 0], [-6, 0], [-7, 0]],
[[-1, -1], [-2, -2], [-3, -3], [-4, -4], [-5, -5], [-6, -6], [-7, -7]],
[[0, -1], [0, -2], [0, -3], [0, -4], [0, -5], [0, -6], [0, -7]],
[[1, -1], [2, -2], [3, -3], [4, -4], [5, -5], [6, -6], [7, -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?(Queen) && 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 Queen'
raise StandardError
end

def find_bishop(origin, active_player)
Expand Down

0 comments on commit 85a97e8

Please sign in to comment.