Class: PGN::EPD

Inherits:
Object
  • Object
show all
Includes:
PositionFields
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

Methods included from PositionFields

#board_string, #board_string=, #castling=, #en_passant=

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).



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

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.



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

def active
  @active
end

#boardObject

Returns the value of attribute board.



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

def board
  @board
end

#castlingObject (readonly)

Returns the value of attribute castling.



20
21
22
# File 'lib/pgn/epd.rb', line 20

def castling
  @castling
end

#en_passantObject (readonly)

Returns the value of attribute en_passant.



20
21
22
# File 'lib/pgn/epd.rb', line 20

def en_passant
  @en_passant
end

#opsObject

Returns the value of attribute ops.



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

def ops
  @ops
end

Instance Method Details

#inspectObject



50
51
52
# File 'lib/pgn/epd.rb', line 50

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).



39
40
41
42
# File 'lib/pgn/epd.rb', line 39

def to_position
  player, castling_rights, ep = position_fields
  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)



46
47
48
# File 'lib/pgn/epd.rb', line 46

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