class OpeningTree

Constants

DATA_FILE

Attributes

root[RW]
size[RW]

Public Class Methods

new() click to toggle source
# File lib/eco_classifier/opening_tree.rb, line 40
def initialize
  @root = Node.new
  @size = 0
end

Public Instance Methods

build_tree(openings) click to toggle source
# File lib/eco_classifier/opening_tree.rb, line 45
def build_tree(openings)
  openings.each do |opening|
    current = @root
    moves = opening.move_list
    while (move = moves.shift)
      if !current.children[move]
        current.children[move] = Node.new
      end
      current = current.children[move]
    end
    current.set_opening opening
    @size += 1
  end
end
generate!() click to toggle source
# File lib/eco_classifier/opening_tree.rb, line 60
def generate!
  openings = EcoDataFileParser.new(DATA_FILE).scan_into_openings
  build_tree(openings)
end
get_opening(move_list) click to toggle source
# File lib/eco_classifier/opening_tree.rb, line 79
def get_opening(move_list)
  search_for_opening(move_list)[:opening]
end
get_opening_from_pgn(pgn) click to toggle source
# File lib/eco_classifier/opening_tree.rb, line 83
def get_opening_from_pgn(pgn)
  get_opening pgn.gsub(/\d+\./, '').gsub(/\*/, '').strip.split(/\s+/)
end
inspect() click to toggle source
# File lib/eco_classifier/opening_tree.rb, line 87
def inspect
  %(<OpeningTree @size=#{@size}>)
end
search_for_opening(move_list) click to toggle source
# File lib/eco_classifier/opening_tree.rb, line 65
def search_for_opening(move_list)
  current = @root
  last_opening = nil
  while (move = move_list.shift)
    break unless current.children[move]
    current = current.children[move]
    last_opening = current.opening if current.opening
  end
  {
    opening:     last_opening,
    search_done: current.is_leaf? || move_list.length > 0
  }
end