Module: CastlingBoardControl

Included in:
Board
Defined in:
lib/sapphire-chess/movement_rules/castling_board_control.rb

Instance Method Summary collapse

Instance Method Details

#castle!(side, color, permanent: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sapphire-chess/movement_rules/castling_board_control.rb', line 9

def castle!(side, color, permanent: false)
  castling_squares =
    if color == :white && side == :king then [[7, 4], [7, 6], [7, 7], [7, 5]]
    elsif color == :white && side == :queen then [[7, 4], [7, 2], [7, 0], [7, 3]]
    elsif color == :black && side == :king then [[0, 4], [0, 6], [0, 7], [0, 5]]
    else
      [[0, 4], [0, 2], [0, 0], [0, 3]]
    end

  king_start, king_target, rook_start, rook_target = castling_squares

  move_piece!(king_start, king_target, permanent: permanent)
  move_piece!(rook_start, rook_target, permanent: permanent)
end

#mark_moved_piece!(piece) ⇒ Object

(See Castling, movement.rb::CastlingPieceControl, Rook, King)



3
4
5
6
7
# File 'lib/sapphire-chess/movement_rules/castling_board_control.rb', line 3

def mark_moved_piece!(piece)
  return unless self[piece].is_a?(Rook) || self[piece].is_a?(King)

  self[piece].mark!
end

#uncastle!(side, color) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sapphire-chess/movement_rules/castling_board_control.rb', line 24

def uncastle!(side, color)
  castling_squares =
    if color == :white && side == :king then [[7, 6], [7, 4], [7, 5], [7, 7]]
    elsif color == :white && side == :queen then [[7, 2], [7, 4], [7, 3], [7, 0]]
    elsif color == :black && side == :king then [[0, 6], [0, 4], [0, 5], [0, 7]]
    else
      [[0, 2], [0, 4], [0, 3], [0, 0]]
    end
  king_start, king_target, rook_start, rook_target = castling_squares

  move_piece!(king_start, king_target, permanent: false)
  move_piece!(rook_start, rook_target, permanent: false)
end