class Bchess::PGN::MoveInfoParser

Constants

CHECK
MATE
PROMOTION
TAKE
TAKE_2

Attributes

castle[RW]
info[RW]
move[RW]
move_string[RW]

Public Class Methods

new(move, castle) click to toggle source
# File lib/pgn/move_info_parser.rb, line 12
def initialize(move, castle)
  @move = move
  @castle = castle
end

Public Instance Methods

parse_move_string() click to toggle source
# File lib/pgn/move_info_parser.rb, line 17
def parse_move_string
  info = {}
  info.merge!(extract_promotion)
  move_split = @move.split('.')

  info.merge!(basic_move_data(move_split))
  return info if @castle

  move_string.tr!('+x:#', '')
  info.merge!(additional_info(move_string))
end

Private Instance Methods

additional_info(move_string) click to toggle source
# File lib/pgn/move_info_parser.rb, line 31
def additional_info(move_string)
  {
    piece_type: extract_piece_type(move_string),
    column: extract_column(move_string),
    row: extract_row(move_string),
    additional_info: extract_additional_info(move_string)
  }
end
basic_move_data(move_split) click to toggle source
# File lib/pgn/move_info_parser.rb, line 79
def basic_move_data(move_split)
  @move_string = move_split.last.strip

  raise InvalidMoveException, 'Wrong move description' if move_string.size < 2

  color = move_split.size == 2 ? Bchess::WHITE : Bchess::BLACK
  number = move_split.first.strip if move_split.size == 2
  {
    piece_color: color,
    move_number: number,
    check: move_string.include?(CHECK),
    take: move_string.include?(TAKE) || move_string.include?(TAKE_2),
    mate: move_string.include?(MATE)
  }
end
extract_additional_info(move_string) click to toggle source
# File lib/pgn/move_info_parser.rb, line 60
def extract_additional_info(move_string)
  if (move_string.size == 3) && pawn_taking?(move_string)
    move_string[0]
  elsif move_string.size == 4
    move_string[1]
  end
end
extract_column(move_string) click to toggle source
# File lib/pgn/move_info_parser.rb, line 52
def extract_column(move_string)
  move_string[move_string.size - 2]
end
extract_piece_type(move_string) click to toggle source
# File lib/pgn/move_info_parser.rb, line 40
def extract_piece_type(move_string)
  if move_string.size == 2 || move_string.size && pawn_taking?(move_string)
    Bchess::Pawn
  else
    get_piece_from_letter(move_string[0])
  end
end
extract_promotion() click to toggle source
# File lib/pgn/move_info_parser.rb, line 68
def extract_promotion
  promotion = @move.split(PROMOTION)

  if promotion.size == 2
    @move = promotion.first
    { promoted_piece: get_piece_from_letter(promotion.last[0]) }
  else
    {}
  end
end
extract_row(move_string) click to toggle source
# File lib/pgn/move_info_parser.rb, line 56
def extract_row(move_string)
  move_string[move_string.size - 1]
end
get_piece_from_letter(letter) click to toggle source
# File lib/pgn/move_info_parser.rb, line 95
def get_piece_from_letter(letter)
  {
    'K' => Bchess::King,
    'Q' => Bchess::Queen,
    'R' => Bchess::Rook,
    'B' => Bchess::Bishop,
    'N' => Bchess::Knight
  }[letter]
end
pawn_taking?(move_string) click to toggle source
# File lib/pgn/move_info_parser.rb, line 48
def pawn_taking?(move_string)
  move_string[0].ord >= 'a'.ord && move_string[0].ord <= 'h'.ord
end