toy_robot_cli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f9ea2b0b48996a8fcb4f68df38f7773060e92ac1221d36e09eb05268a6cb6db6
4
+ data.tar.gz: '0145910220ff22a216dfe8b4ffe4786d337a993d0a8b743c52b9092fa716b7b9'
5
+ SHA512:
6
+ metadata.gz: be4037d0629acfd481136ea6ab35a7875a3b3ec2670a55d9e93e86f866b0b1c555b98366a5e0518c288431803f4ac64d5183479ff6cb7d19da149cf9eb7c1133
7
+ data.tar.gz: aa39acda6ad7fb7b128970fc40420888a314c784c83bb81f2a951a3a48a7d7bcbe439d439a3769e9a49ee83508848111b8029fce45cfaf24ecf8016d42cdec12
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
14
+
15
+ Style/Documentation:
16
+ Enabled: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Paulo H Sacramento
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # ToyRobotCli
2
+
3
+ ToyRobotCli is a RubyGem that provides a command-line interface (CLI) for interacting with a toy robot on a table.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'toy_robot_cli'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```shell
16
+ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```ruby
22
+ gem install toy_robot_cli
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ To use the ToyRobotCli gem, you can run the toy_robot command in your terminal.
28
+
29
+ The available commands are:
30
+
31
+ - place X Y FACING: Place the robot on the table at position X,Y facing the specified direction (NORTH, EAST, SOUTH, WEST).
32
+ - move: Move the robot one unit forward in the current direction.
33
+ - left: Rotate the robot 90 degrees to the left.
34
+ - right: Rotate the robot 90 degrees to the right.
35
+ - report: Report the current position and direction of the robot.
36
+
37
+ Here's an example usage:
38
+
39
+ ```shell
40
+ $ toy_robot
41
+ > place 1 2 EAST
42
+ > move
43
+ > move
44
+ > right
45
+ > move
46
+ > report
47
+ ```
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` or `bundle exec cucumber features` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/phsacramento/toy_robot_cli.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/toy_robot ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "toy_robot_cli"
5
+
6
+ ToyRobotCli::CLI.start
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+
5
+ module ToyRobotCli
6
+ class CLI < Thor
7
+ VALID_DIRECTIONS = %w[NORTH EAST SOUTH WEST].freeze
8
+
9
+ def initialize(*args)
10
+ super
11
+ @robot = Robot.new
12
+ end
13
+
14
+ desc "place X Y FACING", "Place the robot on the table"
15
+ def place(x, y, facing)
16
+ @robot.place(x.to_i, y.to_i, facing.upcase)
17
+ end
18
+
19
+ desc "move", "Move the robot one unit forward in the current direction"
20
+ def move
21
+ @robot.move
22
+ end
23
+
24
+ desc "left", "Rotate the robot 90 degrees to the left"
25
+ def left
26
+ @robot.left
27
+ end
28
+
29
+ desc "right", "Rotate the robot 90 degrees to the right"
30
+ def right
31
+ @robot.right
32
+ end
33
+
34
+ desc "report", "Report the current position and direction of the robot"
35
+ def report
36
+ puts @robot.report
37
+ end
38
+
39
+ desc "interactive", "Interactive mode to input commands"
40
+ def interactive
41
+ loop do
42
+ print "> "
43
+ input = $stdin.gets.chomp.downcase
44
+
45
+ command, *args = input.split(" ")
46
+
47
+ case command
48
+ when "place"
49
+ place(*args)
50
+ when "move"
51
+ move
52
+ when "left"
53
+ left
54
+ when "right"
55
+ right
56
+ when "report"
57
+ report
58
+ when "exit"
59
+ break
60
+ else
61
+ puts "Invalid command. Please try again."
62
+ end
63
+ end
64
+ end
65
+
66
+ default_task :interactive
67
+ end
68
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ruby module to wrap robot moves features
4
+ module ToyRobotCli
5
+ # Ruby class to handle Robot moves on square table top
6
+ class Robot
7
+ # Valid directions what robot can be faced
8
+ VALID_DIRECTIONS = %w[NORTH EAST SOUTH WEST].freeze
9
+
10
+ # Initialize a new Robot instance.
11
+ def initialize
12
+ @position = nil
13
+ @direction = nil
14
+ end
15
+
16
+ # Place the robot on the table at the specified coordinates and facing direction.
17
+ # Example:
18
+ # >> robot = ToyRobotCli::Robot.new
19
+ # => #<ToyRobotCli::Robot:0x000000... @direction=nil, @position=nil>
20
+ # >> robot.place(0, 0, "SOUTH")
21
+ # => 2
22
+ #
23
+ # Arguments:
24
+ # coordinate_x: (Integer) - The X-coordinate of the robot's position.
25
+ # coordinate_y: (Integer) - The Y-coordinate of the robot's position.
26
+ # facing: (String) - The direction the robot is facing (NORTH, EAST, SOUTH, WEST).
27
+
28
+ def place(coordinate_x, coordinate_y, facing)
29
+ return unless valid_position?(coordinate_x, coordinate_y) && VALID_DIRECTIONS.include?(facing)
30
+
31
+ @position = Matrix[[coordinate_x], [coordinate_y]]
32
+ @direction = VALID_DIRECTIONS.index(facing)
33
+ end
34
+
35
+ # Move the robot one unit forward in the current direction.
36
+ def move
37
+ return unless placed?
38
+
39
+ new_position = @position + direction_vector
40
+ @position = new_position if valid_position?(new_position[0, 0], new_position[1, 0])
41
+ end
42
+
43
+ # Rotate the robot 90 degrees to the left.
44
+ def left
45
+ return unless placed?
46
+
47
+ @direction = (@direction - 1) % VALID_DIRECTIONS.length
48
+ end
49
+
50
+ # Rotate the robot 90 degrees to the right.
51
+ def right
52
+ return unless placed?
53
+
54
+ @direction = (@direction + 1) % VALID_DIRECTIONS.length
55
+ end
56
+
57
+ # Report the current position and direction of the robot.
58
+ # Example:
59
+ # >> robot = ToyRobotCli::Robot.new
60
+ # => #<ToyRobotCli::Robot:0x000000... @direction=nil, @position=nil>
61
+ # >> robot.place(0, 0, "SOUTH")
62
+ # => 2
63
+ # >> robot.report
64
+ # => "0,0,SOUTH"
65
+ #
66
+ # Returns:
67
+ # (String) - The current position and direction of the robot in the format "X,Y,FACING".
68
+ def report
69
+ return unless placed?
70
+
71
+ coordinate_x, coordinate_y = @position.to_a.flatten
72
+ "#{coordinate_x},#{coordinate_y},#{VALID_DIRECTIONS[@direction]}"
73
+ end
74
+
75
+ private
76
+
77
+ # Check if the specified position is valid within the table boundaries.
78
+ def valid_position?(coordinate_x, coordinate_y)
79
+ (0..4).cover?(coordinate_x) && (0..4).cover?(coordinate_y)
80
+ end
81
+
82
+ # Check if the robot is placed on the table.
83
+ def placed?
84
+ !@position.nil? && !@direction.nil?
85
+ end
86
+
87
+ # Calculate the direction vector based on the current direction.
88
+ def direction_vector
89
+ direction = Matrix[[0], [0]]
90
+ update_direction(direction)
91
+ direction
92
+ end
93
+
94
+ # Update the direction vector based on the current direction.
95
+ def update_direction(direction)
96
+ case @direction
97
+ when 0
98
+ direction[1, 0] = -1
99
+ when 1
100
+ direction[0, 0] = 1
101
+ when 2
102
+ direction[1, 0] = 1
103
+ when 3
104
+ direction[0, 0] = -1
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ToyRobotCli
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "matrix"
4
+
5
+ require File.expand_path("toy_robot_cli/cli", __dir__)
6
+ require File.expand_path("toy_robot_cli/version", __dir__)
7
+ require File.expand_path("toy_robot_cli/robot", __dir__)
8
+
9
+ module ToyRobotCli
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
@@ -0,0 +1,4 @@
1
+ module ToyRobotCli
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/toy_robot_cli/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "toy_robot_cli"
7
+ spec.version = ToyRobotCli::VERSION
8
+
9
+ spec.authors = ["Paulo H Sacramento"]
10
+ spec.email = ["contato@henriquesacramento.net"]
11
+
12
+ spec.summary = "A CLI interface for the Toy Robot gem."
13
+ spec.description = "A CLI interface using the thor gem for the Toy Robot gem."
14
+ spec.homepage = "https://github.com/phsacramento/toy-robot-cli"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 2.6.0"
17
+
18
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/phsacramento/toy-robot-cli"
22
+ spec.metadata["changelog_uri"] = "https://github.com/phsacramento/toy-robot-cli"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(__dir__) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ (File.expand_path(f) == __FILE__) ||
29
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile {lib,bin}/**/* lib/**/*])
30
+ end
31
+ end
32
+ spec.executables = ["toy_robot"]
33
+ spec.bindir = "bin"
34
+ spec.require_paths = ["lib"]
35
+
36
+ # Uncomment to register a new dependency of your gem
37
+ spec.add_development_dependency "aruba", "~> 2.1"
38
+ spec.add_development_dependency "cucumber", "~> 8.0"
39
+
40
+ spec.add_dependency "matrix", "~> 0.4.2"
41
+ spec.add_dependency "rdoc", "~> 4.2", ">= 4.2.2"
42
+ spec.add_dependency "thor", "~> 1.2", ">= 1.2.2"
43
+
44
+ # For more information and examples about making a new gem, check out our
45
+ # guide at: https://bundler.io/guides/creating_gem.html
46
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toy_robot_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paulo H Sacramento
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aruba
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cucumber
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '8.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '8.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: matrix
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 4.2.2
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '4.2'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 4.2.2
75
+ - !ruby/object:Gem::Dependency
76
+ name: thor
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.2'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 1.2.2
85
+ type: :runtime
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '1.2'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 1.2.2
95
+ description: A CLI interface using the thor gem for the Toy Robot gem.
96
+ email:
97
+ - contato@henriquesacramento.net
98
+ executables:
99
+ - toy_robot
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".rubocop.yml"
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - bin/toy_robot
108
+ - lib/toy_robot_cli.rb
109
+ - lib/toy_robot_cli/cli.rb
110
+ - lib/toy_robot_cli/robot.rb
111
+ - lib/toy_robot_cli/version.rb
112
+ - sig/toy_robot_cli.rbs
113
+ - toy_robot_cli.gemspec
114
+ homepage: https://github.com/phsacramento/toy-robot-cli
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ allowed_push_host: https://rubygems.org
119
+ homepage_uri: https://github.com/phsacramento/toy-robot-cli
120
+ source_code_uri: https://github.com/phsacramento/toy-robot-cli
121
+ changelog_uri: https://github.com/phsacramento/toy-robot-cli
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 2.6.0
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubygems_version: 3.4.17
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: A CLI interface for the Toy Robot gem.
141
+ test_files: []