File tree Expand file tree Collapse file tree 5 files changed +33
-46
lines changed Expand file tree Collapse file tree 5 files changed +33
-46
lines changed Original file line number Diff line number Diff line change 1
1
# Direction
2
2
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 ]
9
5
end
10
6
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 )
13
10
14
- # does the current direction exist?
15
- return unless @directions_array . include? ( current_direction )
11
+ i = @options_array . index { |e | e == direction }
16
12
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
20
15
i += 1
21
16
22
- new_direction = @directions_array . fetch ( i )
23
- puts "New Direction: #{ new_direction } "
24
-
25
- return new_direction
17
+ return @options_array . fetch ( i )
26
18
end
27
19
28
- puts "New Direction: #{ @directions_array . first } "
20
+ @options_array . first
29
21
end
30
22
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 )
Original file line number Diff line number Diff line change
1
+ require_relative 'direction'
2
+ require_relative 'position'
3
+
1
4
# HandleInput class
2
5
class HandleInput
3
6
attr_accessor :robot , :table
@@ -38,7 +41,14 @@ def interpret(command)
38
41
39
42
puts 'turn left' if left . match? ( command )
40
43
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
42
52
43
53
puts 'reporting' if report . match? ( command )
44
54
end
Original file line number Diff line number Diff line change 1
1
# Position class
2
2
class Position
3
- attr_accessor :x , :y , :f
3
+ attr_accessor :x , :y , :f
4
4
5
5
def initialize ( x , y , f )
6
6
@x = x
7
7
@y = y
8
- @f = f
8
+ @f = f
9
9
end
10
10
end
Original file line number Diff line number Diff line change 3
3
require './lib/direction'
4
4
5
5
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
-
14
6
describe '#turn_right' do
15
7
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'
18
10
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'
21
13
end
22
14
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 = ''
26
18
27
- expect ( instance . turn_right ( current_direction ) ) . to be_nil
19
+ expect ( instance . turn_right ( direction ) ) . to be_nil
28
20
end
29
21
end
30
22
end
Original file line number Diff line number Diff line change 5
5
describe Position do
6
6
describe '#initialize' do
7
7
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' )
9
9
10
10
expect ( instance . x ) . to be_a Integer
11
11
expect ( instance . x ) . to be >= 0
12
12
expect ( instance . x ) . to be <= 4
13
13
end
14
14
15
15
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' )
17
17
18
18
expect ( instance . y ) . to be_a Integer
19
19
expect ( instance . y ) . to be >= 0
You can’t perform that action at this time.
0 commit comments