Module: ToyRobotSim::Parser

Defined in:
lib/toy_robot_sim/parser.rb

Class Method Summary collapse

Class Method Details

.execute(robot, instruction) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/toy_robot_sim/parser.rb', line 21

def self.execute(robot, instruction)
  if instruction.start_with?('PLACE')
    place(robot, instruction)
  elsif instruction.start_with?('MOVE')
    robot.move
  elsif instruction.start_with?('LEFT')
    robot.left
  elsif instruction.start_with?('RIGHT')
    robot.right
  elsif instruction.start_with?('REPORT')
    puts robot.report
  else
    puts "Error: Robot does not know what to do with instruction '#{instruction.chomp}'"
  end
end

.parse(file_path) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/toy_robot_sim/parser.rb', line 3

def self.parse(file_path)
  if !File.file?(file_path)
    puts "Error: File not found (#{file_path})"
  elsif File.extname(file_path) != ".txt"
    puts "Error: File not .txt (#{file_path})"
  else

    file = File.open(file_path)
    puts "Parsing #{file.path}..."
    table = ToyRobotSim::Table.new(5,5)
    robot = ToyRobotSim::Robot.new(table)

    file.each do |instruction|
      execute(robot,instruction)
    end
  end
end