Class: ChessOpenings

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

Overview

Class responsible for searching for chess Openings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChessOpenings

Returns a new instance of ChessOpenings.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/chess_openings.rb', line 11

def initialize
  @tree = SearchTree.new
  @list = []
  file = '/chess_openings/json_openings/openings.json'
  openings_file = File.join(File.dirname(__FILE__), file)
  openings = JSON.load(File.open(openings_file))['openings']
  openings.each do |op|
    opening = Opening.new(op['name'], op['eco_code'], op['moves'])
    @list << opening
    @tree.insert opening.moves, opening
  end
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



9
10
11
# File 'lib/chess_openings.rb', line 9

def list
  @list
end

#treeObject

Returns the value of attribute tree.



9
10
11
# File 'lib/chess_openings.rb', line 9

def tree
  @tree
end

Instance Method Details

#allArray

Get all openings available

Returns:

  • (Array)

    Array with all the Openings possible



27
28
29
# File 'lib/chess_openings.rb', line 27

def all
  @list.dup
end

#from_fen(fen_string) ⇒ Opening

Get Opening from FEN string

Parameters:

  • fen_string (String)

    FEN formatted String

Returns:

  • (Opening)

    Opening that was used in the game of the FEN String



92
93
94
95
96
97
# File 'lib/chess_openings.rb', line 92

def from_fen(fen_string)
  fen = PGN::FEN.new(fen_string)
  move_number = (fen.fullmove.to_i - 1) * 2
  final_list = @tree.get_moves_in_depth(move_number)
  final_list.select { |op| op.to_fen == fen_string }.first
end

#from_moves(moves) ⇒ Opening

Get Opening from moves (as symbols or strings)

Parameters:

  • moves (Array)

    Array with strings or symbols

Returns:

  • (Opening)

    Opening that has exactly the moves in the argument



35
36
37
# File 'lib/chess_openings.rb', line 35

def from_moves(moves)
  @tree.search moves
end

#from_pgn(file_name) ⇒ Opening

Get Opening from a PGN file

Parameters:

  • file_name (String)

    String with the path to the PGN file

Returns:

  • (Opening)

    Opening that was used in the PGN game

Raises:

  • (InvalidPGNError)

    Path points to invalid PGN file

  • (LoadError)

    Path points to non existent file



45
46
47
48
49
50
51
52
53
# File 'lib/chess_openings.rb', line 45

def from_pgn(file_name)
  raise LoadError, 'File does not exist' unless File.exist?(file_name)
  begin
    game = PGN.parse(File.read(file_name)).first
    @tree.search game.moves.map(&:notation)
  rescue
    raise InvalidPGNError, 'Invalid PGN file'
  end
end

#from_string(string) ⇒ Opening

Get Opening from a PGN formatted String

Parameters:

  • string (String)

    String in PGN format

Returns:

  • (Opening)

    Opening that was used in PGN formatted String



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/chess_openings.rb', line 75

def from_string(string)
  array = string.split
  moves = []
  array.each do |move|
    if move.include?('.')
      next if move.end_with?('.')
      move = move.split('.').last
    end
    moves << move
  end
  @tree.search moves
end

#that_start_with(moves) ⇒ Array

Search all Openings that start with the moves in the argument

Parameters:

  • moves (Array)

    Moves to be searched for

Returns:

  • (Array)

    Array with all Openings that start with the moves provided



67
68
69
# File 'lib/chess_openings.rb', line 67

def that_start_with(moves)
  @tree.search_all_with_moves moves
end

#with_name(name) ⇒ Array

Search Openings by name

Parameters:

  • name (String)

    String with the name to search for

Returns:

  • (Array)

    Array with all the Openings found



59
60
61
# File 'lib/chess_openings.rb', line 59

def with_name(name)
  @list.select { |op| op.name.downcase.include?(name.downcase) }
end