Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Add direction to Place object
- Loading branch information
|
@@ -11,24 +11,25 @@ def initialize(robot, table) |
|
|
def interpret(command) |
|
|
# Valid command patterns |
|
|
# TODO: Add direction to place command |
|
|
place = /^PLACE\s+\d+\s*,\s*\d+\s*$/ |
|
|
place = /^PLACE\s+\d+\s*,\s*\d+\s*,\s*(WEST||NORTH||EAST||SOUTH)$/ |
|
|
move = /^MOVE$/ |
|
|
left = /^LEFT$/ |
|
|
right = /^RIGHT$/ |
|
|
report = /^REPORT$/ |
|
|
|
|
|
if place.match?(command) |
|
|
command, x, y = command.tr(',', ' ').split |
|
|
command, x, y, f = command.tr(',', ' ').split |
|
|
|
|
|
position = @robot.place(x.to_i, y.to_i) |
|
|
position = @robot.place(x.to_i, y.to_i, f) |
|
|
|
|
|
# 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}" |
|
|
puts "CMD: #{command} #{position.x},#{position.y},#{position.f}" |
|
|
|
|
|
return "#{command} #{position.x},#{position.y}" unless robot.not_in_place? |
|
|
end |
|
|
puts 'placed!' unless robot.not_in_place? |
|
|
return "#{command} #{position.x},#{position.y},#{position.f}" unless robot.not_in_place? |
|
|
end |
|
|
|
|
|
# PLACE is the only valid command unless robot is in place |
|
|
return if robot.not_in_place? |
|
|
|
|
@@ -1,9 +1,10 @@ |
|
|
# Position class |
|
|
class Position |
|
|
attr_accessor :x, :y |
|
|
attr_accessor :x, :y, :f |
|
|
|
|
|
def initialize(x, y) |
|
|
def initialize(x, y, f) |
|
|
@x = x |
|
|
@y = y |
|
|
@f = f |
|
|
end |
|
|
end |
|
@@ -9,8 +9,8 @@ def intialize |
|
|
end |
|
|
|
|
|
# Place the robot |
|
|
def place(x, y) |
|
|
Position.new(x, y) |
|
|
def place(x, y, f) |
|
|
Position.new(x, y, f) |
|
|
end |
|
|
|
|
|
# Update the robot's position |
|
|
|
@@ -20,10 +20,10 @@ |
|
|
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' |
|
|
command = 'PLACE 1,2,NORTH' |
|
|
|
|
|
expect(instance.interpret(command)).to be_a String |
|
|
expect(instance.interpret(command)).to match(/^PLACE\s+\d+\s*,\s*\d+\s*$/) |
|
|
expect(instance.interpret(command)).to match(/^PLACE\s+\d+\s*,\s*\d+\s*,\s*(WEST||NORTH||EAST||SOUTH)$/) |
|
|
end |
|
|
|
|
|
it 'should return nil if command does not match a valid pattern' do |
|
|
|
@@ -5,15 +5,15 @@ |
|
|
describe Position do |
|
|
describe '#initialize' do |
|
|
it 'should have an x attribute which is an integer' do |
|
|
instance = Position.new(0, 0) |
|
|
instance = Position.new(0, 0, 'NORTH') |
|
|
|
|
|
expect(instance.x).to be_a Integer |
|
|
expect(instance.x).to be >= 0 |
|
|
expect(instance.x).to be <= 4 |
|
|
end |
|
|
|
|
|
it 'should have a y attribute which is an integer' do |
|
|
instance = Position.new(0, 0) |
|
|
instance = Position.new(0, 0, 'NORTH') |
|
|
|
|
|
expect(instance.y).to be_a Integer |
|
|
expect(instance.y).to be >= 0 |
|
|
|
@@ -14,15 +14,15 @@ |
|
|
describe '#place' do |
|
|
it 'should return an instance of Position' do |
|
|
instance = Robot.new |
|
|
command = 'PLACE 1,2' |
|
|
expect(instance.place(0, 0)).to be_a Position |
|
|
|
|
|
expect(instance.place(0, 0, 'NORTH')).to be_a Position |
|
|
end |
|
|
end |
|
|
|
|
|
describe '#update_robot' do |
|
|
it 'should return an instance of Position' do |
|
|
instance = Robot.new |
|
|
new_position = Position.new(0, 1) |
|
|
new_position = Position.new(0, 1, 'NORTH') |
|
|
|
|
|
expect(instance.update_robot(new_position)).to be_a Position |
|
|
end |
|
|