Class: AIGames::FourInARow::Parser

Inherits:
Parser::AbstractParser
  • Object
show all
Defined in:
lib/ai_games/four_in_a_row/parser.rb

Overview

The parser handles the communication with the game engine. It sets up the game using the information provided by the engine before the game starts, and updates the match when new information comes in.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match, options = nil) ⇒ Parser

Initializes the parser with the given match instance.



33
34
35
36
# File 'lib/ai_games/four_in_a_row/parser.rb', line 33

def initialize(match, options = nil)
  super(options)
  @match = match
end

Instance Attribute Details

#matchObject (readonly)

The match instance



30
31
32
# File 'lib/ai_games/four_in_a_row/parser.rb', line 30

def match
  @match
end

Instance Method Details

#parse(command_array) ⇒ Object

Parses the game engine’s commands. This overwrites DefaultParser.parse with the specific instructions needed for the “Four In A Row” competition.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ai_games/four_in_a_row/parser.rb', line 44

def parse(command_array)
  case command_array[0]
  when 'action'
    parse_action command_array[2].to_i
  when 'update'
    parse_update command_array[2..-1]
    return nil
  when 'settings'
    parse_settings command_array[1..-1]
    return nil
  else
    Logger.warn('Parser.parse : Unknown command ' \
      "'#{command_array.join ' '}'")
  end
end