Skip to content

Commit 5047899

Browse files
committed
Tidy and reorganise code to make more sense. Update specs.
1 parent ceed3ff commit 5047899

File tree

16 files changed

+226
-240
lines changed

16 files changed

+226
-240
lines changed

lib/actions/move.rb

Lines changed: 0 additions & 39 deletions
This file was deleted.

lib/actions/place.rb

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/actions/report.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/actions/turn.rb

Lines changed: 0 additions & 45 deletions
This file was deleted.

lib/handle_input.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
require_relative './actions/place'
2-
require_relative './actions/move'
3-
require_relative './actions/turn'
4-
require_relative './actions/report'
5-
6-
# HandleInput class
1+
# HandleInput
72
class HandleInput
83
attr_accessor :robot, :table
94

@@ -21,18 +16,25 @@ def initialize(robot, table)
2116

2217
# Interpret user Command
2318
def interpret(command)
24-
return Place.new(robot, table).perform(command) if PLACE.match?(command)
19+
return exec(robot.place(command)) if PLACE.match?(command)
2520

2621
return if robot.not_in_place?
2722

28-
return Move.new(robot, table).perform if MOVE.match?(command)
23+
return exec(robot.move(robot.position)) if MOVE.match?(command)
2924

30-
return Turn.new(robot).left if LEFT.match?(command)
25+
return exec(robot.left(robot.position)) if LEFT.match?(command)
3126

32-
return Turn.new(robot).right if RIGHT.match?(command)
27+
return exec(robot.right(robot.position)) if RIGHT.match?(command)
3328

34-
return Report.new(robot).perform if REPORT.match?(command)
29+
return robot.report(robot.position) if REPORT.match?(command)
3530

3631
# TODO: Exception handler
3732
end
33+
34+
# Update robot position if it's valid
35+
def exec(position)
36+
return unless table.valid_position?(position)
37+
38+
robot.update_robot(position)
39+
end
3840
end

lib/position.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Position class
1+
# Position
22
class Position
33
attr_accessor :x, :y, :f
44

lib/robot.rb

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,76 @@
11
require_relative 'position'
22

3-
# Robot Class
3+
# Robot
44
class Robot
55
attr_reader :position
66

77
def intialize
88
@position = nil
99
end
1010

11-
# Update the robot's position
11+
def place(command)
12+
_command, x, y, f = command.tr(',', ' ').split
13+
14+
Position.new(x.to_i, y.to_i, f)
15+
end
16+
17+
def move(position)
18+
y = position.y
19+
x = position.x
20+
f = position.f
21+
22+
y += 1 if f.match?('NORTH')
23+
x += 1 if f.match?('EAST')
24+
y -= 1 if f.match?('SOUTH')
25+
x -= 1 if f.match?('WEST')
26+
27+
Position.new(x, y, f)
28+
end
29+
30+
def left(position)
31+
Position.new(position.x, position.y, prev_option(position.f))
32+
end
33+
34+
def right(position)
35+
Position.new(position.x, position.y, next_option(position.f))
36+
end
37+
38+
def report(position)
39+
message = "Output: #{position.x},#{position.y},#{position.f} \n"
40+
$stdout.print message
41+
end
42+
1243
def update_robot(new_position)
1344
@position = new_position
1445
end
1546

16-
# Check the robot has been placed
1747
def not_in_place?
1848
@position.nil?
1949
end
50+
51+
private
52+
53+
OPTIONS = %w[WEST NORTH EAST SOUTH].freeze
54+
55+
def prev_option(direction)
56+
return unless OPTIONS.include?(direction)
57+
58+
return OPTIONS.last if direction == OPTIONS.first
59+
60+
i = OPTIONS.index { |e| e == direction }
61+
i -= 1
62+
63+
OPTIONS.fetch(i)
64+
end
65+
66+
def next_option(direction)
67+
return unless OPTIONS.include?(direction)
68+
69+
return OPTIONS.first if direction == OPTIONS.last
70+
71+
i = OPTIONS.index { |e| e == direction }
72+
i += 1
73+
74+
OPTIONS.fetch(i)
75+
end
2076
end

lib/table.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Table Class
1+
# Table
22
class Table
33
attr_reader :x, :y
44

@@ -7,8 +7,7 @@ def initialize(x = 5, y = 5)
77
@y = (0..(y - 1))
88
end
99

10-
# Validate x & y axis are within 5x5 area
11-
def valid_position?(x, y)
12-
@x.cover?(x) && @y.cover?(y)
10+
def valid_position?(position)
11+
@x.cover?(position.x) && @y.cover?(position.y)
1312
end
1413
end

spec/handle_input_spec.rb

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,74 @@
1818
end
1919

2020
describe '#interpret' do
21-
it 'should return an instance of Position command matches place pattern' do
21+
it 'should return an instance of Position command matches PLACE pattern' do
2222
instance = HandleInput.new(Robot.new, Table.new)
2323
command = 'PLACE 1,2,NORTH'
2424

2525
expect(instance.interpret(command)).to be_a Position
2626
end
2727

28-
it 'should return nil if command does not match a valid pattern' do
28+
it 'should return nil if robot is not in place' do
2929
instance = HandleInput.new(Robot.new, Table.new)
30-
command = 'hello'
30+
command = ''
3131

3232
expect(instance.interpret(command)).to be_nil
3333
end
34+
35+
it 'should return an instance of Position if command matches MOVE pattern' do
36+
instance = HandleInput.new(Robot.new, Table.new)
37+
command1 = 'PLACE 1,2,NORTH'
38+
command2 = 'MOVE'
39+
40+
instance.interpret(command1)
41+
42+
expect(instance.interpret(command2)).to be_a Position
43+
end
44+
45+
it 'should return an instance of Position if command matches LEFT pattern' do
46+
instance = HandleInput.new(Robot.new, Table.new)
47+
command1 = 'PLACE 1,2,NORTH'
48+
command2 = 'LEFT'
49+
50+
instance.interpret(command1)
51+
52+
expect(instance.interpret(command2)).to be_a Position
53+
end
54+
55+
it 'should return an instance of Position if command matches RIGHT pattern' do
56+
instance = HandleInput.new(Robot.new, Table.new)
57+
command1 = 'PLACE 1,2,NORTH'
58+
command2 = 'RIGHT'
59+
60+
instance.interpret(command1)
61+
62+
expect(instance.interpret(command2)).to be_a Position
63+
end
64+
65+
it 'should return nil if command matches REPORT pattern' do
66+
instance = HandleInput.new(Robot.new, Table.new)
67+
command1 = 'PLACE 1,2,NORTH'
68+
command2 = 'REPORT'
69+
70+
instance.interpret(command1)
71+
72+
expect(instance.interpret(command2)).to be_nil
73+
end
74+
end
75+
76+
describe '#exec' do
77+
it 'should return an instance of Position if position is a valid table position' do
78+
instance = HandleInput.new(Robot.new, Table.new)
79+
position = Position.new(0, 0, 'NORTH')
80+
81+
expect(instance.exec(position)).to be_a Position
82+
end
83+
84+
it 'should return nil if position is not a valid table position' do
85+
instance = HandleInput.new(Robot.new, Table.new)
86+
position = Position.new(0, 6, 'NORTH')
87+
88+
expect(instance.exec(position)).to be_nil
89+
end
3490
end
3591
end

spec/move_spec.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)