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
-
#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.
-
#get_all ⇒ Array
Get all openings available.
-
#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
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
#list ⇒ Object
Returns the value of attribute list
10 11 12 |
# File 'lib/chess_openings.rb', line 10 def list @list end |
#tree ⇒ Object
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
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)
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
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
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_all ⇒ Array
Get all openings available
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
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
63 64 65 |
# File 'lib/chess_openings.rb', line 63 def with_name(name) @list.select { |op| op.name.downcase.include?(name.downcase) } end |