Class: PGN::EPD

Inherits:
Object
  • Object
show all
Defined in:
lib/pgn/epd.rb

Overview

EPD translates between strings in Extended Position Description and a Position. EPD is the FEN-like position format used by EPD tools: it shares FEN's first four fields (piece placement, side to move, castling availability, en passant target square) and then carries a trailing list of operations (ops) instead of the halfmove/fullmove counters.

Examples:

PGN::EPD.new('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -')
PGN::FEN.start.to_epd #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(epd_string = nil) ⇒ EPD

Returns a new instance of EPD.

Parameters:

  • epd_string (String) (defaults to: nil)

    an EPD string: four FEN fields followed by zero or more operation fields (kept verbatim as #ops).



23
24
25
26
27
28
29
30
31
32
# File 'lib/pgn/epd.rb', line 23

def initialize(epd_string = nil)
  return unless epd_string

  fields = epd_string.split(' ', 5)
  self.board_string = fields[0]
  self.active = fields[1]
  self.castling = fields[2]
  self.en_passant = fields[3]
  self.ops = fields[4]
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



17
18
19
# File 'lib/pgn/epd.rb', line 17

def active
  @active
end

#boardObject

Returns the value of attribute board.



17
18
19
# File 'lib/pgn/epd.rb', line 17

def board
  @board
end

#castlingObject

Returns the value of attribute castling.



18
19
20
# File 'lib/pgn/epd.rb', line 18

def castling
  @castling
end

#en_passantObject

Returns the value of attribute en_passant.



18
19
20
# File 'lib/pgn/epd.rb', line 18

def en_passant
  @en_passant
end

#opsObject

Returns the value of attribute ops.



17
18
19
# File 'lib/pgn/epd.rb', line 17

def ops
  @ops
end

Instance Method Details

#board_stringString

Returns the EPD board-string portion.

Returns:

  • (String)

    the EPD board-string portion



56
57
58
# File 'lib/pgn/epd.rb', line 56

def board_string
  board.fen_board_string
end

#board_string=(board_fen) ⇒ Object

Parameters:

  • board_fen (String)

    the FEN/EPD representation of the board



44
45
46
47
48
49
50
51
52
# File 'lib/pgn/epd.rb', line 44

def board_string=(board_fen)
  squares = board_fen.gsub(/\d/) { |match| '_' * match.to_i }
                     .split('/')
                     .map(&:chars)
                     .map { |row| row.map { |e| e == '_' ? nil : e } }
                     .reverse
                     .transpose
  self.board = PGN::Board.new(squares)
end

#inspectObject



77
78
79
# File 'lib/pgn/epd.rb', line 77

def inspect
  to_s
end

#to_positionPGN::Position

Returns a Position for this EPD. Halfmove and fullmove default to 0 and 1 (EPD does not carry them).

Returns:

  • (PGN::Position)

    a Position for this EPD. Halfmove and fullmove default to 0 and 1 (EPD does not carry them).



63
64
65
66
67
68
69
# File 'lib/pgn/epd.rb', line 63

def to_position
  player = active == 'w' ? :white : :black
  castling_rights = castling.chars - ['-']
  ep = en_passant == '-' ? nil : en_passant

  PGN::Position.new(board, player, castling_rights, ep, 0, 1)
end

#to_sString

Returns the EPD string (four fields, then ops if present).

Returns:

  • (String)

    the EPD string (four fields, then ops if present)



73
74
75
# File 'lib/pgn/epd.rb', line 73

def to_s
  [board_string, active, castling, en_passant, ops].compact.reject(&:empty?).join(' ')
end