Class: Opening
- Inherits:
-
Object
- Object
- Opening
- Defined in:
- lib/chess_openings/opening.rb
Overview
Class that represents a chess Opening
Instance Attribute Summary collapse
-
#eco_code ⇒ Object
Returns the value of attribute eco_code.
-
#moves ⇒ Object
Returns the value of attribute moves.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares two Openings.
-
#initialize(name, eco_code, moves) ⇒ Opening
constructor
A new instance of Opening.
-
#to_fen ⇒ String
Returns FEN formatted String representation of the Opening.
-
#to_h ⇒ Hash
Hash representation of Opening.
-
#to_pgn ⇒ String
Returns PGN formatted String of the Opening.
-
#to_s ⇒ String
String representation of Opening.
Constructor Details
#initialize(name, eco_code, moves) ⇒ Opening
Returns a new instance of Opening.
8 9 10 11 12 |
# 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 |
Instance Attribute Details
#eco_code ⇒ Object
Returns the value of attribute eco_code.
6 7 8 |
# File 'lib/chess_openings/opening.rb', line 6 def eco_code @eco_code end |
#moves ⇒ Object
Returns the value of attribute moves.
6 7 8 |
# File 'lib/chess_openings/opening.rb', line 6 def moves @moves end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/chess_openings/opening.rb', line 6 def name @name end |
Instance Method Details
#==(other) ⇒ Boolean
Compares two Openings
32 33 34 |
# File 'lib/chess_openings/opening.rb', line 32 def ==(other) @name == other.name && @eco_code == other.eco_code && @moves == other.moves end |
#to_fen ⇒ String
Returns FEN formatted String representation of the Opening
52 53 54 55 |
# 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 ⇒ Hash
Hash representation of Opening
24 25 26 |
# File 'lib/chess_openings/opening.rb', line 24 def to_h { 'name' => @name, 'eco_code' => @eco_code, 'moves' => @moves } end |
#to_pgn ⇒ String
Returns PGN formatted String of the Opening
39 40 41 42 43 44 45 46 47 |
# 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 ⇒ String
String representation of Opening
17 18 19 |
# File 'lib/chess_openings/opening.rb', line 17 def to_s "#{@eco_code}: #{@name}\n#{@moves}" end |