Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
b39456f
commit 6809289
Showing
5 changed files
with
87 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters