Skip to content
Permalink
Browse files

Turn left when user inputs the left command. Update robots position.

  • Loading branch information...
SelenaSmall committed Aug 26, 2017
1 parent ce8603d commit de39a04f72f4c9a6864b484ce92fca8072361fe7
Showing with 53 additions and 12 deletions.
  1. +23 −9 lib/direction.rb
  2. +12 −2 lib/handle_input.rb
  3. +18 −1 spec/direction_spec.rb
@@ -1,22 +1,36 @@
# Direction
class Direction
def initialize
@options_array = %w[WEST NORTH EAST SOUTH]
end
OPTIONS = %w[WEST NORTH EAST SOUTH].freeze

def turn_right(direction)
# Return if unless the direction exists
return unless @options_array.include?(direction)
# Return unless the direction exists
return unless OPTIONS.include?(direction)

i = @options_array.index { |e| e == direction }
i = OPTIONS.index { |e| e == direction }

# Find next value in array
unless direction == @options_array.last
unless direction == OPTIONS.last
i += 1

return @options_array.fetch(i)
return OPTIONS.fetch(i)
end

OPTIONS.first
end

def turn_left(direction)
# Return if unless the direction exists
return unless OPTIONS.include?(direction)

i = OPTIONS.index { |e| e == direction }

# Find next value in array
unless direction == OPTIONS.first
i -= 1

return OPTIONS.fetch(i)
end

@options_array.first
OPTIONS.last
end
end
@@ -20,6 +20,7 @@ def interpret(command)
right = /^RIGHT$/
report = /^REPORT$/

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

@@ -37,10 +38,17 @@ def interpret(command)
# PLACE is the only valid command unless robot is in place
return if robot.not_in_place?

puts 'moving' if move.match?(command)
# LEFT
if left.match?(command)
# Turn left and update the robots direction
robot.position.f = Direction.new.turn_left(robot.position.f)

puts 'turn left' if left.match?(command)
robot.update_robot(robot.position)

puts "New position #{robot.position.x},#{robot.position.y},#{robot.position.f}"
end

# RIGHT
if right.match?(command)
# Turn right and update the robots direction
robot.position.f = Direction.new.turn_right(robot.position.f)
@@ -50,6 +58,8 @@ def interpret(command)
puts "New position #{robot.position.x},#{robot.position.y},#{robot.position.f}"
end

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

puts 'reporting' if report.match?(command)
end
end
@@ -4,7 +4,7 @@

describe Direction do
describe '#turn_right' do
it 'should return the next value in the directions array' do
it 'should return the next value in the OPTIONS array' do
instance = Direction.new
direction = 'NORTH'

@@ -19,4 +19,21 @@
expect(instance.turn_right(direction)).to be_nil
end
end

describe '#turn_left' do
it 'should return the previous value in the OPTIONS array' do
instance = Direction.new
direction = 'NORTH'

expect(instance.turn_left(direction)).to be_a String
expect(instance.turn_left(direction)).to eq 'WEST'
end

it 'should return nil if the direction is empty' do
instance = Direction.new
direction = ''

expect(instance.turn_left(direction)).to be_nil
end
end
end

0 comments on commit de39a04

Please sign in to comment.
You can’t perform that action at this time.