Class: Opening

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

Overview

Class that represents a chess Opening

Instance Attribute Summary collapse

Instance Method Summary collapse

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_codeObject

Returns the value of attribute eco_code.



6
7
8
# File 'lib/chess_openings/opening.rb', line 6

def eco_code
  @eco_code
end

#movesObject

Returns the value of attribute moves.



6
7
8
# File 'lib/chess_openings/opening.rb', line 6

def moves
  @moves
end

#nameObject

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

Parameters:

  • other (Opening)

    Opening to be compared

Returns:

  • (Boolean)

    True if both Openings have same values, false otherwise



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_fenString

Returns FEN formatted String representation of the Opening

Returns:

  • (String)

    String that represents a game where Opening was used



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_hHash

Hash representation of Opening

Returns:

  • (Hash)

    Hash that represents an 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_pgnString

Returns PGN formatted String of the Opening

Returns:

  • (String)

    String that represents a game where Opening was used



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_sString

String representation of Opening

Returns:

  • (String)

    String that represents an Opening



17
18
19
# File 'lib/chess_openings/opening.rb', line 17

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