Class: PGN::EPD
- Inherits:
-
Object
- Object
- PGN::EPD
- 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.
Instance Attribute Summary collapse
-
#active ⇒ Object
Returns the value of attribute active.
-
#board ⇒ Object
Returns the value of attribute board.
-
#castling ⇒ Object
Returns the value of attribute castling.
-
#en_passant ⇒ Object
Returns the value of attribute en_passant.
-
#ops ⇒ Object
Returns the value of attribute ops.
Instance Method Summary collapse
-
#board_string ⇒ String
The EPD board-string portion.
- #board_string=(board_fen) ⇒ Object
-
#initialize(epd_string = nil) ⇒ EPD
constructor
A new instance of EPD.
- #inspect ⇒ Object
-
#to_position ⇒ PGN::Position
A Position for this EPD.
-
#to_s ⇒ String
The EPD string (four fields, then
opsif present).
Constructor Details
#initialize(epd_string = nil) ⇒ EPD
Returns a new instance of EPD.
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
#active ⇒ Object
Returns the value of attribute active.
17 18 19 |
# File 'lib/pgn/epd.rb', line 17 def active @active end |
#board ⇒ Object
Returns the value of attribute board.
17 18 19 |
# File 'lib/pgn/epd.rb', line 17 def board @board end |
#castling ⇒ Object
Returns the value of attribute castling.
18 19 20 |
# File 'lib/pgn/epd.rb', line 18 def castling @castling end |
#en_passant ⇒ Object
Returns the value of attribute en_passant.
18 19 20 |
# File 'lib/pgn/epd.rb', line 18 def en_passant @en_passant end |
#ops ⇒ Object
Returns the value of attribute ops.
17 18 19 |
# File 'lib/pgn/epd.rb', line 17 def ops @ops end |
Instance Method Details
#board_string ⇒ String
Returns 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
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 |
#inspect ⇒ Object
77 78 79 |
# File 'lib/pgn/epd.rb', line 77 def inspect to_s end |
#to_position ⇒ PGN::Position
Returns 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_s ⇒ String
Returns 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 |