robotoy 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56a479731ddcb3a09a0793b69ead552469426cf1
4
+ data.tar.gz: 3893ccbc3181adff03d2d14a8e70473c530bf84c
5
+ SHA512:
6
+ metadata.gz: eb4968be37f2beef217592f526b0995a8404bc92bf3ba520a0692705386a193308e10aab31e22179115af47a31c21e543f3be0d1854cb2e5e774f769f041a9ff
7
+ data.tar.gz: 0c0f8f8532b621faeb465b94260be048393fd8142949d4ce503ce67259676f0bc42c0cdac3e80782f54409d065a18b84ac64c97715351d8c76af997a6c343b23
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .*.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at hossein@mindvalley.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in robotoy.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 hosseintoussi
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.
@@ -0,0 +1,37 @@
1
+ [![Build Status](https://travis-ci.org/hosseintoussi/robotoy.svg?branch=master)](https://travis-ci.org/hosseintoussi/robotoy)
2
+ # Robotoy
3
+ - The application is a simulation of a toy robot moving on a square tabletop, of dimensions 5 units x 5 units.
4
+ - There are no other obstructions on the table surface.
5
+ - The robot is free to roam around the surface of the table, but must be prevented from falling to destruction. Any movement
6
+ that would result in the robot falling from the table must be prevented, however further valid movement commands must still
7
+ be allowed.
8
+
9
+ ## Installation
10
+
11
+ Install it yourself with:
12
+
13
+ $ gem install robotoy
14
+
15
+ ## Usage
16
+
17
+ After installation you can run the app by passing a file of commands( a sample file is provided in this repo `commands.txt`) to `robotoy` in terminal:
18
+
19
+ ```
20
+ robotoy path/to/commands.txt
21
+ ```
22
+
23
+ ## Development
24
+
25
+ Run `rspec` to run the tests. You can also run `rake console` for an interactive prompt that will allow you to experiment.
26
+
27
+ 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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hosseintoussi/robotoy. 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.
32
+
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
37
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :console do
9
+ exec "irb -r Robotoy -I ./lib"
10
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "robotoy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "robotoy"
4
+ Robotoy::CommandParser.new.perform
5
+
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ move
2
+ tests
3
+ PLACE 0,0,NORTH
4
+ MOVE
5
+ MOVE
6
+ REPORT
7
+ RIGHT
8
+ MOVE
9
+ REPORT
10
+ LEFT
11
+ MOVE
12
+ RIGHT
13
+ RIGHT
14
+ MOVE
15
+ REPORT
16
+ RIGHT
17
+ MOVE
18
+ MOVE
19
+ MOVE
20
+ REPORT
21
+ PLACE 3,3,SOUTH
22
+ REPORT
23
+ MOVE
24
+ REPORT
@@ -0,0 +1,14 @@
1
+ require "robotoy/version"
2
+ require "robotoy/errors"
3
+ require "robotoy/services/orientation"
4
+ require "robotoy/services/move"
5
+ require "robotoy/services/place"
6
+ require "robotoy/services/report"
7
+ require "robotoy/table"
8
+ require "robotoy/robot"
9
+ require "robotoy/game"
10
+ require "robotoy/command_parser"
11
+
12
+ module Robotoy
13
+ # Your code goes here...
14
+ end
@@ -0,0 +1,29 @@
1
+ module Robotoy
2
+ class CommandParser
3
+ def initialize(game: Game.new, commands: ARGF)
4
+ @game = game
5
+ @commands = commands
6
+ end
7
+
8
+ def perform
9
+ @commands.each_line do |command|
10
+ begin
11
+ next if command.strip.empty?
12
+ puts command
13
+ set_params(command)
14
+ @game.perform(@method, @args)
15
+ rescue
16
+ puts "Something went wrong"
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def set_params(command)
24
+ splitted = command.split(/\s+/)
25
+ @method = splitted[0].strip
26
+ @args = splitted[1].split(',').map(&:strip) if splitted[1]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ module Robotoy
2
+ class Errors < StandardError; end
3
+ class NotValidOrientationError < Errors; end
4
+ class NotValidMoveError < Errors; end
5
+ class NotPlacedError < Errors; end
6
+ class NotValidMethodError < Errors; end
7
+ end
@@ -0,0 +1,51 @@
1
+ module Robotoy
2
+ class Game
3
+ def initialize(robot: Robot.new, table: Table.new, move: Services::Move, place: Services::Place, orientation: Services::Orientation, report: Services::Report)
4
+ @robot = robot
5
+ @table = table
6
+ @move = move
7
+ @place = place
8
+ @orientation = orientation
9
+ @report = report
10
+ end
11
+
12
+ def perform(method, *args)
13
+ send(method.downcase, *args.flatten)
14
+ @action.perform
15
+ rescue NotValidMoveError
16
+ puts "Not a valid place unfortunately"
17
+ rescue NotValidOrientationError
18
+ puts "Not a valid orientation unfortunately"
19
+ rescue NotPlacedError
20
+ puts "Not placed yet"
21
+ rescue NotValidMethodError
22
+ puts "Not a valid method"
23
+ end
24
+
25
+ private
26
+
27
+ def place(x, y, orient)
28
+ @action = @place.new(robot: @robot, table: @table, x: x, y: y, orient: orient)
29
+ end
30
+
31
+ def move(*_args)
32
+ @action = @move.new(robot: @robot, table: @table)
33
+ end
34
+
35
+ def left(*_args)
36
+ @action = @orientation.new(robot: @robot, side: :left)
37
+ end
38
+
39
+ def right(*_args)
40
+ @action = @orientation.new(robot: @robot, side: :right)
41
+ end
42
+
43
+ def report(*_args)
44
+ @action = @report.new(robot: @robot)
45
+ end
46
+
47
+ def method_missing(name, *args)
48
+ raise Robotoy::NotValidMethodError
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ module Robotoy
2
+ class Robot
3
+ attr_accessor :x, :y, :orientation, :orients
4
+
5
+ def initialize(orients: [:north, :east, :south, :west])
6
+ @orients = orients
7
+ end
8
+
9
+ def validate_if_placed
10
+ raise NotPlacedError if @x.nil? || @y.nil?
11
+ end
12
+
13
+ def validate_orientation(orientation:)
14
+ raise NotValidOrientationError unless @orients.include?(orientation.downcase)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ module Robotoy
2
+ module Services
3
+ class Move
4
+ def initialize(robot:, table:, x_range: 1, y_range: 1)
5
+ @robot = robot
6
+ @table = table
7
+ @x_range = x_range
8
+ @y_range = y_range
9
+ end
10
+
11
+ def perform
12
+ @robot.validate_if_placed
13
+ send(@robot.orientation)
14
+ end
15
+
16
+ private
17
+
18
+ def north
19
+ @table.validate_next_position(y: @robot.y + @y_range)
20
+ @robot.y += @y_range
21
+ end
22
+
23
+ def south
24
+ @table.validate_next_position(y: @robot.y - @y_range)
25
+ @robot.y -= @y_range
26
+ end
27
+
28
+ def east
29
+ @table.validate_next_position(x: @robot.x + @x_range)
30
+ @robot.x += @x_range
31
+ end
32
+
33
+ def west
34
+ @table.validate_next_position(x: @robot.x - @x_range)
35
+ @robot.x -= @x_range
36
+ end
37
+
38
+ def method_missing(name, *args)
39
+ raise Robotoy::NotValidMethodError
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,32 @@
1
+ module Robotoy
2
+ module Services
3
+ class Orientation
4
+ def initialize(robot:, side:)
5
+ @robot = robot
6
+ @side = side
7
+ order_orients
8
+ end
9
+
10
+ def perform
11
+ send(@side)
12
+ end
13
+
14
+ private
15
+
16
+ def right
17
+ @robot.orientation = @robot.orients.rotate[0]
18
+ end
19
+
20
+ def left
21
+ @robot.orientation = @robot.orients.rotate(-1)[0]
22
+ end
23
+
24
+ def order_orients
25
+ return unless @robot.orientation
26
+ index = @robot.orients.index(@robot.orientation)
27
+ @robot.orients.rotate!(index)
28
+ end
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,27 @@
1
+ module Robotoy
2
+ module Services
3
+ class Place
4
+ def initialize(robot:, table:, x:, y:, orient:)
5
+ @robot = robot
6
+ @table = table
7
+ @x = x.to_i
8
+ @y = y.to_i
9
+ @orient = orient.downcase.to_sym
10
+ end
11
+
12
+ def perform
13
+ validate_placement(@x, @y, @orient)
14
+ @robot.x = @x
15
+ @robot.y = @y
16
+ @robot.orientation = @orient
17
+ end
18
+
19
+ private
20
+
21
+ def validate_placement(x, y, orientation)
22
+ @table.validate_next_position(x: x, y: y)
23
+ @robot.validate_orientation(orientation: orientation)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ module Robotoy
2
+ module Services
3
+ class Report
4
+ def initialize(robot:)
5
+ @robot = robot
6
+ end
7
+
8
+ def perform(type: :string)
9
+ @robot.validate_if_placed
10
+ send(type)
11
+ end
12
+
13
+ private
14
+
15
+ def string
16
+ puts "#{@robot.x},#{@robot.y},#{@robot.orientation.upcase}"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module Robotoy
2
+ class Table
3
+ attr_reader :x, :y
4
+
5
+ def initialize(x: 5, y: 5)
6
+ @x = x
7
+ @y = y
8
+ end
9
+
10
+ def validate_next_position(x: 0, y: 0)
11
+ raise NotValidMoveError unless x.between?(0, @x) && y.between?(0, @y)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Robotoy
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "robotoy/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "robotoy"
8
+ spec.version = Robotoy::VERSION
9
+ spec.authors = ["hosseintoussi"]
10
+ spec.email = ["hossein@mindvalley.com"]
11
+
12
+ spec.summary = %q{Toy Robot Ruby}
13
+ spec.description = %q{This gem is a simulation of a toy robot moving on a square tabletop.}
14
+ spec.homepage = "https://github.com/hosseintoussi/robotoy"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = ["robotoy"]
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "pry"
25
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robotoy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hosseintoussi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: This gem is a simulation of a toy robot moving on a square tabletop.
70
+ email:
71
+ - hossein@mindvalley.com
72
+ executables:
73
+ - robotoy
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/robotoy
87
+ - bin/setup
88
+ - commands.txt
89
+ - lib/robotoy.rb
90
+ - lib/robotoy/command_parser.rb
91
+ - lib/robotoy/errors.rb
92
+ - lib/robotoy/game.rb
93
+ - lib/robotoy/robot.rb
94
+ - lib/robotoy/services/move.rb
95
+ - lib/robotoy/services/orientation.rb
96
+ - lib/robotoy/services/place.rb
97
+ - lib/robotoy/services/report.rb
98
+ - lib/robotoy/table.rb
99
+ - lib/robotoy/version.rb
100
+ - robotoy.gemspec
101
+ homepage: https://github.com/hosseintoussi/robotoy
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.5.1
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Toy Robot Ruby
125
+ test_files: []