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
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
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
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
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
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
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
Reads from the input stream.
# File lib/ai_games/parser.rb, line 67 def read_from_engine input.gets end
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
Writes to the output stream.
# File lib/ai_games/parser.rb, line 72 def write_to_engine(string) output.puts string output.flush end