Module: Bchess::FenHelpers

Included in:
Board
Defined in:
lib/bchess/helpers/fen_helpers.rb

Instance Method Summary collapse

Instance Method Details

#additional_infoObject



137
138
139
# File 'lib/bchess/helpers/fen_helpers.rb', line 137

def additional_info
  " #{to_move} #{castles} #{en_passant} #{halfmove_clock} #{move_number}"
end

#change_halfmove_clock(piece) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/bchess/helpers/fen_helpers.rb', line 88

def change_halfmove_clock(piece)
  @halfmove_clock = if piece.is_a?(Bchess::Pawn)
                      0
                    else
                      halfmove_clock + 1
                    end
end

#change_move_numberObject



113
114
115
# File 'lib/bchess/helpers/fen_helpers.rb', line 113

def change_move_number
  @move_number = move_number + 1
end

#create_fen_line(pieces) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bchess/helpers/fen_helpers.rb', line 31

def create_fen_line(pieces)
  line = ''
  counter = 0

  0.upto(7) do |i|
    piece = pieces.select { |p| p.column == i }.first
    if !!piece
      if counter > 0
        line.concat(counter.to_s)
        counter = 0
        line.concat(to_fen(piece))
      else
        line.concat(to_fen(piece))
      end
    else
      counter += 1
    end
  end
  line.concat(counter.to_s) if counter > 0
  line
end

#fen_allows?(piece, column) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bchess/helpers/fen_helpers.rb', line 68

def fen_allows?(piece, column)
  if piece.color == Bchess::WHITE
    if column == 6
      castles.chars.include?('k')
    else
      castles.chars.include?('q')
    end
  else
    if column == 6
      castles.chars.include?('K')
    else
      castles.chars.include?('Q')
    end
  end
end

#fen_hashObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bchess/helpers/fen_helpers.rb', line 3

def fen_hash
  {
    'k': { klass: Bchess::King, color: Bchess::BLACK },
    'q': { klass: Bchess::Queen, color: Bchess::BLACK },
    'r': { klass: Bchess::Rook, color: Bchess::BLACK },
    'b': { klass: Bchess::Bishop, color: Bchess::BLACK },
    'n': { klass: Bchess::Knight, color: Bchess::BLACK },
    'p': { klass: Bchess::Pawn, color: Bchess::BLACK },
    'K': { klass: Bchess::King, color: Bchess::WHITE },
    'Q': { klass: Bchess::Queen, color: Bchess::WHITE },
    'R': { klass: Bchess::Rook, color: Bchess::WHITE },
    'B': { klass: Bchess::Bishop, color: Bchess::WHITE },
    'N': { klass: Bchess::Knight, color: Bchess::WHITE },
    'P': { klass: Bchess::Pawn, color: Bchess::WHITE }
  }
end

#set_castles(fen_castles) ⇒ Object



121
122
123
# File 'lib/bchess/helpers/fen_helpers.rb', line 121

def set_castles(fen_castles)
  @castles = fen_castles
end

#set_en_passant(fen_en_passant) ⇒ Object



125
126
127
# File 'lib/bchess/helpers/fen_helpers.rb', line 125

def set_en_passant(fen_en_passant)
  @en_passant = fen_en_passant
end

#set_halfmove_clock(fen_halfmove_clock) ⇒ Object



129
130
131
# File 'lib/bchess/helpers/fen_helpers.rb', line 129

def set_halfmove_clock(fen_halfmove_clock)
  @halfmove_clock = fen_halfmove_clock.to_i
end

#set_move_number(fen_move_number) ⇒ Object



133
134
135
# File 'lib/bchess/helpers/fen_helpers.rb', line 133

def set_move_number(fen_move_number)
  @move_number = fen_move_number.to_i
end

#set_pieces(board) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bchess/helpers/fen_helpers.rb', line 53

def set_pieces(board)
  pieces.clear
  board.split('/').each_with_index do |line, index|
    column = 0
    line.each_char do |char|
      if char.to_i != 0
        column += char.to_i - 1
      else
        pieces << fen_hash[char.to_sym][:klass].new(fen_hash[char.to_sym][:color], column, 7 - index)
      end
      column += 1
    end
  end
end

#set_to_move(fen_colors) ⇒ Object



117
118
119
# File 'lib/bchess/helpers/fen_helpers.rb', line 117

def set_to_move(fen_colors)
  @to_move = fen_colors == 'w' ? Bchess::WHITE : Bchess::BLACK
end

#to_fen(piece) ⇒ Object



84
85
86
# File 'lib/bchess/helpers/fen_helpers.rb', line 84

def to_fen(piece)
  fen_hash.key(klass: piece.class, color: piece.color).to_s
end

#update_castles_after_king_move(color) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/bchess/helpers/fen_helpers.rb', line 105

def update_castles_after_king_move(color)
  if color == Bchess::WHITE
    @castles.delete!('K').delete!('Q')
  else
    @castles.delete!('k').delete!('q')
  end
end

#update_castles_after_move(piece) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/bchess/helpers/fen_helpers.rb', line 96

def update_castles_after_move(piece)
  if piece == Bchess::King
    update_castles_after_king_move(piece.color)
  elsif piece == Bchess::Rook
    update_castles_after_rook_move(piece)
  end
  @castles = '-' if @castles == ''
end

#write_fenObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/bchess/helpers/fen_helpers.rb', line 20

def write_fen
  result = ''
  7.downto(0) do |i|
    line_pieces = pieces.select { |p| p.row == i }
    one_line = create_fen_line(line_pieces)
    result << one_line
    result << '/' unless i == 0
  end
  result.concat(additional_info)
end