-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
49 lines (42 loc) · 1.03 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require_relative 'lib/toy_robot'
unless input_file = ARGV[0]
help = %q(
Usage: ruby main.rb input_file
input_file: A path of a text file that contains commands for Toy Robot, one command per line.
Valid commands: PLACE X,Y,F
MOVE
LEFT
RIGHT
REPORT
)
print help
exit
end
VALID_COMMANDS = %w(PLACE MOVE LEFT RIGHT REPORT).freeze
commands = []
begin
File.foreach(input_file) do |line|
cmd, args = line.strip.split(' ')
next unless VALID_COMMANDS.include?(cmd)
commands << {
cmd: cmd.downcase.to_sym,
args: args.to_s.split(',')
}
end
rescue Errno::ENOENT => e
puts "Input file not found: #{input_file}"
exit
rescue => e
puts e.message
exit
end
robot = Robot.new(Table.new)
commands.each do |command|
if command[:cmd] == :place
args = command[:args]
robot.place(Position.new(args[0], args[1]), args[2])
next
end
result = robot.send(command[:cmd])
puts "Output: #{result}" if command[:cmd] == :report && result
end