robot_rea 0.1.5 → 0.1.6

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: 73095d64460267f93eb6b7068f2d4b728acd558013dc83ccc3465e168aa10955
4
- data.tar.gz: 9442ab1a0a426cbe65fee5500882f5230d15f55791bd5fa1d41733b3fc78a8c8
3
+ metadata.gz: 4c28f77b0f5fb3c609046a3439f11841d86d5e5f6ac54896647db26d77278f00
4
+ data.tar.gz: 76b2de4755bc647d06a5ee8f3b5fd6ebe4d13465a1b290fcf9c406b7d1c33f42
5
5
  SHA512:
6
- metadata.gz: 2944ec74a6eefc86a7807f32909940f63d0a62f4cb3b6452aad2903f947fa52755fbc01b407549eb92cac06c0b155d36f32997d69c2fd49730254ab0f6dec7bd
7
- data.tar.gz: afb15ea2ec832b0ae10dac2e43f538cb59c9cf1658d9491c45b8640dee0e115bc7b8320de5a0b5098a697ec041a8db6164f46b5b121291dc2d8b9f9c2824f8a5
6
+ metadata.gz: e174a31378b1d2eb8732212fbf888f9b5820cc131ffd562dcd34787ab7f090e7fc5799e1cd9a79f2aeebd8ed9c8d4872b309e6c795d0413de2cfe2f68c10161e
7
+ data.tar.gz: a06bb5ee747927d4aa82435e312348a7f10a1f16c959d85b843a7bed70a81a25210190c962f7e95f9c4f250af6939bdb72198f5ab4bca8d120f4b156cf4dc81d
data/README.md CHANGED
@@ -25,5 +25,6 @@ LEFT
25
25
  REPORT
26
26
  ```
27
27
 
28
- To configure the table size, update the file `config.yaml`
28
+
29
+ To run the specs, clone the repo then run `bundle exec rspec`
29
30
 
@@ -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'
@@ -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
@@ -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,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
-
@@ -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.5"
4
+ VERSION = '0.1.6'
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.5
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-09 00:00:00.000000000 Z
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
@@ -1,4 +0,0 @@
1
- min_x: 0
2
- min_y: 0
3
- max_x: 4
4
- max_y: 4