maux_robot 0.0.5 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -2
- data/lib/maux_robot/cli.rb +5 -1
- data/lib/maux_robot/position.rb +1 -1
- data/lib/maux_robot/robot.rb +66 -8
- data/lib/maux_robot/table.rb +2 -1
- data/lib/maux_robot/version.rb +1 -1
- data/spec/maux_robot/robot_spec.rb +12 -19
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 961e2b5489e001d3676b9a8f4360b212b62e677b
|
4
|
+
data.tar.gz: '028a03a16e37befedecb9336b8d1a5f6c3cd78e1'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1676475a44d162ae4038dc3862e380f81527caf621e3ea74e942ce3d431553476635373cffe05e9f4e468afea18ce5035e2c7d297bbfd3d57d645bf6fb1def11
|
7
|
+
data.tar.gz: ff3e5c81d4c9d4d348b90d7e99e76e683fb14064c2bf4431203498b479de0e60a4d1c2da186a4e4b59f63590e2868c4c1d25e0a63d6bff37dd6abb5ebb591819
|
data/README.md
CHANGED
@@ -24,7 +24,8 @@ PLACE X,Y,F
|
|
24
24
|
MOVE
|
25
25
|
LEFT
|
26
26
|
RIGHT
|
27
|
-
REPORT
|
27
|
+
REPORT [csv|json]
|
28
|
+
VERBOSE
|
28
29
|
```
|
29
30
|
|
30
31
|
* `PLACE` will put the toy robot on the table in position X,Y
|
@@ -40,7 +41,8 @@ in the direction it is currently facing.
|
|
40
41
|
* `LEFT` and `RIGHT` will rotate the robot 90 degrees
|
41
42
|
in the specified direction
|
42
43
|
without changing the position of the robot.
|
43
|
-
* `REPORT` will announce the X,Y and F of the robot.
|
44
|
+
* `REPORT` will announce the X,Y and F of the robot. If `csv` or `json` is passed, it formats the whereabouts accordingly. The default format is `csv`.
|
45
|
+
* `VERBOSE` will turn the error messages (talkative mode) on or off. The robot starts with talkative mode set to off.
|
44
46
|
|
45
47
|
* A robot that is not on the table ignores the `MOVE`, `LEFT`, `RIGHT` and `REPORT` commands.
|
46
48
|
|
data/lib/maux_robot/cli.rb
CHANGED
@@ -17,7 +17,11 @@ module MauxRobot
|
|
17
17
|
def run_script(all_input)
|
18
18
|
all_input.split("\n").each do |line_input|
|
19
19
|
command = parse(line_input)
|
20
|
-
|
20
|
+
begin
|
21
|
+
execute(command) if command
|
22
|
+
rescue MauxRobot::RobotError => e
|
23
|
+
puts e if @robot.talkative
|
24
|
+
end
|
21
25
|
end
|
22
26
|
end
|
23
27
|
|
data/lib/maux_robot/position.rb
CHANGED
data/lib/maux_robot/robot.rb
CHANGED
@@ -4,31 +4,89 @@ require 'forwardable'
|
|
4
4
|
|
5
5
|
module MauxRobot
|
6
6
|
|
7
|
+
class RobotError < StandardError; end
|
8
|
+
|
9
|
+
# Error to notify that the robot was not placed yet
|
10
|
+
class RobotNotPlacedYet < RobotError
|
11
|
+
def initialize
|
12
|
+
super('The robot was not placed yet!')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Error to notify that the position is/would be outside the table
|
17
|
+
class NotOkToGo < RobotError
|
18
|
+
def initialize(position)
|
19
|
+
super("It's not ok to go to #{position.x},#{position.y}")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
7
23
|
# The main class. It executes the actions using its table and position
|
8
24
|
class Robot
|
9
|
-
attr_reader :position
|
10
|
-
|
11
|
-
extend Forwardable
|
12
|
-
def_delegators :@position, :left, :right, :report
|
25
|
+
attr_reader :position, :talkative
|
13
26
|
|
14
27
|
def initialize(table=MauxRobot::Table.new)
|
15
28
|
@table = table
|
16
29
|
@position = MauxRobot::NullPosition.new
|
30
|
+
@talkative = false
|
17
31
|
end
|
18
32
|
|
19
33
|
def place(x:, y:, face:)
|
20
34
|
position = MauxRobot::Position.new(x, y, face)
|
21
35
|
|
22
|
-
|
36
|
+
ok_to_go?(position) do
|
23
37
|
@position = position
|
24
38
|
end
|
25
39
|
end
|
26
40
|
|
27
41
|
def move
|
28
|
-
|
42
|
+
robot_placed? do
|
43
|
+
next_position = @position.forward_position
|
44
|
+
|
45
|
+
ok_to_go?(next_position) do
|
46
|
+
@position = next_position
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def left
|
52
|
+
robot_placed? do
|
53
|
+
@position.left
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def right
|
58
|
+
robot_placed? do
|
59
|
+
@position.right
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def report(format_type: :csv)
|
64
|
+
robot_placed? do
|
65
|
+
@position.report(format_type)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def verbose
|
70
|
+
@talkative = !@talkative
|
71
|
+
is_not = @talkative ? 'is' : 'is not'
|
72
|
+
puts "Robot #{is_not} talkative now."
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def ok_to_go?(position)
|
78
|
+
if @table.contains?(position)
|
79
|
+
yield
|
80
|
+
else
|
81
|
+
raise NotOkToGo.new(position)
|
82
|
+
end
|
83
|
+
end
|
29
84
|
|
30
|
-
|
31
|
-
|
85
|
+
def robot_placed?
|
86
|
+
unless @position.class == MauxRobot::NullPosition
|
87
|
+
yield
|
88
|
+
else
|
89
|
+
raise RobotNotPlacedYet
|
32
90
|
end
|
33
91
|
end
|
34
92
|
end
|
data/lib/maux_robot/table.rb
CHANGED
data/lib/maux_robot/version.rb
CHANGED
@@ -14,28 +14,22 @@ describe MauxRobot::Robot do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
context 'given an invalid place' do
|
17
|
-
it 'should
|
18
|
-
robot.place(x: 5, y: 0, face: :south)
|
19
|
-
|
20
|
-
expect(robot.position).to eq null_position
|
17
|
+
it 'should raise NotOkToGo error' do
|
18
|
+
expect { robot.place(x: 5, y: 0, face: :south) }.to raise_error MauxRobot::NotOkToGo
|
21
19
|
end
|
22
20
|
end
|
23
21
|
|
24
22
|
context 'given an invalid direction' do
|
25
|
-
it 'should
|
26
|
-
robot.place(x: 0, y: 2, face: :south_east)
|
27
|
-
|
28
|
-
expect(robot.position).to eq null_position
|
23
|
+
it 'should raise NotOkToGo error' do
|
24
|
+
expect { robot.place(x: 0, y: 2, face: :south_east) }.to raise_error MauxRobot::NotOkToGo
|
29
25
|
end
|
30
26
|
end
|
31
27
|
end
|
32
28
|
|
33
29
|
describe '#left' do
|
34
30
|
context 'robot not placed' do
|
35
|
-
it 'should
|
36
|
-
robot.
|
37
|
-
robot.left
|
38
|
-
|
31
|
+
it 'should raise RobotNotPlacedYet error' do
|
32
|
+
expect { robot.left }.to raise_error MauxRobot::RobotNotPlacedYet
|
39
33
|
expect(robot.position).to eq null_position
|
40
34
|
end
|
41
35
|
end
|
@@ -52,9 +46,8 @@ describe MauxRobot::Robot do
|
|
52
46
|
|
53
47
|
describe '#right' do
|
54
48
|
context 'robot not placed' do
|
55
|
-
it 'should
|
56
|
-
robot.
|
57
|
-
robot.right
|
49
|
+
it 'should raise RobotNotPlacedYet error' do
|
50
|
+
expect { robot.right }.to raise_error MauxRobot::RobotNotPlacedYet
|
58
51
|
|
59
52
|
expect(robot.position).to eq null_position
|
60
53
|
end
|
@@ -72,9 +65,8 @@ describe MauxRobot::Robot do
|
|
72
65
|
|
73
66
|
describe '#move' do
|
74
67
|
context 'robot not placed' do
|
75
|
-
it 'should
|
76
|
-
robot.
|
77
|
-
|
68
|
+
it 'should raise RobotNotPlacedYet error' do
|
69
|
+
expect { robot.right }.to raise_error MauxRobot::RobotNotPlacedYet
|
78
70
|
expect(robot.position).to eq null_position
|
79
71
|
end
|
80
72
|
end
|
@@ -93,7 +85,8 @@ describe MauxRobot::Robot do
|
|
93
85
|
context 'to outside the table' do
|
94
86
|
it 'should stay where it is' do
|
95
87
|
robot.place(x: 0, y: 0, face: :south)
|
96
|
-
|
88
|
+
|
89
|
+
expect { robot.move }.to raise_error MauxRobot::NotOkToGo
|
97
90
|
|
98
91
|
expect(robot.position.x).to eq(0)
|
99
92
|
expect(robot.position.y).to eq(0)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maux_robot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mauricio Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|