Module: Egd::Procedures

Extended by:
Procedures
Included in:
Procedures
Defined in:
lib/egd/procedures.rb

Instance Method Summary collapse

Instance Method Details

#fen_index_to_square(index) ⇒ Object

Egd::Procedures.fen_index_to_square(index)



31
32
33
34
35
36
37
38
39
40
# File 'lib/egd/procedures.rb', line 31

def fen_index_to_square(index)
  # 3 -> c8
  row = Egd::COLUMN_HEIGHT - ((index - 1) / Egd::COLUMN_HEIGHT) # => 8

  column_index = index - ((Egd::COLUMN_HEIGHT - row) * Egd::ROW_LENGTH)

  column = Egd::FenToBoard::LETTER_VALUES[column_index] # => "c"

  "#{column}#{row}"
end

#parse_fen(fen) ⇒ Object

Egd::Procedures.parse_fen(fen)



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/egd/procedures.rb', line 8

def parse_fen(fen)
  match = fen.split(%r'\s+') # FEN lines are delimited with whitespace, splitting on that

  {
    board: match[0],
    to_move: match[1],
    castling: match[2],
    ep_square: match[3],
    halfmove: match[4],
    fullmove: match[5]
  }
end

#square_color(square) ⇒ Object

Egd::Procedures.square_color(“a7”)



43
44
45
46
47
48
49
50
51
52
# File 'lib/egd/procedures.rb', line 43

def square_color(square)
  column = square[0] #=> a
  row = square[1] #=> 8

  if %w|a c e g|.include?(column)
    row.to_i.even? ? "w" : "b"
  else
    row.to_i.odd? ? "w" : "b"
  end
end

#square_to_fen_index(square) ⇒ Object

Egd::Procedures.square_to_fen_index(“b2”)



22
23
24
25
26
27
28
# File 'lib/egd/procedures.rb', line 22

def square_to_fen_index(square)
  column = square[0]
  row = square[1]

  row_value = Egd::COLUMN_HEIGHT - row.to_i
  row_value * Egd::ROW_LENGTH + Egd::FenToBoard::LETTER_VALUES.index(column)
end