Module: ToyRobotController

Extended by:
CommandParserHelper
Defined in:
lib/toy_robot_controller.rb

Overview

ToyRobotController module which sets up the Robot, Table and execute commands provided

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from CommandParserHelper

parse_command

Methods included from CommandHelper

#a_valid_place_command?, #command_type, #commands, #directions, #split_place_command

Class Attribute Details

.commandsObject

Returns the value of attribute commands.



11
12
13
# File 'lib/toy_robot_controller.rb', line 11

def commands
  @commands
end

.robotObject

Returns the value of attribute robot.



11
12
13
# File 'lib/toy_robot_controller.rb', line 11

def robot
  @robot
end

Class Method Details

.execute_commandsObject

Simply executes the commands array.

considerations:

Raises:



39
40
41
42
43
44
# File 'lib/toy_robot_controller.rb', line 39

def execute_commands
  raise Command::NoValidCommandsFound if @commands.size.zero?
  raise Command::PlaceCommandNotFound if @commands.select { |c| c.type == 'PLACE' }.size.zero?

  @commands.each { |command| send_command_to_robot(command) }
end

.init(commands_stream, table_size = 5) ⇒ Object

This method is expecting [Array<String>] of commands such as [‘PLACE 0,0,NORTH’, ‘MOVE’, ‘REPORT’]

Parameters:

  • commands_stream (Array<String>)
  • table_size (Integer) (defaults to: 5)


17
18
19
20
21
22
23
24
25
# File 'lib/toy_robot_controller.rb', line 17

def init(commands_stream, table_size = 5)
  validate_command_stream(commands_stream)

  @commands_stream = commands_stream
  @table = Surface::Table.new(table_size, table_size)
  @robot = ToyRobot::Robot.new(@table)
  @commands = []
  populate_commands
end

.report(format = 'console') ⇒ Object

This method return the coordinates of Robot on the table and the direction it is facing



28
29
30
# File 'lib/toy_robot_controller.rb', line 28

def report(format = 'console')
  @robot.report(format)
end