class RTanque::Runner

Runner manages running an {RTanque::Match}

Constants

BRAIN_PATH_PARSER
LoadError
ParsedBrainPath

@!visibility private

Attributes

match[R]

Public Class Methods

new(width, height, *match_args) click to toggle source

@param [Integer] width @param [Integer] height @param [*match_args] args provided to {RTanque::Match#initialize}

# File lib/rtanque/runner.rb, line 15
def initialize(width, height, *match_args)
  @match = RTanque::Match.new(RTanque::Arena.new(width, height), *match_args)
end

Public Instance Methods

add_brain_path(brain_path) click to toggle source

Attempts to load given {RTanque::Bot::Brain} given its path @param [String] brain_path @raise [RTanque::Runner::LoadError] if brain could not be loaded

# File lib/rtanque/runner.rb, line 22
def add_brain_path(brain_path)
  parsed_path = self.parse_brain_path(brain_path)
  bots = parsed_path.multiplier.times.map { self.new_bots_from_brain_path(parsed_path.path) }.flatten
  self.match.add_bots(bots)
end
start(gui = true) click to toggle source

Starts the match @param [Boolean] gui if false, runs headless match

# File lib/rtanque/runner.rb, line 30
def start(gui = true)
  if gui
    require 'rtanque/gui'
    window = RTanque::Gui::Window.new(self.match)
    trap(:INT) { window.close }
    window.show
  else
    trap(:INT) { self.match.stop }
    self.match.start
  end
end

Protected Instance Methods

fetch_brain_klasses(brain_path) click to toggle source
# File lib/rtanque/runner.rb, line 50
def fetch_brain_klasses(brain_path)
  @load_brain_klass_cache ||= Hash.new do |cache, path|
    cache[path] = self.get_diff_in_object_space(RTanque::Bot::Brain) {
      begin
        require(path)
      rescue ::LoadError => e
        raise LoadError, e.message
      end
    }
    raise LoadError, "No class of type #{RTanque::Bot::Brain} found in #{path}" if cache[path].empty?
    cache[path]
  end
  @load_brain_klass_cache[brain_path]
end
get_descendants_of_class(klass) click to toggle source
# File lib/rtanque/runner.rb, line 71
def get_descendants_of_class(klass)
  ::ObjectSpace.each_object(::Class).select {|k| k < klass }
end
get_diff_in_object_space(klass) { || ... } click to toggle source
# File lib/rtanque/runner.rb, line 65
def get_diff_in_object_space(klass)
  current_object_space = self.get_descendants_of_class(klass)
  yield
  self.get_descendants_of_class(klass) - current_object_space
end
new_bots_from_brain_path(brain_path) click to toggle source
# File lib/rtanque/runner.rb, line 44
def new_bots_from_brain_path(brain_path)
  self.fetch_brain_klasses(brain_path).map do |brain_klass|
    RTanque::Bot.new_random_location(self.match.arena, brain_klass)
  end
end
parse_brain_path(brain_path) click to toggle source
# File lib/rtanque/runner.rb, line 78
def parse_brain_path(brain_path)
  path = brain_path.gsub('\.rb$', '')
  multiplier = 1
  brain_path.match(BRAIN_PATH_PARSER) { |m|
    path = m[1]
    multiplier = m[2].to_i
  }
  ParsedBrainPath.new(path, multiplier)
end