Class: Bchess::Board

Inherits:
Object
  • Object
show all
Includes:
BoardHelpers, FenHelpers, CastleHelpers, EnPassantHelpers, Validations
Defined in:
lib/bchess/board.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations

#valid_position?, #validate_move

Methods included from EnPassantHelpers

#execute_en_passant, #long_pawn_move, #remove_en_passant, #validate_en_passant

Methods included from CastleHelpers

#black_castle, #castle, #castling_rook, #execute_castle, #rook_moved?, #select_rook, #short_castle?, #validate_castle, #white_castle

Methods included from FenHelpers

#additional_info, #change_halfmove_clock, #change_move_number, #create_fen_line, #fen_allows?, #fen_hash, #set_castles, #set_en_passant, #set_halfmove_clock, #set_move_number, #set_pieces, #set_to_move, #to_fen, #update_castles_after_king_move, #update_castles_after_move, #write_fen

Methods included from BoardHelpers

#castle_detected?, #en_passant_detected?, #field, #invalid_data?, #kings_present?, #pawn_long_move_detected?, #promotion_detected?, #to_column, #to_row

Constructor Details

#initialize(fen = Bchess::START_FEN, to_move = Bchess::WHITE) ⇒ Board

Returns a new instance of Board.



11
12
13
14
15
16
17
18
19
# File 'lib/bchess/board.rb', line 11

def initialize(fen = Bchess::START_FEN, to_move = Bchess::WHITE)
  @fen = fen
  @to_move = to_move
  @move_number = 0
  @halfmove_clock = 0
  @pieces = []
  @en_passant = '-'
  @castles = '-'
end

Instance Attribute Details

#castlesObject

Returns the value of attribute castles.



9
10
11
# File 'lib/bchess/board.rb', line 9

def castles
  @castles
end

#en_passantObject

Returns the value of attribute en_passant.



9
10
11
# File 'lib/bchess/board.rb', line 9

def en_passant
  @en_passant
end

#fenObject

Returns the value of attribute fen.



9
10
11
# File 'lib/bchess/board.rb', line 9

def fen
  @fen
end

#halfmove_clockObject

Returns the value of attribute halfmove_clock.



9
10
11
# File 'lib/bchess/board.rb', line 9

def halfmove_clock
  @halfmove_clock
end

#move_numberObject

Returns the value of attribute move_number.



9
10
11
# File 'lib/bchess/board.rb', line 9

def move_number
  @move_number
end

#piecesObject

Returns the value of attribute pieces.



9
10
11
# File 'lib/bchess/board.rb', line 9

def pieces
  @pieces
end

#to_moveObject

Returns the value of attribute to_move.



9
10
11
# File 'lib/bchess/board.rb', line 9

def to_move
  @to_move
end

Instance Method Details

#at(column, row) ⇒ Object



73
74
75
# File 'lib/bchess/board.rb', line 73

def at(column, row)
  pieces.select { |p| p.row == row && p.column == column }.first
end

#change_to_moveObject



98
99
100
# File 'lib/bchess/board.rb', line 98

def change_to_move
  @to_move = to_move == Bchess::WHITE ? Bchess::BLACK : Bchess::WHITE
end

#execute_promotion(piece, column, row, promoted_piece) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/bchess/board.rb', line 60

def execute_promotion(piece, column, row, promoted_piece)
  raise 'Promotion Not Specified' if promoted_piece.nil? || !(promoted_piece < Bchess::Piece)

  pieces.delete(piece)
  promoted = promoted_piece.new(piece.color, column, row)
  promoted.moved = true if promoted_piece == Bchess::Rook
  pieces << promoted
end

#get_possible_pieces(info) ⇒ Object



48
49
50
# File 'lib/bchess/board.rb', line 48

def get_possible_pieces(info)
  @pieces.select { |p| p.can_make_move?(info, self) }
end

#king(color) ⇒ Object



94
95
96
# File 'lib/bchess/board.rb', line 94

def king(color)
  @pieces.select { |p| p.class == Bchess::King && p.color == color }.first
end

#move(piece, column, row, promoted_piece = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bchess/board.rb', line 21

def move(piece, column, row, promoted_piece = nil)
  return false if invalid_data?(piece, column, row)

  @en_passant = '-' unless en_passant_detected?(piece, column, row)

  if castle_detected?(piece, column)
    castle(piece, column, row)
  elsif promotion_detected?(piece, row)
    execute_promotion(piece, column, row, promoted_piece)
  elsif en_passant_detected?(piece, column, row)
    validate_en_passant(piece, column, row) && execute_en_passant(piece, column, row)
  elsif pawn_long_move_detected?(piece, row)
    long_pawn_move(piece, column, row)
  elsif piece.can_move_to_field?(column, row)
    piece.move(column, row)
  elsif piece.can_take_on_field?(column, row)
    piece.move(column, row)
  else
    return false
  end

  update_info(piece, column, row)
  validate_move
rescue RuntimeError
  false
end


87
88
89
90
91
92
# File 'lib/bchess/board.rb', line 87

def print
  puts 'Pieces WHITE: '
  puts _pieces(Bchess::WHITE).map(&:to_s).join(', ')
  puts 'Pieces BLACK: '
  puts _pieces(Bchess::BLACK).map(&:to_s).join(', ')
end

#read_fenObject



77
78
79
80
81
82
83
84
85
# File 'lib/bchess/board.rb', line 77

def read_fen
  fen_pieces, fen_colors, fen_castles, fen_en_passant, fen_halfmove_clock, fen_move_number = fen.strip.split(' ')
  set_pieces(fen_pieces)
  set_to_move(fen_colors)
  set_castles(fen_castles)
  set_en_passant(fen_en_passant)
  set_halfmove_clock(fen_halfmove_clock)
  set_move_number(fen_move_number)
end

#transform_field(field) ⇒ Object



69
70
71
# File 'lib/bchess/board.rb', line 69

def transform_field(field)
  [field.chars.first.ord - 97, field.chars.last.to_i - 1]
end

#update_info(piece, column, row) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/bchess/board.rb', line 52

def update_info(piece, column, row)
  update_castles_after_move(piece) if piece.moved
  change_halfmove_clock(piece)
  change_to_move
  change_move_number if piece.color == Bchess::BLACK
  remove_old_piece(column, row, piece.color) if !!at(column, row)
end