module Egd::Procedures

Public Instance Methods

fen_index_to_square(index) click to toggle source

Egd::Procedures.fen_index_to_square(index)

# 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) click to toggle source

Egd::Procedures.parse_fen(fen)

# 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) click to toggle source

Egd::Procedures.square_color(“a7”)

# 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) click to toggle source

Egd::Procedures.square_to_fen_index(“b2”)

# 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