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
9 10 11 12 13 |
# File 'lib/chess_openings/opening.rb', line 9 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
7 8 9 |
# File 'lib/chess_openings/opening.rb', line 7 def eco_code @eco_code end |
#moves ⇒ Object
Returns the value of attribute moves
7 8 9 |
# File 'lib/chess_openings/opening.rb', line 7 def moves @moves end |
#name ⇒ Object
Returns the value of attribute name
7 8 9 |
# File 'lib/chess_openings/opening.rb', line 7 def name @name end |
Instance Method Details
#==(other) ⇒ Boolean
Compares two Openings
33 34 35 |
# File 'lib/chess_openings/opening.rb', line 33 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
61 62 63 64 |
# File 'lib/chess_openings/opening.rb', line 61 def to_fen moves = ChessOpeningsHelper.moves_as_strings(@moves) PGN::Game.new(moves).fen_list.last end |
#to_h ⇒ Hash
Hash representation of Opening
25 26 27 |
# File 'lib/chess_openings/opening.rb', line 25 def to_h { 'name' => @name, 'eco_code' => @eco_code, 'moves' => @moves } end |
#to_pgn ⇒ String
Returns PGN formatted String of the Opening
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/chess_openings/opening.rb', line 40 def to_pgn result = "" index = 1 @moves.each do |move| if index.odd? result += "#{(index / 2.0).ceil}.#{move}" elsif index.even? && @moves.size != index result += " #{move} " else result += " #{move}" end index += 1 end return result end |
#to_s ⇒ String
String representation of Opening
18 19 20 |
# File 'lib/chess_openings/opening.rb', line 18 def to_s "#{@eco_code}: #{@name}\n#{@moves}" end |