Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Turn right when user inputs the right command. Update robots position
  • Loading branch information
SelenaSmall committed Aug 26, 2017
1 parent 92324ac commit ce8603d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 46 deletions.
35 changes: 10 additions & 25 deletions lib/direction.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)
12 changes: 11 additions & 1 deletion lib/handle_input.rb
@@ -1,3 +1,6 @@
require_relative 'direction'
require_relative 'position'

# HandleInput class
class HandleInput
attr_accessor :robot, :table
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/position.rb
@@ -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
24 changes: 8 additions & 16 deletions spec/direction_spec.rb
Expand Up @@ -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
4 changes: 2 additions & 2 deletions spec/position_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit ce8603d

Please sign in to comment.