Class: Egd::FenBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/egd/fen_builder.rb

Constant Summary collapse

NULL_FEN =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_fen: nil, move: nil) ⇒ FenBuilder

Egd::FenBuilder.new(start_fen: nil, move:).call



11
12
13
14
# File 'lib/egd/fen_builder.rb', line 11

def initialize(start_fen: nil, move: nil)
  @start_fen = start_fen || NULL_FEN
  @move = move.to_s.gsub(%r'\A\d+\.\s*\.*\s*', "")
end

Instance Attribute Details

#moveObject (readonly)

This service takes in a FEN string and a chess move in algebraic notation. Outputs the FEN of the resulting position



6
7
8
# File 'lib/egd/fen_builder.rb', line 6

def move
  @move
end

#start_fenObject (readonly)

This service takes in a FEN string and a chess move in algebraic notation. Outputs the FEN of the resulting position



6
7
8
# File 'lib/egd/fen_builder.rb', line 6

def start_fen
  @start_fen
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
# File 'lib/egd/fen_builder.rb', line 16

def call
  @fen ||= (
    move != "" ?
      PGN::FEN.new(start_fen).to_position.move(move).to_fen.to_s :
      @start_fen
  )
end