Class: Opening

Inherits:
Object
  • Object
show all
Defined in:
lib/chess_openings/opening.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_codeObject

Returns the value of attribute eco_code.



3
4
5
# File 'lib/chess_openings/opening.rb', line 3

def eco_code
  @eco_code
end

#movesObject

Returns the value of attribute moves.



3
4
5
# File 'lib/chess_openings/opening.rb', line 3

def moves
  @moves
end

#nameObject

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_hObject



15
16
17
# File 'lib/chess_openings/opening.rb', line 15

def to_h
  { "name" => @name, "eco_code" => @eco_code, "moves" => @moves }
end

#to_pgnObject



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_sObject



11
12
13
# File 'lib/chess_openings/opening.rb', line 11

def to_s
  "#{@eco_code}: #{@name}\n#{@moves}"
end