robot_rea 0.1.5 → 0.1.6
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 +2 -1
- data/lib/robot.rb +19 -1
- data/lib/robot/command_proxy.rb +6 -4
- data/lib/robot/commands.rb +2 -1
- data/lib/robot/commands/base.rb +4 -1
- data/lib/robot/commands/factory.rb +3 -1
- data/lib/robot/commands/left.rb +2 -0
- data/lib/robot/commands/move.rb +5 -3
- data/lib/robot/commands/no_op.rb +5 -4
- data/lib/robot/commands/place.rb +5 -2
- data/lib/robot/commands/place_command_parser.rb +3 -1
- data/lib/robot/commands/report.rb +3 -1
- data/lib/robot/commands/right.rb +3 -1
- data/lib/robot/directions.rb +2 -0
- data/lib/robot/game.rb +3 -20
- data/lib/robot/point.rb +8 -6
- data/lib/robot/position.rb +12 -12
- data/lib/robot/table.rb +5 -37
- data/lib/robot/version.rb +3 -1
- metadata +2 -3
- data/config.yaml +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c28f77b0f5fb3c609046a3439f11841d86d5e5f6ac54896647db26d77278f00
|
4
|
+
data.tar.gz: 76b2de4755bc647d06a5ee8f3b5fd6ebe4d13465a1b290fcf9c406b7d1c33f42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e174a31378b1d2eb8732212fbf888f9b5820cc131ffd562dcd34787ab7f090e7fc5799e1cd9a79f2aeebd8ed9c8d4872b309e6c795d0413de2cfe2f68c10161e
|
7
|
+
data.tar.gz: a06bb5ee747927d4aa82435e312348a7f10a1f16c959d85b843a7bed70a81a25210190c962f7e95f9c4f250af6939bdb72198f5ab4bca8d120f4b156cf4dc81d
|
data/README.md
CHANGED
data/lib/robot.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'robot/version'
|
2
4
|
require 'robot/point'
|
3
5
|
require 'robot/position'
|
4
6
|
require 'robot/directions'
|
@@ -10,5 +12,21 @@ require 'robot/game'
|
|
10
12
|
module Robot
|
11
13
|
def self.run
|
12
14
|
Game.()
|
15
|
+
instructions
|
16
|
+
end
|
17
|
+
|
18
|
+
def instructions
|
19
|
+
puts <<~HEREDOC
|
20
|
+
Welcome to the Robot game
|
21
|
+
Please enter one of the following commands:
|
22
|
+
PLACE 0,0,NORTH (PLACE X,Y,DIRECTION)
|
23
|
+
MOVE
|
24
|
+
LEFT
|
25
|
+
RIGHT
|
26
|
+
REPORT
|
27
|
+
|
28
|
+
invalid commands are ignored.
|
29
|
+
You must start with a place command.
|
30
|
+
HEREDOC
|
13
31
|
end
|
14
32
|
end
|
data/lib/robot/command_proxy.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
3
|
module Robot
|
4
|
+
# This class implements the proxy design pattern. It sits between the client and the commands,
|
5
|
+
# and only invokes the command if it passes validation.
|
4
6
|
class CommandProxy
|
5
|
-
|
6
|
-
attr_reader :command_string, :position, :min_point, :max_point
|
7
|
+
attr_reader :command_string, :position
|
7
8
|
|
8
9
|
def initialize(command_string:, position: nil)
|
9
10
|
@command_string = command_string
|
@@ -33,6 +34,7 @@ module Robot
|
|
33
34
|
|
34
35
|
def would_fall?
|
35
36
|
return unless [Robot::Commands::Move, Robot::Commands::Place].include?(command_class)
|
37
|
+
|
36
38
|
new_position = execute
|
37
39
|
new_position.point > table.max_point || new_position.point < table.min_point
|
38
40
|
end
|
data/lib/robot/commands.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'robot/commands/place_command_parser'
|
2
4
|
require 'robot/commands/base'
|
3
5
|
require 'robot/commands/no_op'
|
@@ -7,4 +9,3 @@ require 'robot/commands/right'
|
|
7
9
|
require 'robot/commands/place'
|
8
10
|
require 'robot/commands/report'
|
9
11
|
require 'robot/commands/factory'
|
10
|
-
|
data/lib/robot/commands/base.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Factory class that takes a command input and returns the command object that can handle it,
|
2
4
|
# this is used instead of if/else or case/when for each command.
|
3
5
|
# If the command entered does not match any known command, the NoOp Command will be returned.
|
4
6
|
|
5
7
|
module Robot
|
6
8
|
module Commands
|
7
|
-
COMMANDS = [Left, Right, Move, Place, Report, NoOp]
|
9
|
+
COMMANDS = [Left, Right, Move, Place, Report, NoOp].freeze
|
8
10
|
|
9
11
|
class Factory
|
10
12
|
def self.build(command)
|
data/lib/robot/commands/left.rb
CHANGED
data/lib/robot/commands/move.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Robot
|
2
4
|
module Commands
|
3
5
|
class Move < Base
|
4
|
-
MOVE = 'MOVE'
|
6
|
+
MOVE = 'MOVE'
|
5
7
|
|
6
8
|
def self.matches?(command)
|
7
9
|
command == MOVE
|
@@ -11,8 +13,8 @@ module Robot
|
|
11
13
|
{
|
12
14
|
Robot::Directions::NORTH => position.north,
|
13
15
|
Robot::Directions::SOUTH => position.south,
|
14
|
-
Robot::Directions::EAST
|
15
|
-
Robot::Directions::WEST
|
16
|
+
Robot::Directions::EAST => position.east,
|
17
|
+
Robot::Directions::WEST => position.west
|
16
18
|
}.fetch(position.direction)
|
17
19
|
end
|
18
20
|
end
|
data/lib/robot/commands/no_op.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
#
|
2
|
-
# enters is invalid. The idea is based on the null pointer design pattern
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
3
|
module Robot
|
5
4
|
module Commands
|
6
|
-
|
5
|
+
# A no operation command that gets returned to avoid nil exceptions when the command the user
|
6
|
+
# enters is invalid. The idea is based on the null pointer design pattern
|
7
7
|
|
8
|
-
|
8
|
+
class NoOp < Base
|
9
|
+
def self.matches?(_command)
|
9
10
|
true
|
10
11
|
end
|
11
12
|
|
data/lib/robot/commands/place.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Robot
|
2
4
|
module Commands
|
5
|
+
# Place Command
|
6
|
+
|
3
7
|
class Place < Base
|
4
8
|
extend PlaceCommandParser
|
5
9
|
|
6
|
-
REGEX = /^\A(PLACE) \d,\d,(NORTH|EAST|WEST|SOUTH)\z
|
7
|
-
|
10
|
+
REGEX = /^\A(PLACE) \d,\d,(NORTH|EAST|WEST|SOUTH)\z/.freeze
|
8
11
|
|
9
12
|
def self.matches?(command)
|
10
13
|
command =~ REGEX
|
data/lib/robot/commands/right.rb
CHANGED
data/lib/robot/directions.rb
CHANGED
data/lib/robot/game.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Robot
|
2
4
|
class Game
|
3
5
|
attr_reader :input
|
@@ -7,7 +9,7 @@ module Robot
|
|
7
9
|
end
|
8
10
|
|
9
11
|
def simulate
|
10
|
-
while command = input.gets
|
12
|
+
while (command = input.gets)
|
11
13
|
command = command.chomp
|
12
14
|
@position = Robot::CommandProxy.new(command_string: command, position: @position).call
|
13
15
|
end
|
@@ -15,26 +17,7 @@ module Robot
|
|
15
17
|
|
16
18
|
def self.call(input: $stdin)
|
17
19
|
game = new(input: input)
|
18
|
-
instruction
|
19
20
|
game.simulate
|
20
21
|
end
|
21
|
-
|
22
|
-
def self.instruction
|
23
|
-
puts <<~HEREDOC
|
24
|
-
Welcome to the Robot game
|
25
|
-
Please enter one of the following commands:
|
26
|
-
PLACE 0,0,NORTH (PLACE X,Y,DIRECTION)
|
27
|
-
MOVE
|
28
|
-
LEFT
|
29
|
-
RIGHT
|
30
|
-
REPORT
|
31
|
-
|
32
|
-
invalid commands are ignored.
|
33
|
-
You must start with a place command.
|
34
|
-
HEREDOC
|
35
|
-
end
|
36
22
|
end
|
37
23
|
end
|
38
|
-
|
39
|
-
|
40
|
-
|
data/lib/robot/point.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Point value object
|
2
4
|
|
3
5
|
module Robot
|
@@ -25,16 +27,16 @@ module Robot
|
|
25
27
|
Point.new(x: x - 1, y: y)
|
26
28
|
end
|
27
29
|
|
28
|
-
def ==(
|
29
|
-
x ==
|
30
|
+
def ==(other)
|
31
|
+
x == other.x && y == other.y
|
30
32
|
end
|
31
33
|
|
32
|
-
def >(
|
33
|
-
x >
|
34
|
+
def >(other)
|
35
|
+
x > other.x || y > other.y
|
34
36
|
end
|
35
37
|
|
36
|
-
def <(
|
37
|
-
x <
|
38
|
+
def <(other)
|
39
|
+
x < other.x || y < other.y
|
38
40
|
end
|
39
41
|
|
40
42
|
def to_s
|
data/lib/robot/position.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Position value object
|
2
4
|
|
3
5
|
module Robot
|
@@ -26,15 +28,15 @@ module Robot
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def left
|
29
|
-
Position.new(point: point, direction: lefts[direction]
|
31
|
+
Position.new(point: point, direction: lefts[direction])
|
30
32
|
end
|
31
33
|
|
32
34
|
def right
|
33
|
-
Position.new(point: point, direction: rights[direction]
|
35
|
+
Position.new(point: point, direction: rights[direction])
|
34
36
|
end
|
35
37
|
|
36
|
-
def ==(
|
37
|
-
point ==
|
38
|
+
def ==(other)
|
39
|
+
point == other.point && direction == other.direction
|
38
40
|
end
|
39
41
|
|
40
42
|
def to_s
|
@@ -45,22 +47,20 @@ module Robot
|
|
45
47
|
|
46
48
|
def lefts
|
47
49
|
{
|
48
|
-
Robot::Directions::EAST
|
50
|
+
Robot::Directions::EAST => Robot::Directions::NORTH,
|
49
51
|
Robot::Directions::NORTH => Robot::Directions::WEST,
|
50
|
-
Robot::Directions::WEST
|
51
|
-
Robot::Directions::SOUTH => Robot::Directions::EAST
|
52
|
+
Robot::Directions::WEST => Robot::Directions::SOUTH,
|
53
|
+
Robot::Directions::SOUTH => Robot::Directions::EAST
|
52
54
|
}.freeze
|
53
55
|
end
|
54
56
|
|
55
57
|
def rights
|
56
58
|
{
|
57
|
-
Robot::Directions::EAST
|
59
|
+
Robot::Directions::EAST => Robot::Directions::SOUTH,
|
58
60
|
Robot::Directions::SOUTH => Robot::Directions::WEST,
|
59
|
-
Robot::Directions::WEST
|
60
|
-
Robot::Directions::NORTH => Robot::Directions::EAST
|
61
|
+
Robot::Directions::WEST => Robot::Directions::NORTH,
|
62
|
+
Robot::Directions::NORTH => Robot::Directions::EAST
|
61
63
|
}.freeze
|
62
64
|
end
|
63
65
|
end
|
64
66
|
end
|
65
|
-
|
66
|
-
|
data/lib/robot/table.rb
CHANGED
@@ -1,46 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'yaml'
|
2
4
|
|
3
5
|
module Robot
|
4
6
|
class Table
|
5
7
|
attr_reader :min_point, :max_point
|
6
8
|
|
7
|
-
def initialize
|
8
|
-
@min_point =
|
9
|
-
@max_point =
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def set_min_point
|
15
|
-
Point.new(x: minimum_x, y: minimum_y)
|
16
|
-
end
|
17
|
-
|
18
|
-
def set_max_point
|
19
|
-
Point.new(x: maximum_x, y: maximum_y)
|
20
|
-
end
|
21
|
-
|
22
|
-
def config_file
|
23
|
-
YAML.load_file(File.join(root, 'config.yaml'))
|
24
|
-
end
|
25
|
-
|
26
|
-
def root
|
27
|
-
File.expand_path('../../../', __FILE__)
|
28
|
-
end
|
29
|
-
|
30
|
-
def minimum_x
|
31
|
-
config_file["min_x"]
|
32
|
-
end
|
33
|
-
|
34
|
-
def minimum_y
|
35
|
-
config_file["min_y"]
|
36
|
-
end
|
37
|
-
|
38
|
-
def maximum_x
|
39
|
-
config_file["max_x"]
|
40
|
-
end
|
41
|
-
|
42
|
-
def maximum_y
|
43
|
-
config_file["max_y"]
|
9
|
+
def initialize(min_point: Point.new(x: 0, y: 0), max_point: Point.new(x: 4, y: 4))
|
10
|
+
@min_point = min_point
|
11
|
+
@max_point = max_point
|
44
12
|
end
|
45
13
|
end
|
46
14
|
end
|
data/lib/robot/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: robot_rea
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anonymous
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-08-
|
11
|
+
date: 2020-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,7 +86,6 @@ files:
|
|
86
86
|
- bin/robot
|
87
87
|
- bin/robot.exe
|
88
88
|
- bin/setup
|
89
|
-
- config.yaml
|
90
89
|
- lib/robot.rb
|
91
90
|
- lib/robot/command_proxy.rb
|
92
91
|
- lib/robot/commands.rb
|
data/config.yaml
DELETED