Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle user inputs and ensure that commands are valid. Update robot_spec
  • Loading branch information
SelenaSmall committed Aug 26, 2017
1 parent b39456f commit 6809289
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 31 deletions.
44 changes: 44 additions & 0 deletions lib/handle_input.rb
@@ -0,0 +1,44 @@
# HandleInput class
class HandleInput
attr_accessor :robot, :table

def initialize(robot, table)
@robot = robot
@table = table
end

# Interpret user Command
def interpret(command)
# Valid command patterns
# TODO: Add direction to place command
place = /^PLACE\s+\d+\s*,\s*\d+\s*$/
move = /^MOVE$/
left = /^LEFT$/
right = /^RIGHT$/
report = /^REPORT$/

if place.match?(command)
command, x, y = command.tr(',', ' ').split

position = @robot.place(x.to_i, y.to_i)

# Only update robot if the position is valid on the table
robot.update_robot(position) if @table.valid_position?(position.x, position.y)

puts "CMD: #{command} #{position.x},#{position.y}"

return "#{command} #{position.x},#{position.y}" unless robot.not_in_place?
end

# PLACE is the only valid command unless robot is in place
return if robot.not_in_place?

puts 'moving' if move.match?(command)

puts 'turn left' if left.match?(command)

puts 'turn right' if right.match?(command)

puts 'reporting' if report.match?(command)
end
end
10 changes: 2 additions & 8 deletions lib/robot.rb
Expand Up @@ -9,14 +9,8 @@ def intialize
end

# Place the robot
def place(command)
place = /^PLACE\s+\d+\s*,\s*\d+\s*$/

if place.match?(command)
command, x, y = command.tr(',', ' ').split

Position.new(x.to_i, y.to_i)
end
def place(x, y)
Position.new(x, y)
end

# Update the robot's position
Expand Down
36 changes: 36 additions & 0 deletions spec/handle_input_spec.rb
@@ -0,0 +1,36 @@
require 'rspec'
require 'spec_helper'
require './lib/handle_input'

describe HandleInput do
describe '#initialize' do
it 'should have a robot attribute which is nil' do
instance = HandleInput.new(@robot, @table)

expect(instance.robot).to be_nil
end

it 'should have a table attribute which is nil' do
instance = HandleInput.new(@robot, @table)

expect(instance.table).to be_nil
end
end

describe '#interpret' do
it 'should return a string if command matches place pattern' do
instance = HandleInput.new(Robot.new, Table.new)
command = 'PLACE 1,2'

expect(instance.interpret(command)).to be_a String
expect(instance.interpret(command)).to match(/^PLACE\s+\d+\s*,\s*\d+\s*$/)
end

it 'should return nil if command does not match a valid pattern' do
instance = HandleInput.new(Robot.new, Table.new)
command = 'hello'

expect(instance.interpret(command)).to be_nil
end
end
end
17 changes: 1 addition & 16 deletions spec/robot_spec.rb
Expand Up @@ -15,22 +15,7 @@
it 'should return an instance of Position' do
instance = Robot.new
command = 'PLACE 1,2'

expect(instance.place(command)).to be_a Position
end

it 'should return nil if command is empty' do
instance = Robot.new
command = ''

expect(instance.place(command)).to be_nil
end

it 'should return nil if command does not match the place pattern' do
instance = Robot.new
command = 'get'

expect(instance.place(command)).to be_nil
expect(instance.place(0, 0)).to be_a Position
end
end

Expand Down
11 changes: 4 additions & 7 deletions toy_robot.rb
Expand Up @@ -2,6 +2,7 @@

require_relative './lib/table'
require_relative './lib/robot'
require_relative './lib/handle_input'

table = Table.new
robot = Robot.new
Expand All @@ -11,14 +12,10 @@
puts 'Select your command'
input = gets.chomp

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

puts "Position: #{position.x},#{position.y}"

puts 'placed' unless robot.not_in_place?
command.interpret(input)
next
end

Expand Down

0 comments on commit 6809289

Please sign in to comment.