toy_robo_simulator 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: 8d36581000b8d9c29ed537e9dd0f387fb9916e76
4
+ data.tar.gz: dd370f74e8875bdd316da77319ca0272a3f1b2d9
5
+ SHA512:
6
+ metadata.gz: e9220146ac4c361c80413c1eecf03dbafb5b52f39101491fd2b4c660fb5957a6141d8ac77ad2dd0482bb9995219cfd5021f4f81b23ebb91bb0c9c461b836ec98
7
+ data.tar.gz: b5fce97d98b20109882deb14a82ea1f2d397c7e100422c038f475f669195c669b2a4a5dfff2cdad30160135b3b0fec7fbe537b5eecce3da628080dd0e4e3eb7f
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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bin/**/*'
4
+ - '*.gemspec'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.3
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 toy_robo_simulator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Adler
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,34 @@
1
+ # Toy Robot Simulator
2
+
3
+ ## Installation
4
+
5
+ Run:
6
+
7
+ ```
8
+ gem install toy_robo_simulator
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ## Development Specs
14
+
15
+ - [ ] Create a 5x5 tabletop when initated
16
+ - [ ] The (0,0) is the south west corner
17
+ - [ ] The robot cannot move out of range
18
+ - [ ] Available Commands
19
+ - [ ] PLACE X, Y, F: Place the robot on a specific position with direction
20
+ - [ ] MOVE: move forward one space
21
+ - [ ] LEFT: turn left
22
+ - [ ] RIGHT: turn right
23
+ - [ ] REPORT: report current position and direction
24
+ - [ ] PLACE should be the first command, otherwise ignored
25
+ - [ ] Allow both CLI input and file input
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/toy_robo_simulator. 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.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
34
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "toy_robo_simulator"
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/robo ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'toy_robo_simulator'
4
+
5
+ ToyRoboSimulator::Console.new.watch
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
@@ -0,0 +1,59 @@
1
+ module ToyRoboSimulator
2
+ # The console is responsible for initaing a command line interface for
3
+ # users to access the program.
4
+ class Console
5
+ def initialize
6
+ puts ::MESSAGE
7
+ @n = 0
8
+ @robo = Robo.new
9
+ print "#{format('%02d', @n)} > "
10
+ end
11
+
12
+ def watch
13
+ command = STDIN.gets
14
+ while command
15
+ run(command)
16
+ print "#{format('%02d', @n += 1)} > "
17
+ command = STDIN.gets
18
+ end
19
+ end
20
+
21
+ def run(command)
22
+ args = command.split(' ')
23
+ action = args[0].gsub("\n", '').downcase
24
+ case
25
+ when ::AVAILABLE_COMMANDS.include?(action)
26
+ process(action, args)
27
+ when action == 'exit'
28
+ exit_program
29
+ when action == 'help'
30
+ help
31
+ else
32
+ puts ::WARNING
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def process(action, args)
39
+ begin
40
+ @robo.send(action.to_sym, *args[1..-1])
41
+ rescue ArgumentError => e
42
+ tip if e.message.include? 'wrong number of arguments'
43
+ end
44
+ end
45
+
46
+ def tip
47
+ puts TIP
48
+ end
49
+
50
+ def exit_program
51
+ puts "\nThank You!\n\n"
52
+ exit
53
+ end
54
+
55
+ def help
56
+ puts HELP
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,23 @@
1
+ require 'colorize'
2
+
3
+ AVAILABLE_COMMANDS = %w(place move left right report)
4
+
5
+ HELP = "Available Commands:
6
+ - PLACE: Place a robo. Requires x, y, and orientation arguments.
7
+ e.g. PLACE 2 5 NORTH
8
+ - MOVE: Move forward
9
+ - LEFT: Turn left
10
+ - RIGHT: Turn right
11
+ - REPORT: Report current position and orientation
12
+ - HELP: Display all available commands"
13
+
14
+ MESSAGE = "\n#{'Welcome to Toy Robo Simulator!'.colorize(:green)}
15
+ \n#{HELP}\nUse PLACE first to start the simulation :)\n\n"
16
+
17
+ WARNING = "Command Not Found. See 'HELP'."
18
+
19
+ TIP = "#{'WARNING'.colorize(:red)}
20
+ - PLACE should take 3 arguments: x, y, and orientation. e.g. PLACE 1 2 NORTH
21
+ - Other commands take no argument."
22
+
23
+ ORIENTATIONS = %i(north west south east)
@@ -0,0 +1,62 @@
1
+ module ToyRoboSimulator
2
+ class Robo
3
+ include Validator
4
+ attr_accessor :x, :y, :orientation
5
+
6
+ def initialize
7
+ @table = Table.new(5, 5)
8
+ @errors = []
9
+ end
10
+
11
+ def place(x, y, orientation)
12
+ validate_placement(x, y, orientation)
13
+ warning && return if @errors.any?
14
+ @x, @y = x.to_i, y.to_i
15
+ @orientation = orientation.downcase.to_sym
16
+ puts 'It is placed.'
17
+ end
18
+
19
+ def move
20
+ validate_if_placed
21
+ validate_movement
22
+ warning && return if @errors.any?
23
+ case orientation
24
+ when :north then @y += 1
25
+ when :east then @x += 1
26
+ when :south then @y -= 1
27
+ when :west then @x -= 1
28
+ end
29
+ puts 'It moves forward.'
30
+ end
31
+
32
+ def left
33
+ validate_if_placed
34
+ warning && return if @errors.any?
35
+ index = ::ORIENTATIONS.index(@orientation) + 1
36
+ @orientation = ::ORIENTATIONS[index] || :north
37
+ puts 'It turns left.'
38
+ end
39
+
40
+ def right
41
+ validate_if_placed
42
+ warning && return if @errors.any?
43
+ index = ::ORIENTATIONS.index(@orientation) - 1
44
+ @orientation = ::ORIENTATIONS[index]
45
+ puts 'It turns right'
46
+ end
47
+
48
+ def report
49
+ validate_if_placed
50
+ warning && return if @errors.any?
51
+ puts "Robo is now at (#{@x},#{@y}) facing #{@orientation.to_s.upcase}"
52
+ end
53
+
54
+ private
55
+
56
+ def warning
57
+ @errors.each { |message| puts message }
58
+ @errors = []
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ module ToyRoboSimulator
2
+ class Table
3
+ attr_accessor :x, :y
4
+ def initialize(x, y)
5
+ @x = x
6
+ @y = y
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ module ToyRoboSimulator
2
+ module Validator
3
+ def validate_placement(x, y, orientation)
4
+ @errors << 'X must be a number' unless int?(x)
5
+ @errors << 'Y must be a number' unless int?(y)
6
+ @errors << "X must be between 0 and #{@table.x}" unless in_range?(x, @table.x)
7
+ @errors << "Y must be between 0 and #{@table.y}" unless in_range?(y, @table.y)
8
+ @errors << 'Orientation should be either NORTH, SOUTH, EAST, or WEST' unless orientation_valid?(orientation)
9
+ end
10
+
11
+ def validate_movement
12
+ @errors << 'The Robo is at edge. No further move is allowed.' if at_table_edge?
13
+ end
14
+
15
+ def validate_if_placed
16
+ @errors << 'The Robo is not placed yet. Use PLACE command first.' unless @x && @y && @orientation
17
+ end
18
+
19
+ def at_table_edge?
20
+ (@orientation == :north && @y == @table.y) ||
21
+ (@orientation == :east && @x == @table.x) ||
22
+ (@orientation == :south && @y == 0) ||
23
+ (@orientation == :west && @x == 0)
24
+ end
25
+
26
+ private
27
+
28
+ def int?(value)
29
+ value.to_s == value.to_i.to_s
30
+ end
31
+
32
+ def in_range?(value, max)
33
+ value.to_i >= 0 && value.to_i <= max
34
+ end
35
+
36
+ def orientation_valid?(orientation)
37
+ ::ORIENTATIONS.include? orientation.downcase.to_sym
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module ToyRoboSimulator
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'toy_robo_simulator/version'
2
+ require_relative 'toy_robo_simulator/env'
3
+ require_relative 'toy_robo_simulator/table'
4
+ require_relative 'toy_robo_simulator/validator'
5
+ require_relative 'toy_robo_simulator/robo'
6
+ require_relative 'toy_robo_simulator/console'
7
+
8
+ # This module includes all other modules and classes in this program.
9
+ # Basically it can be divided into:
10
+ #
11
+ # - Console: Offering a command line interface for input & output.
12
+ # - Robo: A robot simulating the movement on a table.
13
+ # - Validator: Validating the robo's actions and their prerequisities.
14
+ # - Others
15
+ #
16
+ module ToyRoboSimulator
17
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'toy_robo_simulator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "toy_robo_simulator"
8
+ spec.version = ToyRoboSimulator::VERSION
9
+ spec.authors = ["Adler"]
10
+ spec.email = ["nkj20932@hotmail.com"]
11
+
12
+ spec.summary = %q{A toy robot simulator}
13
+ spec.description = %q{A CLI that simulates the movement of a robot on a 5x5 table}
14
+ spec.homepage = "http://github.com/adlerhsieh/toy_rubo_simulator"
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 "rspec"
25
+ spec.add_development_dependency "colorize"
26
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toy_robo_simulator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adler
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-10 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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
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: A CLI that simulates the movement of a robot on a 5x5 table
70
+ email:
71
+ - nkj20932@hotmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".ruby-version"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/robo
88
+ - bin/setup
89
+ - lib/toy_robo_simulator.rb
90
+ - lib/toy_robo_simulator/console.rb
91
+ - lib/toy_robo_simulator/env.rb
92
+ - lib/toy_robo_simulator/robo.rb
93
+ - lib/toy_robo_simulator/table.rb
94
+ - lib/toy_robo_simulator/validator.rb
95
+ - lib/toy_robo_simulator/version.rb
96
+ - toy_robo_simulator.gemspec
97
+ homepage: http://github.com/adlerhsieh/toy_rubo_simulator
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.5.1
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: A toy robot simulator
121
+ test_files: []