class Egd::FenToBoard

Constants

LETTER_VALUES

Attributes

fen[R]

this service parses a FEN string into a hash-representation of a chess board and pieces So you can do board = Egd::FenToBoard.new(fen_string) board #=> ā€œPā€ # as in white pawn

Public Class Methods

new(fen) click to toggle source

Egd::FenToBoard.new(fen_string)["b2"]

# File lib/egd/fen_to_board.rb, line 14
def initialize(fen)
  @fen = fen
end

Public Instance Methods

[](square) click to toggle source
# File lib/egd/fen_to_board.rb, line 18
def [](square)
  board_hash[square]
end
boardline() click to toggle source
# File lib/egd/fen_to_board.rb, line 22
def boardline
  # this replaces numbers with corresponding amount of dashes
  @boardline ||= parsed_fen[:board].gsub(%r'\d') do |match|
    "-" * match.to_i
  end.gsub("/", "")
end

Private Instance Methods

board_hash() click to toggle source
# File lib/egd/fen_to_board.rb, line 34
def board_hash
  return @board_hash if defined?(@board_hash)

  look_up_square_behavior = ->(hash, key) {
    hash[key] = boardline[
      Egd::Procedures.square_to_fen_index(key) - 1
    ]
  }

  @board_hash = Hash.new(&look_up_square_behavior)
end
parsed_fen() click to toggle source
# File lib/egd/fen_to_board.rb, line 30
def parsed_fen
  @parsed_fen ||= Egd::Procedures.parse_fen(fen)
end