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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cce95d6985de86d54196c23696060da2a089fdd251c3d48368be695c7f22c494
4
- data.tar.gz: 19e0d3f20d223884e0ec59860ee1a735a7f57181de20afee9759af0318638683
3
+ metadata.gz: f4016e42d5e11c2d3a2d304d1b499c3823cc905ee9a9ae4990ddb17edaf168c8
4
+ data.tar.gz: f9cfd2a9c681525eb2b27b62137985d8524616d4092e9f7e17cc8e249941f32e
5
5
  SHA512:
6
- metadata.gz: 70faa108dac6ed5ff1d7ac2146af47f2da3f17326c5b10c5b4b8cf9db96038241719b0af53b0663775293d9d937ca42196573001c4fe1da9711959cd3d65b501
7
- data.tar.gz: c7d23f3794b00b7e7a422fd114b3568cc7e28b353256ce328658748d5d536490f1566739f1b6c4fa7324b76f1287948ffff14fe36c44d508d650d5644178bff1
6
+ metadata.gz: 1d5d0d0c6ff0a229711c1f97b484dfb805fee3fad42720ff9379e85c419f9377aa5ddae623aa1d86b7be36c65b46541ebb47c1f33061b72179ab71756d2667c0
7
+ data.tar.gz: 18ee25e68835d739182a028c3269f1d88822460ecc9fb40c38f6c443842dccd1debb550bcb2de75749904d562e920d8ab1aac7e2edf9ddd9223aaccb0b74be2b
data/README.md CHANGED
@@ -1,43 +1,30 @@
1
1
  # Robot
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/robot`. To experiment with that code, run `bin/console` for an interactive prompt.
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 'robot'
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
- TODO: Write usage instructions here
26
-
27
- ## Development
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
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/robot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
18
+ Try some commands:
36
19
 
37
- ## License
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
- ## Code of Conduct
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).
@@ -1,4 +1,6 @@
1
- require "robot/version"
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
@@ -1,9 +1,10 @@
1
- # This class implements the proxy design pattern. It sits between the client and the commands,
2
- # and only invokes the command if it passes validation.
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
@@ -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
-
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Robot
2
4
  module Commands
5
+ # Command Base/Interface
3
6
  class Base
4
- def self.matches(command)
7
+ def self.matches(_command)
5
8
  raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
6
9
  end
7
10
 
@@ -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)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Robot
2
4
  module Commands
3
5
  class Left < Base
@@ -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'.freeze
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 => position.east,
15
- Robot::Directions::WEST => position.west,
16
+ Robot::Directions::EAST => position.east,
17
+ Robot::Directions::WEST => position.west
16
18
  }.fetch(position.direction)
17
19
  end
18
20
  end
@@ -1,11 +1,12 @@
1
- # A no operation command that's returned to avoid nil exceptions when the command the user
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
- class NoOp < Base
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
- def self.matches?(command)
8
+ class NoOp < Base
9
+ def self.matches?(_command)
9
10
  true
10
11
  end
11
12
 
@@ -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
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Robot
2
4
  module Commands
3
5
  module PlaceCommandParser
4
6
  def parse(command)
5
- command.gsub('PLACE ', '').split(",")
7
+ command.gsub('PLACE ', '').split(',')
6
8
  end
7
9
  end
8
10
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Robot
2
4
  module Commands
3
5
  class Report < Base
4
- REPORT = 'REPORT'.freeze
6
+ REPORT = 'REPORT'
5
7
 
6
8
  def self.matches?(command)
7
9
  command == REPORT
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Robot
2
4
  module Commands
3
5
  class Right < Base
4
- RIGHT = 'RIGHT'.freeze
6
+ RIGHT = 'RIGHT'
5
7
 
6
8
  def self.matches?(command)
7
9
  command == RIGHT
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Robot
2
4
  module Directions
3
5
  EAST = :EAST
@@ -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
-
@@ -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 ==(point)
29
- x == point.x && y == point.y
30
+ def ==(other)
31
+ x == other.x && y == other.y
30
32
  end
31
33
 
32
- def >(point)
33
- x > point.x || y > point.y
34
+ def >(other)
35
+ x > other.x || y > other.y
34
36
  end
35
37
 
36
- def <(point)
37
- x < point.x || y < point.y
38
+ def <(other)
39
+ x < other.x || y < other.y
38
40
  end
39
41
 
40
42
  def to_s
@@ -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 ==(position)
37
- point == position.point && direction == position.direction
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 => Robot::Directions::NORTH,
50
+ Robot::Directions::EAST => Robot::Directions::NORTH,
49
51
  Robot::Directions::NORTH => Robot::Directions::WEST,
50
- Robot::Directions::WEST => Robot::Directions::SOUTH,
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 => Robot::Directions::SOUTH,
59
+ Robot::Directions::EAST => Robot::Directions::SOUTH,
58
60
  Robot::Directions::SOUTH => Robot::Directions::WEST,
59
- Robot::Directions::WEST => Robot::Directions::NORTH,
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
-
@@ -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 = set_min_point
9
- @max_point = set_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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Robot
2
- VERSION = "0.1.4"
4
+ VERSION = '0.1.9'
3
5
  end
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
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-09 00:00:00.000000000 Z
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:
@@ -1,4 +0,0 @@
1
- min_x: 0
2
- min_y: 0
3
- max_x: 4
4
- max_y: 4