toy_robot_sim 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: c48bc089f5ccd054f9a8614548e9e9b7dd0bad02
4
+ data.tar.gz: 999561783977bfe560ef08c15d41e957ead7eb07
5
+ SHA512:
6
+ metadata.gz: 01a6d6de23d9d102d7e21061bd8b611ffb5a55e12951c8d44fc903019c861ea5a8c31d0ade1f3c2fc0e266d768fb0d54fa6f141e372061868253f4b8a62f13c9
7
+ data.tar.gz: 3e280941d966f92c89750ffd0a30293844b4f58d2c7515b395e86c7cdef6fcd47135de45b4d22775a64c9ba46cde907c2cc472c46cc895c0e882684930f9d8fc
@@ -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,2 @@
1
+ --format documentation
2
+ --color
@@ -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_robot_sim.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Chris Odicho
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,39 @@
1
+ # ToyRobotSim
2
+
3
+ A Toy Robot Simulator written in Ruby 2.2.3
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'toy_robot_sim', github: 'ChrisOdicho/ToyRobotSim'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install toy_robot_sim -l https://github.com/ChrisOdicho/ToyRobotSim.git
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/toy_robot_sim. 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.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "toy_robot_sim"
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
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'toy_robot_sim'
4
+
5
+ ToyRobotSim::Cli::Application.start
@@ -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,3 @@
1
+ PLACE 0,0,NORTH
2
+ MOVE
3
+ REPORT
@@ -0,0 +1,2 @@
1
+ 1234
2
+ ABCD
@@ -0,0 +1,102 @@
1
+ require 'thor'
2
+
3
+ module ToyRobotSim
4
+ module Cli
5
+ class Application < Thor
6
+
7
+ desc 'start', 'Begin the toy robot simulation'
8
+
9
+ method_option :directory, desc: "Choose a directory to parse",
10
+ lazy_default: "docs/samples",
11
+ aliases: "d"
12
+
13
+ method_option :file, desc: "Choose a file to parse",
14
+ lazy_default: "docs/samples/instructions.txt",
15
+ aliases: "f"
16
+
17
+ method_option :interactive, desc: "Input instructions via terminal (PLACE x,y,f | MOVE | LEFT | RIGHT | REPORT ) ",
18
+ aliases: "i"
19
+
20
+ #############################################################################
21
+
22
+ def start
23
+ if options[:directory]
24
+ directory(options[:directory])
25
+ elsif options[:file]
26
+ file(options[:file])
27
+ elsif options[:interactive]
28
+ interactive
29
+ end
30
+ end
31
+
32
+ #############################################################################
33
+
34
+ private
35
+
36
+ def directory(path)
37
+ working_directory = Dir.pwd
38
+
39
+ if Dir.exists?(path)
40
+ Dir.chdir(path)
41
+
42
+ puts "Parsing directory #{path}..."
43
+ puts "-"*80
44
+
45
+ Dir.glob("*.txt") do |file|
46
+ ToyRobotSim::Parser.parse(file)
47
+ puts "-"*80
48
+ end
49
+
50
+ Dir.chdir(working_directory)
51
+ else
52
+ puts "Error: #{path} is an invalid directory"
53
+ end
54
+ end
55
+
56
+ def file(path)
57
+ if File.file?(path) && File.extname(path) == ".txt"
58
+ ToyRobotSim::Parser.parse(path)
59
+ else
60
+ puts "Error: #{path} is an invalid file"
61
+ end
62
+ end
63
+
64
+ def interactive
65
+
66
+ say("Create a table...")
67
+
68
+ width = ask("Width : ").to_i
69
+ height = ask("Height: ").to_i
70
+
71
+ while width <= 0 || height <= 0
72
+ say ("Table width and height need to be integers greater than zero")
73
+ width = ask("Width : ").to_i
74
+ height = ask("Height: ").to_i
75
+ end
76
+
77
+ table = ToyRobotSim::Table.new(width, height)
78
+ robot = ToyRobotSim::Robot.new(table)
79
+
80
+ say("Instruction List")
81
+ say( ("-"*80) )
82
+ say('PLACE X,Y,F | F can be NORTH, EAST, SOUTH, WEST')
83
+ say('MOVE | Move forward one step')
84
+ say('RIGHT | Rotate clockwise')
85
+ say('LEFT | Rotate anti-clockwise')
86
+ say('REPORT | Current location of the robot')
87
+ say('END | Stops the simulation')
88
+ say( ("-"*80) )
89
+ say("Input an instruction (non case sensitive)")
90
+ instruction = ask("Instruction: ")
91
+
92
+ while instruction.upcase != 'END'
93
+ ToyRobotSim::Parser.execute(robot, instruction.upcase)
94
+ instruction = ask("Instruction: ")
95
+ end
96
+
97
+ say("Toy Robot Simulation Ended", Thor::Shell::Color::GREEN)
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,12 @@
1
+ require "toy_robot_sim/version"
2
+ require "toy_robot_sim/table"
3
+ require "toy_robot_sim/robot"
4
+ require "toy_robot_sim/location"
5
+
6
+ require "toy_robot_sim/parser"
7
+
8
+ require "cli/application"
9
+
10
+ module ToyRobotSim
11
+
12
+ end
@@ -0,0 +1,33 @@
1
+ class ToyRobotSim::Location
2
+
3
+ attr_reader :x, :y
4
+
5
+ def initialize(x, y)
6
+ if x.respond_to?(:to_i) && y.respond_to?(:to_i)
7
+ @x = x.to_i
8
+ @y = y.to_i
9
+ else
10
+ @x = 0
11
+ @y = 0
12
+ end
13
+ end
14
+
15
+ #############################################################################
16
+
17
+ def north
18
+ ToyRobotSim::Location.new(x, y + 1)
19
+ end
20
+
21
+ def east
22
+ ToyRobotSim::Location.new(x + 1, y)
23
+ end
24
+
25
+ def south
26
+ ToyRobotSim::Location.new(x, y - 1)
27
+ end
28
+
29
+ def west
30
+ ToyRobotSim::Location.new(x - 1, y)
31
+ end
32
+
33
+ end
@@ -0,0 +1,51 @@
1
+ module ToyRobotSim::Parser
2
+
3
+ def self.parse(file_path)
4
+ if !File.file?(file_path)
5
+ puts "Error: File not found (#{file_path})"
6
+ elsif File.extname(file_path) != ".txt"
7
+ puts "Error: File not .txt (#{file_path})"
8
+ else
9
+
10
+ file = File.open(file_path)
11
+ puts "Parsing #{file.path}..."
12
+ table = ToyRobotSim::Table.new(5,5)
13
+ robot = ToyRobotSim::Robot.new(table)
14
+
15
+ file.each do |instruction|
16
+ execute(robot,instruction)
17
+ end
18
+ end
19
+ end
20
+
21
+ def self.execute(robot, instruction)
22
+ if instruction.start_with?('PLACE')
23
+ place(robot, instruction)
24
+ elsif instruction.start_with?('MOVE')
25
+ robot.move
26
+ elsif instruction.start_with?('LEFT')
27
+ robot.left
28
+ elsif instruction.start_with?('RIGHT')
29
+ robot.right
30
+ elsif instruction.start_with?('REPORT')
31
+ puts robot.report
32
+ else
33
+ puts "Error: Robot does not know what to do with instruction '#{instruction.chomp}'"
34
+ end
35
+ end
36
+
37
+ #############################################################################
38
+
39
+ private
40
+
41
+ def self.place(robot, instruction)
42
+ if instruction =~ /PLACE [0-9]{1},[0-9]{1},(NORTH|EAST|SOUTH|WEST)/
43
+ *xyf = instruction[6..-1].split(',')
44
+ location = ToyRobotSim::Location.new(xyf[0], xyf[1])
45
+ direction = xyf[2].chomp
46
+
47
+ robot.place(location, direction)
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,62 @@
1
+ class ToyRobotSim::Robot
2
+
3
+ DIRECTIONS = %w(NORTH EAST SOUTH WEST)
4
+
5
+ attr_reader :table, :location, :direction
6
+
7
+ #############################################################################
8
+
9
+ def initialize(table)
10
+ @table = table if table.is_a?(ToyRobotSim::Table)
11
+ end
12
+
13
+ #############################################################################
14
+
15
+ def place(location, direction)
16
+ if valid_move?(location, direction)
17
+ @location = location
18
+ @direction = direction
19
+ end
20
+ end
21
+
22
+ def move
23
+ if placed?
24
+ new_location = @location.send(@direction.downcase)
25
+ @location = new_location if valid_move?(new_location, @direction)
26
+ end
27
+ end
28
+
29
+ def left
30
+ turn(-1)
31
+ end
32
+
33
+ def right
34
+ turn(1)
35
+ end
36
+
37
+ def report
38
+ "#{@location.x},#{@location.y},#{@direction}" if placed?
39
+ end
40
+
41
+ #############################################################################
42
+
43
+ private
44
+
45
+ def turn(left_or_right)
46
+ if placed?
47
+ current_index = DIRECTIONS.index(@direction)
48
+ @direction = DIRECTIONS.rotate(left_or_right)[current_index]
49
+ end
50
+ end
51
+
52
+ def valid_move?(location, direction)
53
+ table.is_a?(ToyRobotSim::Table) &&
54
+ table.in_range?(location) &&
55
+ DIRECTIONS.include?(direction)
56
+ end
57
+
58
+ def placed?
59
+ (@location.nil? || @direction.nil?) ? false : true
60
+ end
61
+
62
+ end
@@ -0,0 +1,28 @@
1
+ class ToyRobotSim::Table
2
+
3
+ attr_reader :width, :height
4
+
5
+ def initialize(width, height)
6
+ @width = width
7
+ @height = height
8
+ end
9
+
10
+ #############################################################################
11
+
12
+ def in_range?(location)
13
+ width_in_range?(location.x) && height_in_range?(location.y)
14
+ end
15
+
16
+ #############################################################################
17
+
18
+ private
19
+
20
+ def width_in_range?(x)
21
+ x >= 0 && x <= (width - 1)
22
+ end
23
+
24
+ def height_in_range?(y)
25
+ y >= 0 && y <= (height - 1)
26
+ end
27
+
28
+ end
@@ -0,0 +1,3 @@
1
+ module ToyRobotSim
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 'toy_robot_sim/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "toy_robot_sim"
8
+ spec.version = ToyRobotSim::VERSION
9
+ spec.authors = ["Chris Odicho"]
10
+ spec.email = ["chris.odicho@gmail.com"]
11
+
12
+ spec.summary = "A Toy Robot Simulator written in Ruby 2.2.3"
13
+ spec.homepage = "https://github.com/ChrisOdicho/toy_robot_sim"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables = ["robot"]
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", " ~> 3.4"
24
+ spec.add_development_dependency "thor", "~> 0.18"
25
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toy_robot_sim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Odicho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-08 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: '3.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.18'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.18'
69
+ description:
70
+ email:
71
+ - chris.odicho@gmail.com
72
+ executables:
73
+ - robot
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/robot
87
+ - bin/setup
88
+ - docs/samples/instructions.txt
89
+ - docs/samples/invalid_instructions.txt
90
+ - lib/cli/application.rb
91
+ - lib/toy_robot_sim.rb
92
+ - lib/toy_robot_sim/location.rb
93
+ - lib/toy_robot_sim/parser.rb
94
+ - lib/toy_robot_sim/robot.rb
95
+ - lib/toy_robot_sim/table.rb
96
+ - lib/toy_robot_sim/version.rb
97
+ - toy_robot_sim.gemspec
98
+ homepage: https://github.com/ChrisOdicho/toy_robot_sim
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.6.6
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: A Toy Robot Simulator written in Ruby 2.2.3
122
+ test_files: []