Class: Opening
- Inherits:
-
Object
- Object
- Opening
- Defined in:
- lib/chess_openings/opening.rb
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) ⇒ Object
-
#initialize(name, eco_code, moves) ⇒ Opening
constructor
A new instance of Opening.
- #to_h ⇒ Object
- #to_pgn ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, eco_code, moves) ⇒ Opening
Returns a new instance of Opening.
5 6 7 8 9 |
# File 'lib/chess_openings/opening.rb', line 5 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.
3 4 5 |
# File 'lib/chess_openings/opening.rb', line 3 def eco_code @eco_code end |
#moves ⇒ Object
Returns the value of attribute moves.
3 4 5 |
# File 'lib/chess_openings/opening.rb', line 3 def moves @moves end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/chess_openings/opening.rb', line 3 def name @name end |
Instance Method Details
#==(other) ⇒ Object
19 20 21 |
# File 'lib/chess_openings/opening.rb', line 19 def ==(other) @name == other.name && @eco_code == other.eco_code && @moves == other.moves end |
#to_h ⇒ Object
15 16 17 |
# File 'lib/chess_openings/opening.rb', line 15 def to_h { "name" => @name, "eco_code" => @eco_code, "moves" => @moves } end |
#to_pgn ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/chess_openings/opening.rb', line 23 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 ⇒ Object
11 12 13 |
# File 'lib/chess_openings/opening.rb', line 11 def to_s "#{@eco_code}: #{@name}\n#{@moves}" end |