robot_rea 0.1.4 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -28
- 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 +4 -5
- 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 -5
- 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: f4016e42d5e11c2d3a2d304d1b499c3823cc905ee9a9ae4990ddb17edaf168c8
|
4
|
+
data.tar.gz: f9cfd2a9c681525eb2b27b62137985d8524616d4092e9f7e17cc8e249941f32e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d5d0d0c6ff0a229711c1f97b484dfb805fee3fad42720ff9379e85c419f9377aa5ddae623aa1d86b7be36c65b46541ebb47c1f33061b72179ab71756d2667c0
|
7
|
+
data.tar.gz: 18ee25e68835d739182a028c3269f1d88822460ecc9fb40c38f6c443842dccd1debb550bcb2de75749904d562e920d8ab1aac7e2edf9ddd9223aaccb0b74be2b
|
data/README.md
CHANGED
@@ -1,43 +1,30 @@
|
|
1
1
|
# Robot
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Robot game simulation gem.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
7
|
```ruby
|
12
|
-
gem '
|
8
|
+
gem install 'robot_rea'
|
13
9
|
```
|
14
10
|
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install robot
|
22
|
-
|
23
11
|
## Usage
|
24
12
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
-
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
-
|
33
|
-
## Contributing
|
13
|
+
Run the executable:
|
14
|
+
```
|
15
|
+
robot
|
16
|
+
```
|
34
17
|
|
35
|
-
|
18
|
+
Try some commands:
|
36
19
|
|
37
|
-
|
20
|
+
```
|
21
|
+
PLACE 0,0,NORTH
|
22
|
+
MOVE
|
23
|
+
REPORT
|
24
|
+
LEFT
|
25
|
+
REPORT
|
26
|
+
```
|
38
27
|
|
39
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
28
|
|
41
|
-
|
29
|
+
To run the specs, clone the repo then run `bundle exec rspec`
|
42
30
|
|
43
|
-
Everyone interacting in the Robot project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/robot/blob/master/CODE_OF_CONDUCT.md).
|
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'
|
@@ -9,6 +11,22 @@ require 'robot/game'
|
|
9
11
|
|
10
12
|
module Robot
|
11
13
|
def self.run
|
14
|
+
instructions
|
12
15
|
Game.()
|
13
16
|
end
|
17
|
+
|
18
|
+
def self.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
|
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,14 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Robot
|
2
4
|
class Game
|
3
5
|
attr_reader :input
|
6
|
+
|
4
7
|
def initialize(input: $stdin)
|
5
8
|
@input = input
|
6
9
|
end
|
7
10
|
|
8
11
|
def simulate
|
9
|
-
while command = input.gets
|
12
|
+
while (command = input.gets)
|
10
13
|
command = command.chomp
|
11
|
-
|
12
14
|
@position = Robot::CommandProxy.new(command_string: command, position: @position).call
|
13
15
|
end
|
14
16
|
end
|
@@ -19,6 +21,3 @@ module Robot
|
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
22
|
-
|
23
|
-
|
24
|
-
|
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.9
|
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-20 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
|
@@ -105,8 +104,6 @@ files:
|
|
105
104
|
- lib/robot/position.rb
|
106
105
|
- lib/robot/table.rb
|
107
106
|
- lib/robot/version.rb
|
108
|
-
- robot-0.1.0.gem
|
109
|
-
- robot-0.1.1.gem
|
110
107
|
- robot.gemspec
|
111
108
|
homepage: http://rubygems.org/gems/robot_rea
|
112
109
|
licenses:
|
data/config.yaml
DELETED