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



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

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



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

def list
  @list
end

#treeObject

Returns the value of attribute tree



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

def tree
  @tree
end

Instance Method Details

#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



96
97
98
99
100
101
# File 'lib/chess_openings.rb', line 96

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



39
40
41
# File 'lib/chess_openings.rb', line 39

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



49
50
51
52
53
54
55
56
57
# File 'lib/chess_openings.rb', line 49

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



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chess_openings.rb', line 79

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

#get_allArray

Get all openings available

Returns:

  • (Array)

    Array with all the Openings possible



31
32
33
# File 'lib/chess_openings.rb', line 31

def get_all
  @list.dup
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



71
72
73
# File 'lib/chess_openings.rb', line 71

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



63
64
65
# File 'lib/chess_openings.rb', line 63

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