class Bchess::PGN::GameBody

Constants

BLACK_ROW
C_COLUMN
G_COLUMN
WHITE_ROW

Attributes

board[R]
body[R]
moves[R]

Public Class Methods

new(body) click to toggle source
# File lib/pgn/game_body.rb, line 14
def initialize(body)
  @body = body
  @board = Bchess::Board.new
  @moves = []
end

Public Instance Methods

extract_move(move) click to toggle source
# File lib/pgn/game_body.rb, line 37
def extract_move(move)
  move_text = move.text_value
  info = return_move_information(move_text)

  piece = get_moving_piece(board, info)
  {
    piece: piece,
    column: to_column(info[:column]),
    row: to_row(info[:row]),
    promoted_piece: info[:promoted_piece]
  }
end
extract_moves() click to toggle source
# File lib/pgn/game_body.rb, line 20
def extract_moves
  @board.read_fen

  body.elements&.each do |move|
    if is_castle?(move)
      move_info = extract_castle(move)
    elsif is_move?(move)
      move_info = extract_move(move)
    else
      next
    end

    board.move(*move_info.values)
    @moves << move_info
  end
end

Private Instance Methods

extract_castle(move) click to toggle source
# File lib/pgn/game_body.rb, line 62
def extract_castle(move)
  move_text = move.text_value
  info = return_move_information(move_text, true)

  if C_COLUMN == move_text.split('-').size
    column = G_COLUMN
    row = is_white?(info) ? WHITE_ROW : BLACK_ROW
  else
    column = C_COLUMN
    row = is_white?(info) ? WHITE_ROW : BLACK_ROW
  end

  {
    piece: board.king(info[:piece_color]),
    column: column,
    row: row,
    promoted_piece: false
  }
end
get_moving_piece(board, info) click to toggle source
# File lib/pgn/game_body.rb, line 52
def get_moving_piece(board, info)
  pieces = board.get_possible_pieces(info)

  if pieces.size != 1
    raise Bchess::InvalidMoveException.new("Too many or too few pieces to make move: #{info} : #{pieces.size}")
  end

  pieces.first
end
is_castle?(move) click to toggle source
# File lib/pgn/game_body.rb, line 90
def is_castle?(move)
  move.kind_of?(Sexp::PCastle)
end
is_comment?(move) click to toggle source
# File lib/pgn/game_body.rb, line 94
def is_comment?(move)
  move.kind_of?(Sexp::PCommentWithBracket)
end
is_move?(move) click to toggle source
# File lib/pgn/game_body.rb, line 86
def is_move?(move)
  move.kind_of?(Sexp::PMove)
end
is_variation?(move) click to toggle source
# File lib/pgn/game_body.rb, line 98
def is_variation?(move)
  move.kind_of?(Sexp::PVariation)
end
is_white?(info) click to toggle source
# File lib/pgn/game_body.rb, line 102
def is_white?(info)
  Bchess::WHITE == info[:piece_color]
end
promotion?(info) click to toggle source
# File lib/pgn/game_body.rb, line 106
def promotion?(info)
  info[:promoted_piece]
end
return_move_information(move_text,castle=false) click to toggle source
# File lib/pgn/game_body.rb, line 82
def return_move_information(move_text,castle=false)
  Bchess::PGN::MoveInfoParser.new(move_text, castle).parse_move_string
end