Permalink
Browse files

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

  • Loading branch information...
SelenaSmall committed Aug 26, 2017
1 parent 92324ac commit ce8603d08969f874449279867628eafbeaa1756e
Showing with 33 additions and 46 deletions.
  1. +10 −25 lib/direction.rb
  2. +11 −1 lib/handle_input.rb
  3. +2 −2 lib/position.rb
  4. +8 −16 spec/direction_spec.rb
  5. +2 −2 spec/position_spec.rb
@@ -1,37 +1,22 @@
# Direction
class Direction
attr_reader :start_direction
def initialize(start_direction)
@start_direction = start_direction
@directions_array = %w[WEST NORTH EAST SOUTH]
def initialize
@options_array = %w[WEST NORTH EAST SOUTH]
end
def turn_right(current_direction)
puts 'turn right'
def turn_right(direction)
# Return if unless the direction exists
return unless @options_array.include?(direction)
# does the current direction exist?
return unless @directions_array.include?(current_direction)
i = @options_array.index { |e| e == direction }
i = @directions_array.index { |e| e == current_direction }
unless current_direction == @directions_array.last
# Find next value in array
unless direction == @options_array.last
i += 1
new_direction = @directions_array.fetch(i)
puts "New Direction: #{new_direction}"
return new_direction
return @options_array.fetch(i)
end
puts "New Direction: #{@directions_array.first}"
@options_array.first
end
end
direction = Direction.new('NORTH')
puts "Starting at #{direction.start_direction}"
current_direction = direction.start_direction
direction.turn_right(current_direction)
@@ -1,3 +1,6 @@
require_relative 'direction'
require_relative 'position'
# HandleInput class
class HandleInput
attr_accessor :robot, :table
@@ -38,7 +41,14 @@ def interpret(command)
puts 'turn left' if left.match?(command)
puts 'turn right' if right.match?(command)
if right.match?(command)
# Turn right and update the robots direction
robot.position.f = Direction.new.turn_right(robot.position.f)
robot.update_robot(robot.position)
puts "New position #{robot.position.x},#{robot.position.y},#{robot.position.f}"
end
puts 'reporting' if report.match?(command)
end
@@ -1,10 +1,10 @@
# Position class
class Position
attr_accessor :x, :y, :f
attr_accessor :x, :y, :f
def initialize(x, y, f)
@x = x
@y = y
@f = f
@f = f
end
end
@@ -3,28 +3,20 @@
require './lib/direction'
describe Direction do
describe '#initialize' do
it 'should have a start_direction attribute which is a String' do
instance = Direction.new('NORTH')
expect(instance.start_direction).to be_a String
end
end
describe '#turn_right' do
it 'should return the next value in the directions array' do
instance = Direction.new('NORTH')
current_direction = 'NORTH'
instance = Direction.new
direction = 'NORTH'
expect(instance.turn_right(current_direction)).to be_a String
expect(instance.turn_right(current_direction)).to eq 'EAST'
expect(instance.turn_right(direction)).to be_a String
expect(instance.turn_right(direction)).to eq 'EAST'
end
it 'should return nil if the current_direction is empty' do
instance = Direction.new('NORTH')
current_direction = ''
it 'should return nil if the direction is empty' do
instance = Direction.new
direction = ''
expect(instance.turn_right(current_direction)).to be_nil
expect(instance.turn_right(direction)).to be_nil
end
end
end
@@ -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, 'NORTH')
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, 'NORTH')
instance = Position.new(0, 0, 'NORTH')
expect(instance.y).to be_a Integer
expect(instance.y).to be >= 0

0 comments on commit ce8603d

Please sign in to comment.