robulator 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
+ SHA1:
3
+ metadata.gz: 5782980ac4a94cae445c3205039b189ca5237c2f
4
+ data.tar.gz: 6981570a6608d4d8b3c86baf198eacea74f68705
5
+ SHA512:
6
+ metadata.gz: 7f6382c4d8836bcd63040bfd1617f6fcd75a7fec992c7e0564ab95f8f96a49615fdbc7de19e546db55d494105cdd66ddba13c165c596a54c1b94c1c9d9805abd
7
+ data.tar.gz: 4301a37010aab6aeca7847ed7022e29f4d5687aae62e8fc29dbeb308f606f6ea5220e86fce48dd4b408ede2463f3035da3a1e3a543d85d87478b70a0f0ac4c7d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in robulator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ikhsan Maulana
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,112 @@
1
+ # Robulator
2
+ Robulator is a simulation of a toy robot moving on a square tabletop,
3
+ of dimensions 5 units x 5 units.
4
+
5
+ y
6
+ ^
7
+ | NORTH
8
+ |
9
+ 4 |---+---+---+---+
10
+ | | | | |
11
+ 3 |---+---+---+---+
12
+ | | | | |
13
+ WEST 2 |---+---+---+---+ EAST
14
+ | | | | |
15
+ 1 |---+---+---+---+
16
+ | | | | |
17
+ |---|---|---|---|----> x
18
+ 0 1 2 3 4
19
+
20
+ SOUTH
21
+
22
+
23
+ ## Installation
24
+ Install it with:
25
+
26
+ $ gem install robulator
27
+
28
+
29
+ ## Usage
30
+ Start the toy robot simulator with:
31
+
32
+ $ robulator
33
+
34
+ Robulator accept commands in the following form:
35
+
36
+ PLACE X,Y,F
37
+ MOVE
38
+ LEFT
39
+ RIGHT
40
+ REPORT
41
+
42
+ ### PLACE
43
+ Put the toy robot on the table in position `X`,`Y` and facing `NORTH`, `SOUTH`,
44
+ `EAST` or `WEST`.
45
+
46
+ * The origin (0,0) is the `SOUTH WEST` most corner.
47
+
48
+ * The first valid command to the robot is a `PLACE` command, after that,
49
+ any sequence of commands may be issued, in any order, including another
50
+ `PLACE` command. Robulator discard all commands in the sequence until a
51
+ valid `PLACE` command has been executed.
52
+
53
+ ### MOVE
54
+ Move the toy robot one unit forward in the direction it is currently facing.
55
+
56
+ ### LEFT and RIGHT
57
+ Rotate the robot 90 degrees in the specified direction without changing the
58
+ position of the robot.
59
+
60
+ ### REPORT
61
+ Announce the `X`,`Y` and the direction of the robot to standard output (e.g.
62
+ `2,4,SOUTH`).
63
+
64
+
65
+ ## Example Input and Output
66
+
67
+ >> PLACE 0,0,NORTH
68
+ >> MOVE
69
+ >> REPORT
70
+ 0,1,NORTH
71
+
72
+ >> PLACE 0,0,NORTH
73
+ >> LEFT
74
+ >> REPORT
75
+ 0,0,WEST
76
+
77
+ >> PLACE 1,2,EAST
78
+ >> MOVE
79
+ >> MOVE
80
+ >> LEFT
81
+ >> MOVE
82
+ >> REPORT
83
+ 3,3,NORTH
84
+
85
+
86
+ ## Conditions and Characteristics
87
+
88
+ * There are no other obstructions on the table surface.
89
+
90
+ * The robot is free to roam around the surface of the table but it ignore
91
+ commands that would cause the robot to fall, this also includes the initial
92
+ placement of the toy robot, however further valid commands will be accepted.
93
+
94
+ * A robot that is not on the table will ignore the `MOVE`, `LEFT`, `RIGHT` and
95
+ `REPORT` commands.
96
+
97
+ * Robulator does not provide any graphical output showing the movement of the
98
+ toy robot.
99
+
100
+
101
+ ## Development
102
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec robulator` to use the gem in this directory, ignoring other installed copies of this gem.
103
+
104
+ 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).
105
+
106
+
107
+ ## Contributing
108
+ Bug reports and pull requests are welcome on Bitbucket at https://bitbucket.org/ixandidu/robulator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
109
+
110
+
111
+ ## License
112
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "robulator"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exe/robulator ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "robulator"
4
+ require 'readline'
5
+
6
+ Readline.completion_append_character = ' '
7
+ Readline.completion_proc = proc do |s|
8
+ Robulator::Simulator::PERFORMABLE_COMMANDS.grep(/\A#{Regexp.escape(s)}/)
9
+ end
10
+
11
+ stty_save = `stty -g`.chomp
12
+ trap('INT') do
13
+ system('stty', stty_save)
14
+ exit
15
+ end
16
+
17
+ puts '-- Welcome to Robulator, a Toy Robot Simulator.'
18
+ print '-- Available commands are: '
19
+ print Robulator::Simulator::PERFORMABLE_COMMANDS[0..-2].join(', ')
20
+ print ' and '
21
+ print Robulator::Simulator::PERFORMABLE_COMMANDS[-1]
22
+ puts '.'
23
+
24
+ simulator = Robulator::Simulator.new
25
+ while line = Readline.readline('>> ', true)
26
+ simulator.perform line.chomp
27
+ end
data/lib/robulator.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "robulator/version"
2
+ require "robulator/robot"
3
+ require "robulator/simulator"
4
+ require "robulator/tabletop"
5
+
6
+ module Robulator
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,41 @@
1
+ module Robulator
2
+ class Robot
3
+ ORIENTATIONS = {
4
+ north: {move: [:y, 1], turns: {right: :east, left: :west}},
5
+ south: {move: [:y, -1], turns: {right: :west, left: :east}},
6
+ east: {move: [:x, 1], turns: {right: :south, left: :north}},
7
+ west: {move: [:x, -1], turns: {right: :north, left: :south}}
8
+ }.freeze
9
+ VALID_TURN = %i[right left].freeze
10
+
11
+ attr_reader :position, :direction
12
+
13
+ def place(x, y, direction)
14
+ direction = direction.to_sym.downcase unless direction.is_a?(Symbol)
15
+ return unless ORIENTATIONS.keys.include?(direction)
16
+
17
+ @position = {x: Integer(x), y: Integer(y)}
18
+ @direction = direction
19
+ rescue ArgumentError
20
+ end
21
+
22
+ def placed?
23
+ !!(direction && position)
24
+ end
25
+
26
+ def next_position
27
+ return unless placed?
28
+ axis, value = *ORIENTATIONS[direction][:move]
29
+ position.merge({axis => position[axis] + value})
30
+ end
31
+
32
+ def move
33
+ @position = next_position if next_position
34
+ end
35
+
36
+ def turn(relative_direction)
37
+ return if !placed? || !VALID_TURN.include?(relative_direction)
38
+ @direction = ORIENTATIONS[direction][:turns][relative_direction]
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,51 @@
1
+ require 'forwardable'
2
+
3
+ module Robulator
4
+ class Simulator
5
+ extend ::Forwardable
6
+
7
+ def_delegators :@robot,
8
+ :place,
9
+ :placed?,
10
+ :move,
11
+ :turn,
12
+ :position,
13
+ :direction,
14
+ :next_position
15
+
16
+ def_delegators :@tabletop,
17
+ :placeable_spot?
18
+
19
+ PERFORMABLE_COMMANDS = %w[PLACE MOVE RIGHT LEFT REPORT]
20
+
21
+ def initialize(robot=Robot.new, tabletop=Tabletop.new(5))
22
+ @robot = robot
23
+ @tabletop = tabletop
24
+ end
25
+
26
+ def perform(command)
27
+ command, *args = command.split(/\s|,/).reject(&:empty?)
28
+
29
+ case command
30
+ when 'PLACE'; place(*args) if args.size == 3 && safe_place?(*args)
31
+ when 'MOVE'; move if safe_to_move?
32
+ when 'RIGHT', 'LEFT'; turn(command.downcase.to_sym)
33
+ when 'REPORT'; send(command.downcase)
34
+ end
35
+ end
36
+
37
+ private
38
+ def report
39
+ return unless placed?
40
+ puts "#{position.values.join(',')},#{direction.upcase}"
41
+ end
42
+
43
+ def safe_place?(x, y, _direction)
44
+ placeable_spot?(x, y)
45
+ end
46
+
47
+ def safe_to_move?
48
+ placeable_spot?(next_position[:x], next_position[:y])
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ module Robulator
2
+ class Tabletop
3
+ def initialize(size)
4
+ size = Integer(size)
5
+ @placeable_spots = {x: 0...size, y: 0...size}
6
+ end
7
+
8
+ def placeable_spot?(x, y)
9
+ @placeable_spots[:x].include?(Integer(x)) &&
10
+ @placeable_spots[:y].include?(Integer(y))
11
+ rescue ArgumentError
12
+ false
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Robulator
2
+ VERSION = "0.1.0"
3
+ end
data/robulator.gemspec ADDED
@@ -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 'robulator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "robulator"
8
+ spec.version = Robulator::VERSION
9
+ spec.authors = ["Ikhsan Maulana"]
10
+ spec.email = ["ixandidu@gmail.com"]
11
+
12
+ spec.summary = %q{Toy Robot Simulation.}
13
+ spec.description = %q{Robulator is a simulation of a toy robot moving on a square tabletop.}
14
+ spec.homepage = "https://bitbucket.org/ixandidu/robulator"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.8"
25
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robulator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ikhsan Maulana
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-01 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.8'
55
+ description: Robulator is a simulation of a toy robot moving on a square tabletop.
56
+ email:
57
+ - ixandidu@gmail.com
58
+ executables:
59
+ - robulator
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - exe/robulator
73
+ - lib/robulator.rb
74
+ - lib/robulator/robot.rb
75
+ - lib/robulator/simulator.rb
76
+ - lib/robulator/tabletop.rb
77
+ - lib/robulator/version.rb
78
+ - robulator.gemspec
79
+ homepage: https://bitbucket.org/ixandidu/robulator
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.5.1
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Toy Robot Simulation.
103
+ test_files: []