class Opening

Class that represents a chess Opening

Attributes

eco_code[RW]
moves[RW]
name[RW]

Public Class Methods

new(name, eco_code, moves) click to toggle source
# File lib/chess_openings/opening.rb, line 8
def initialize(name, eco_code, moves)
  @name = name
  @eco_code = eco_code
  @moves = moves.map! { |move| move.is_a?(String) ? move.to_sym : move }
end

Public Instance Methods

==(other) click to toggle source

Compares two Openings

@param [Opening] other Opening to be compared @return [Boolean] True if both Openings have same values, false otherwise

# File lib/chess_openings/opening.rb, line 32
def ==(other)
  @name == other.name && @eco_code == other.eco_code && @moves == other.moves
end
to_fen() click to toggle source

Returns FEN formatted String representation of the Opening

@return [String] String that represents a game where Opening was used

# File lib/chess_openings/opening.rb, line 52
def to_fen
  moves = ChessOpeningsHelper.moves_as_strings(@moves)
  PGN::Game.new(moves).fen_list.last
end
to_h() click to toggle source

Hash representation of Opening

@return [Hash] Hash that represents an Opening

# File lib/chess_openings/opening.rb, line 24
def to_h
  { 'name' => @name, 'eco_code' => @eco_code, 'moves' => @moves }
end
to_pgn() click to toggle source

Returns PGN formatted String of the Opening

@return [String] String that represents a game where Opening was used

# File lib/chess_openings/opening.rb, line 39
def to_pgn
  result = ''
  index = 1
  @moves.each do |move|
    result += index.odd? ? "#{(index / 2.0).ceil}.#{move}" : " #{move} "
    index += 1
  end
  result.chop!
end
to_s() click to toggle source

String representation of Opening

@return [String] String that represents an Opening

# File lib/chess_openings/opening.rb, line 17
def to_s
  "#{@eco_code}: #{@name}\n#{@moves}"
end