class ToyRobotSim::Cli::Application

Public Instance Methods

start() click to toggle source
# File lib/cli/application.rb, line 22
def start
  if options[:directory]
    directory(options[:directory])
  elsif options[:file]
    file(options[:file])
  elsif options[:interactive]
    interactive
  end
end

Private Instance Methods

directory(path) click to toggle source
# File lib/cli/application.rb, line 36
def directory(path)
  working_directory = Dir.pwd

  if Dir.exists?(path)
    Dir.chdir(path)

    puts "Parsing directory #{path}..."
    puts "-"*80

    Dir.glob("*.txt") do |file|
      ToyRobotSim::Parser.parse(file)
      puts "-"*80
    end

    Dir.chdir(working_directory)
  else
    puts "Error: #{path} is an invalid directory"
  end
end
file(path) click to toggle source
# File lib/cli/application.rb, line 56
def file(path)
  if File.file?(path) && File.extname(path) == ".txt"
    ToyRobotSim::Parser.parse(path)
  else
    puts "Error: #{path} is an invalid file"
  end
end
interactive() click to toggle source
# File lib/cli/application.rb, line 64
def interactive

  say("Create a table...")

  width  = ask("Width : ").to_i
  height = ask("Height: ").to_i

  while width <= 0 || height <= 0
    say ("Table width and height need to be integers greater than zero")
    width  = ask("Width : ").to_i
    height = ask("Height: ").to_i
  end

  table = ToyRobotSim::Table.new(width, height)
  robot = ToyRobotSim::Robot.new(table)

  say("Instruction List")
  say( ("-"*80) )
  say('PLACE X,Y,F | F can be NORTH, EAST, SOUTH, WEST')
  say('MOVE        | Move forward one step')
  say('RIGHT       | Rotate clockwise')
  say('LEFT        | Rotate anti-clockwise')
  say('REPORT      | Current location of the robot')
  say('END         | Stops the simulation')
  say( ("-"*80) )
  say("Input an instruction (non case sensitive)")
  instruction = ask("Instruction: ")

  while instruction.upcase != 'END'
    ToyRobotSim::Parser.execute(robot, instruction.upcase)
    instruction = ask("Instruction: ")
  end

  say("Toy Robot Simulation Ended", Thor::Shell::Color::GREEN)
end