class AiGames::Parser

This class provides an abstract parser that can be extended to implement the logic that is required for a specific competition. It provides methods to communicate with the game engine, but does NOT contain any logic how to parse the engine’s commands. This is totally up to the implementations of this class.

Constants

VERSION

Public Class Methods

new(options = nil) click to toggle source

Initializes the parser. Pass in options using a hash structure.

# File lib/ai_games/parser.rb, line 11
def initialize(options = nil)
  initialize_streams options if options
end

Public Instance Methods

parse(command_array) click to toggle source

Parses the given command array. This method MUST return a valid response string that can be send to the engine. rubocop:disable Lint/UnusedMethodArgument

# File lib/ai_games/parser.rb, line 39
def parse(command_array)
  fail 'Method must be overwritten'
end
run() click to toggle source

Starts the main loop. This loop runs until the game engine closes the console line. During the loop, the parser reads from the game engine, sanitizes the input a little bit, and then passes it to a method that needs to be overwritten by parsers extending this interface.

# File lib/ai_games/parser.rb, line 19
def run
  AiGames::Logger.info 'Parser.run : Starting loop'

  loop do
    command = read_from_engine
    break if command.nil?

    command.strip!
    next if command.length == 0

    response = parse split_line command
    write_to_engine response unless response.nil? || response.length < 1
  end

  AiGames::Logger.info 'Parser.run : Stopping loop'
end

Private Instance Methods

initialize_streams(options) click to toggle source

Initializes the input and output streams that the parser uses. The expected format for the options is a hash with the keys ‘:input` and `:output`. Use this to mock `STDIN` and `STDOUT` for tests.

# File lib/ai_games/parser.rb, line 49
def initialize_streams(options)
  @input = options[:input] if options.key? :input
  @output = options[:output] if options.key? :output
end
input() click to toggle source

Returns the input stream. If no stream has been configured, $stdin is returned.

# File lib/ai_games/parser.rb, line 56
def input
  @input ||= $stdin
end
output() click to toggle source

Returns the output stream. If no stream has been configured, $stdout is returned.

# File lib/ai_games/parser.rb, line 62
def output
  @output ||= $stdout
end
read_from_engine() click to toggle source

Reads from the input stream.

# File lib/ai_games/parser.rb, line 67
def read_from_engine
  input.gets
end
split_line(line) click to toggle source

Formats a command line for further processing. The input is split at the whitespace character, and all its parts are converted to downcase.

# File lib/ai_games/parser.rb, line 79
def split_line(line)
  line.split(' ').map(&:downcase)
end
write_to_engine(string) click to toggle source

Writes to the output stream.

# File lib/ai_games/parser.rb, line 72
def write_to_engine(string)
  output.puts string
  output.flush
end