Class: ChessOpenings
- Inherits:
-
Object
- Object
- ChessOpenings
- Defined in:
- lib/chess_openings.rb
Overview
Class responsible for searching for chess Openings
Instance Attribute Summary collapse
-
#list ⇒ Object
Returns the value of attribute list.
-
#tree ⇒ Object
Returns the value of attribute tree.
Instance Method Summary collapse
-
#all ⇒ Array
Get all openings available.
-
#from_fen(fen_string) ⇒ Opening
Get Opening from FEN string.
-
#from_moves(moves) ⇒ Opening
Get Opening from moves (as symbols or strings).
-
#from_pgn(file_name) ⇒ Opening
Get Opening from a PGN file.
-
#from_string(string) ⇒ Opening
Get Opening from a PGN formatted String.
-
#initialize ⇒ ChessOpenings
constructor
A new instance of ChessOpenings.
-
#that_start_with(moves) ⇒ Array
Search all Openings that start with the moves in the argument.
-
#with_name(name) ⇒ Array
Search Openings by name.
Constructor Details
#initialize ⇒ ChessOpenings
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
#list ⇒ Object
Returns the value of attribute list.
9 10 11 |
# File 'lib/chess_openings.rb', line 9 def list @list end |
#tree ⇒ Object
Returns the value of attribute tree.
9 10 11 |
# File 'lib/chess_openings.rb', line 9 def tree @tree end |
Instance Method Details
#all ⇒ Array
Get all openings available
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
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)
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
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
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
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
59 60 61 |
# File 'lib/chess_openings.rb', line 59 def with_name(name) @list.select { |op| op.name.downcase.include?(name.downcase) } end |