Permalink
Browse files

Handle user inputs and ensure that commands are valid. Update robot_spec

  • Loading branch information...
SelenaSmall committed Aug 26, 2017
1 parent b39456f commit 6809289affa0c1966e7dcece90eec81aa0581286
Showing with 87 additions and 31 deletions.
  1. +44 −0 lib/handle_input.rb
  2. +2 −8 lib/robot.rb
  3. +36 −0 spec/handle_input_spec.rb
  4. +1 −16 spec/robot_spec.rb
  5. +4 −7 toy_robot.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
@@ -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
@@ -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
@@ -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
@@ -2,6 +2,7 @@
require_relative './lib/table'
require_relative './lib/robot'
require_relative './lib/handle_input'
table = Table.new
robot = Robot.new
@@ -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

0 comments on commit 6809289

Please sign in to comment.