class Robot::Simulator::UI::CLI
Public Instance Methods
start_game()
click to toggle source
# File lib/robot/simulator/ui/cli.rb, line 11 def start_game @controller = Controller.new if options[:file] commands = read_commands_from_file options[:file] else commands = read_commands_from_console end commands.each {|command| dispatch_command command } end
Private Instance Methods
dispatch_command(command)
click to toggle source
# File lib/robot/simulator/ui/cli.rb, line 47 def dispatch_command(command) action, param = command.split(/\s+/) say "Will execute command: #{command}", :cyan begin case action.upcase when 'PLACE' execute_place_command action, param when 'MOVE' @controller.move when 'LEFT' @controller.left when 'RIGHT' @controller.right when 'REPORT' report = @controller.report say report ? report.join(',') : 'Not on table' else error "Unknow command is ignored: #{command}" end rescue StandardError => err error err.message end end
execute_place_command(action, param)
click to toggle source
# File lib/robot/simulator/ui/cli.rb, line 71 def execute_place_command(action, param) if param x, y, facing = param.split(/\s*\,\s*/) if x and y and facing @controller.place x.to_i, y.to_i, facing else error "Command PLACE needs 3 parameters in format of x,y,facing, got #{param}" end else error "Command PLACE is ignored because of lack parameters" end end
read_commands_from_console()
click to toggle source
# File lib/robot/simulator/ui/cli.rb, line 26 def read_commands_from_console say "Please input robot commands, end with a blank line ...", :green commands = [] loop do command = ask('>').strip break if command.empty? commands << command end commands end
read_commands_from_file(file_name)
click to toggle source
# File lib/robot/simulator/ui/cli.rb, line 37 def read_commands_from_file(file_name) say "Reading robot commands from file #{file_name} ...", :green commands = [] File.foreach(file_name) do |line| line = line.strip commands << line unless line.empty? end commands end