Skip to content

Commit 6809289

Browse files
committed
Handle user inputs and ensure that commands are valid. Update robot_spec
1 parent b39456f commit 6809289

File tree

5 files changed

+87
-31
lines changed

5 files changed

+87
-31
lines changed

lib/handle_input.rb

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# HandleInput class
2+
class HandleInput
3+
attr_accessor :robot, :table
4+
5+
def initialize(robot, table)
6+
@robot = robot
7+
@table = table
8+
end
9+
10+
# Interpret user Command
11+
def interpret(command)
12+
# Valid command patterns
13+
# TODO: Add direction to place command
14+
place = /^PLACE\s+\d+\s*,\s*\d+\s*$/
15+
move = /^MOVE$/
16+
left = /^LEFT$/
17+
right = /^RIGHT$/
18+
report = /^REPORT$/
19+
20+
if place.match?(command)
21+
command, x, y = command.tr(',', ' ').split
22+
23+
position = @robot.place(x.to_i, y.to_i)
24+
25+
# Only update robot if the position is valid on the table
26+
robot.update_robot(position) if @table.valid_position?(position.x, position.y)
27+
28+
puts "CMD: #{command} #{position.x},#{position.y}"
29+
30+
return "#{command} #{position.x},#{position.y}" unless robot.not_in_place?
31+
end
32+
33+
# PLACE is the only valid command unless robot is in place
34+
return if robot.not_in_place?
35+
36+
puts 'moving' if move.match?(command)
37+
38+
puts 'turn left' if left.match?(command)
39+
40+
puts 'turn right' if right.match?(command)
41+
42+
puts 'reporting' if report.match?(command)
43+
end
44+
end

lib/robot.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,8 @@ def intialize
99
end
1010

1111
# Place the robot
12-
def place(command)
13-
place = /^PLACE\s+\d+\s*,\s*\d+\s*$/
14-
15-
if place.match?(command)
16-
command, x, y = command.tr(',', ' ').split
17-
18-
Position.new(x.to_i, y.to_i)
19-
end
12+
def place(x, y)
13+
Position.new(x, y)
2014
end
2115

2216
# Update the robot's position

spec/handle_input_spec.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'rspec'
2+
require 'spec_helper'
3+
require './lib/handle_input'
4+
5+
describe HandleInput do
6+
describe '#initialize' do
7+
it 'should have a robot attribute which is nil' do
8+
instance = HandleInput.new(@robot, @table)
9+
10+
expect(instance.robot).to be_nil
11+
end
12+
13+
it 'should have a table attribute which is nil' do
14+
instance = HandleInput.new(@robot, @table)
15+
16+
expect(instance.table).to be_nil
17+
end
18+
end
19+
20+
describe '#interpret' do
21+
it 'should return a string if command matches place pattern' do
22+
instance = HandleInput.new(Robot.new, Table.new)
23+
command = 'PLACE 1,2'
24+
25+
expect(instance.interpret(command)).to be_a String
26+
expect(instance.interpret(command)).to match(/^PLACE\s+\d+\s*,\s*\d+\s*$/)
27+
end
28+
29+
it 'should return nil if command does not match a valid pattern' do
30+
instance = HandleInput.new(Robot.new, Table.new)
31+
command = 'hello'
32+
33+
expect(instance.interpret(command)).to be_nil
34+
end
35+
end
36+
end

spec/robot_spec.rb

+1-16
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,7 @@
1515
it 'should return an instance of Position' do
1616
instance = Robot.new
1717
command = 'PLACE 1,2'
18-
19-
expect(instance.place(command)).to be_a Position
20-
end
21-
22-
it 'should return nil if command is empty' do
23-
instance = Robot.new
24-
command = ''
25-
26-
expect(instance.place(command)).to be_nil
27-
end
28-
29-
it 'should return nil if command does not match the place pattern' do
30-
instance = Robot.new
31-
command = 'get'
32-
33-
expect(instance.place(command)).to be_nil
18+
expect(instance.place(0, 0)).to be_a Position
3419
end
3520
end
3621

toy_robot.rb

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative './lib/table'
44
require_relative './lib/robot'
5+
require_relative './lib/handle_input'
56

67
table = Table.new
78
robot = Robot.new
@@ -11,14 +12,10 @@
1112
puts 'Select your command'
1213
input = gets.chomp
1314

14-
unless input == 'EXIT'
15-
position = robot.place(input)
16-
# Only update robot if the position is valid on the table
17-
robot.update_robot(position) if table.valid_position?(position.x, position.y)
15+
unless /^EXIT$/.match?(input)
16+
command = HandleInput.new(robot, table)
1817

19-
puts "Position: #{position.x},#{position.y}"
20-
21-
puts 'placed' unless robot.not_in_place?
18+
command.interpret(input)
2219
next
2320
end
2421

0 commit comments

Comments
 (0)