Skip to content

Commit ce8603d

Browse files
committed
Turn right when user inputs the right command. Update robots position
1 parent 92324ac commit ce8603d

File tree

5 files changed

+33
-46
lines changed

5 files changed

+33
-46
lines changed

lib/direction.rb

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
11
# Direction
22
class Direction
3-
attr_reader :start_direction
4-
5-
def initialize(start_direction)
6-
@start_direction = start_direction
7-
8-
@directions_array = %w[WEST NORTH EAST SOUTH]
3+
def initialize
4+
@options_array = %w[WEST NORTH EAST SOUTH]
95
end
106

11-
def turn_right(current_direction)
12-
puts 'turn right'
7+
def turn_right(direction)
8+
# Return if unless the direction exists
9+
return unless @options_array.include?(direction)
1310

14-
# does the current direction exist?
15-
return unless @directions_array.include?(current_direction)
11+
i = @options_array.index { |e| e == direction }
1612

17-
i = @directions_array.index { |e| e == current_direction }
18-
19-
unless current_direction == @directions_array.last
13+
# Find next value in array
14+
unless direction == @options_array.last
2015
i += 1
2116

22-
new_direction = @directions_array.fetch(i)
23-
puts "New Direction: #{new_direction}"
24-
25-
return new_direction
17+
return @options_array.fetch(i)
2618
end
2719

28-
puts "New Direction: #{@directions_array.first}"
20+
@options_array.first
2921
end
3022
end
31-
32-
direction = Direction.new('NORTH')
33-
34-
puts "Starting at #{direction.start_direction}"
35-
current_direction = direction.start_direction
36-
37-
direction.turn_right(current_direction)

lib/handle_input.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require_relative 'direction'
2+
require_relative 'position'
3+
14
# HandleInput class
25
class HandleInput
36
attr_accessor :robot, :table
@@ -38,7 +41,14 @@ def interpret(command)
3841

3942
puts 'turn left' if left.match?(command)
4043

41-
puts 'turn right' if right.match?(command)
44+
if right.match?(command)
45+
# Turn right and update the robots direction
46+
robot.position.f = Direction.new.turn_right(robot.position.f)
47+
48+
robot.update_robot(robot.position)
49+
50+
puts "New position #{robot.position.x},#{robot.position.y},#{robot.position.f}"
51+
end
4252

4353
puts 'reporting' if report.match?(command)
4454
end

lib/position.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Position class
22
class Position
3-
attr_accessor :x, :y, :f
3+
attr_accessor :x, :y, :f
44

55
def initialize(x, y, f)
66
@x = x
77
@y = y
8-
@f = f
8+
@f = f
99
end
1010
end

spec/direction_spec.rb

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,20 @@
33
require './lib/direction'
44

55
describe Direction do
6-
describe '#initialize' do
7-
it 'should have a start_direction attribute which is a String' do
8-
instance = Direction.new('NORTH')
9-
10-
expect(instance.start_direction).to be_a String
11-
end
12-
end
13-
146
describe '#turn_right' do
157
it 'should return the next value in the directions array' do
16-
instance = Direction.new('NORTH')
17-
current_direction = 'NORTH'
8+
instance = Direction.new
9+
direction = 'NORTH'
1810

19-
expect(instance.turn_right(current_direction)).to be_a String
20-
expect(instance.turn_right(current_direction)).to eq 'EAST'
11+
expect(instance.turn_right(direction)).to be_a String
12+
expect(instance.turn_right(direction)).to eq 'EAST'
2113
end
2214

23-
it 'should return nil if the current_direction is empty' do
24-
instance = Direction.new('NORTH')
25-
current_direction = ''
15+
it 'should return nil if the direction is empty' do
16+
instance = Direction.new
17+
direction = ''
2618

27-
expect(instance.turn_right(current_direction)).to be_nil
19+
expect(instance.turn_right(direction)).to be_nil
2820
end
2921
end
3022
end

spec/position_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
describe Position do
66
describe '#initialize' do
77
it 'should have an x attribute which is an integer' do
8-
instance = Position.new(0, 0, 'NORTH')
8+
instance = Position.new(0, 0, 'NORTH')
99

1010
expect(instance.x).to be_a Integer
1111
expect(instance.x).to be >= 0
1212
expect(instance.x).to be <= 4
1313
end
1414

1515
it 'should have a y attribute which is an integer' do
16-
instance = Position.new(0, 0, 'NORTH')
16+
instance = Position.new(0, 0, 'NORTH')
1717

1818
expect(instance.y).to be_a Integer
1919
expect(instance.y).to be >= 0

0 commit comments

Comments
 (0)