Module: CastleHelpers

Included in:
Bchess::Board
Defined in:
lib/bchess/helpers/castle_helpers.rb

Instance Method Summary collapse

Instance Method Details

#black_castle(column) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/bchess/helpers/castle_helpers.rb', line 29

def black_castle(column)
  if short_castle?(column)
    @short_castle = true
    at(7,7)
  else
    @short_castle = false
    at(0,7)
  end
end

#castle(piece, column, row) ⇒ Object



2
3
4
5
6
# File 'lib/bchess/helpers/castle_helpers.rb', line 2

def castle(piece, column, row)
  validate_castle(piece, column)
  update_castles_after_king_move(piece.color)
  execute_castle(piece, column, row)
end

#castling_rook(piece, column) ⇒ Object



43
44
45
# File 'lib/bchess/helpers/castle_helpers.rb', line 43

def castling_rook(piece, column)
  select_rook(piece, column)
end

#execute_castle(piece, column, row) ⇒ Object



12
13
14
15
16
17
# File 'lib/bchess/helpers/castle_helpers.rb', line 12

def execute_castle(piece, column, row)
  piece.move(column, row)
  lngth = @short_castle ? 2 : 3
  @castling_rook = castling_rook(piece, column)
  @castling_rook.move((@castling_rook.column - lngth).abs, row)
end

#rook_moved?(piece, column) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/bchess/helpers/castle_helpers.rb', line 39

def rook_moved?(piece, column)
  castling_rook(piece, column)&.moved
end

#select_rook(piece, column) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/bchess/helpers/castle_helpers.rb', line 47

def select_rook(piece, column)
  if piece.row == 0
    white_castle(column)
  elsif piece.row == 7
    black_castle(column)
  end
end

#short_castle?(column) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/bchess/helpers/castle_helpers.rb', line 55

def short_castle?(column)
  column == 6
end

#validate_castle(piece, column) ⇒ Object



8
9
10
# File 'lib/bchess/helpers/castle_helpers.rb', line 8

def validate_castle(piece, column)
  !piece.moved && !rook_moved?(piece, column) && fen_allows?(piece,column)
end

#white_castle(column) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/bchess/helpers/castle_helpers.rb', line 19

def white_castle(column)
  if short_castle?(column)
    @short_castle = true
    at(7,0)
  else
    @short_castle = false
    at(0,0)
  end
end