Skip to content

Commit de39a04

Browse files
committed
Turn left when user inputs the left command. Update robots position.
1 parent ce8603d commit de39a04

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

lib/direction.rb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
# Direction
22
class Direction
3-
def initialize
4-
@options_array = %w[WEST NORTH EAST SOUTH]
5-
end
3+
OPTIONS = %w[WEST NORTH EAST SOUTH].freeze
64

75
def turn_right(direction)
8-
# Return if unless the direction exists
9-
return unless @options_array.include?(direction)
6+
# Return unless the direction exists
7+
return unless OPTIONS.include?(direction)
108

11-
i = @options_array.index { |e| e == direction }
9+
i = OPTIONS.index { |e| e == direction }
1210

1311
# Find next value in array
14-
unless direction == @options_array.last
12+
unless direction == OPTIONS.last
1513
i += 1
1614

17-
return @options_array.fetch(i)
15+
return OPTIONS.fetch(i)
16+
end
17+
18+
OPTIONS.first
19+
end
20+
21+
def turn_left(direction)
22+
# Return if unless the direction exists
23+
return unless OPTIONS.include?(direction)
24+
25+
i = OPTIONS.index { |e| e == direction }
26+
27+
# Find next value in array
28+
unless direction == OPTIONS.first
29+
i -= 1
30+
31+
return OPTIONS.fetch(i)
1832
end
1933

20-
@options_array.first
34+
OPTIONS.last
2135
end
2236
end

lib/handle_input.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def interpret(command)
2020
right = /^RIGHT$/
2121
report = /^REPORT$/
2222

23+
# PLACE
2324
if place.match?(command)
2425
command, x, y, f = command.tr(',', ' ').split
2526

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

40-
puts 'moving' if move.match?(command)
41+
# LEFT
42+
if left.match?(command)
43+
# Turn left and update the robots direction
44+
robot.position.f = Direction.new.turn_left(robot.position.f)
4145

42-
puts 'turn left' if left.match?(command)
46+
robot.update_robot(robot.position)
4347

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

61+
puts 'moving' if move.match?(command)
62+
5363
puts 'reporting' if report.match?(command)
5464
end
5565
end

spec/direction_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
describe Direction do
66
describe '#turn_right' do
7-
it 'should return the next value in the directions array' do
7+
it 'should return the next value in the OPTIONS array' do
88
instance = Direction.new
99
direction = 'NORTH'
1010

@@ -19,4 +19,21 @@
1919
expect(instance.turn_right(direction)).to be_nil
2020
end
2121
end
22+
23+
describe '#turn_left' do
24+
it 'should return the previous value in the OPTIONS array' do
25+
instance = Direction.new
26+
direction = 'NORTH'
27+
28+
expect(instance.turn_left(direction)).to be_a String
29+
expect(instance.turn_left(direction)).to eq 'WEST'
30+
end
31+
32+
it 'should return nil if the direction is empty' do
33+
instance = Direction.new
34+
direction = ''
35+
36+
expect(instance.turn_left(direction)).to be_nil
37+
end
38+
end
2239
end

0 commit comments

Comments
 (0)