my_toy_robot 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: 0ee0e15eafd201f40ffc6a3b08e460be9fc89949769c3927f6d18217ebef39d1
4
+ data.tar.gz: d8b0289d27d93149f9495eba007bbbfc84f9eab4bb28b97fbb0296aeb577af71
5
+ SHA512:
6
+ metadata.gz: 569fecb394fb5fd8a54dc2fbb360bb0c1b280de82a4ae195f789e03b564dfe662982c22c4c79d8a278a1f3e0302060ee4a118031e01ea2a6af9251599065c218
7
+ data.tar.gz: 906445bf5953468ea158e097a78ec6513555947ce123ac651c60d82acd2dcc918950797176ee2b44147b0f1b93ae49669819cc8114c1ed9b4e2a6036394d475b
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
4
+ --fail-fast
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in my_toy_robot.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.9.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ my_toy_robot (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (12.3.3)
11
+ rspec (3.9.0)
12
+ rspec-core (~> 3.9.0)
13
+ rspec-expectations (~> 3.9.0)
14
+ rspec-mocks (~> 3.9.0)
15
+ rspec-core (3.9.1)
16
+ rspec-support (~> 3.9.1)
17
+ rspec-expectations (3.9.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.9.0)
20
+ rspec-mocks (3.9.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-support (3.9.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ my_toy_robot!
30
+ rake (~> 12.0)
31
+ rspec (~> 3.9.0)
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Harrison Malone
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,40 @@
1
+ # MyToyRobot
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/my_toy_robot`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'my_toy_robot'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install my_toy_robot
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/my_toy_robot.
36
+
37
+
38
+ ## License
39
+
40
+ 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,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 "my_toy_robot"
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(__FILE__)
data/bin/setup ADDED
@@ -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
data/commands.txt ADDED
@@ -0,0 +1,10 @@
1
+ PLACE 1,2,NORTH
2
+ MOVE
3
+ MOVE
4
+ MOVE
5
+ MOVE
6
+ MOVE
7
+ MOVE
8
+ MOVE
9
+ MOVE
10
+ REPORT
data/exe/my_toy_robot ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'my_toy_robot'
4
+
5
+ cli = MyToyRobot::CLI.new
6
+ commands = cli.load_commands(ARGV[0])
7
+ cli.run(commands)
@@ -0,0 +1,21 @@
1
+ module MyToyRobot
2
+ class CLI
3
+ attr_reader :simulator
4
+ def initialize
5
+ table = MyToyRobot::Table.new(5, 5)
6
+ @simulator = MyToyRobot::Simulator.new(table)
7
+ end
8
+
9
+ def load_commands(file)
10
+ File.readlines(file).map do |command|
11
+ MyToyRobot::Command.process(command)
12
+ end
13
+ end
14
+
15
+ def run(commands)
16
+ commands.each do |command, *args|
17
+ simulator.send(command, *args)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ module MyToyRobot
2
+ class Command
3
+ def self.get_match_obj(command)
4
+ place_match = /\APLACE (?<x>\d+),(?<y>\d+),(?<direction>\w+)\Z/.match(command)
5
+ move_match = /\AMOVE\Z/.match(command)
6
+ left_match = /\ALEFT\Z/.match(command)
7
+ right_match = /\ARIGHT\Z/.match(command)
8
+ report_match = /\AREPORT\Z/.match(command)
9
+ [place_match, move_match, left_match, right_match, report_match].compact[0]
10
+ end
11
+
12
+ def self.command_type(command)
13
+ case true
14
+ when command.include?("PLACE") then :place
15
+ when command.include?("MOVE") then :move
16
+ when command.include?("LEFT") then :turn_left
17
+ when command.include?("RIGHT") then :turn_right
18
+ when command.include?("REPORT") then :report
19
+ end
20
+ end
21
+
22
+ def self.process(command)
23
+ match = self.get_match_obj(command)
24
+ unless match
25
+ return [:invalid, command]
26
+ end
27
+ type = self.command_type(command)
28
+ if type == :place
29
+ [type, match[:x].to_i, match[:y].to_i, match[:direction]]
30
+ else
31
+ [type]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,68 @@
1
+ module MyToyRobot
2
+ class Robot
3
+ attr_reader :east, :north, :direction
4
+ DIRECTIONS = ["NORTH", "EAST", "SOUTH", "WEST"]
5
+ def initialize(east = 0, north = 0, direction = "NORTH")
6
+ @east = east
7
+ @north = north
8
+ @direction = direction
9
+ end
10
+
11
+ def move_east
12
+ @east += 1
13
+ end
14
+
15
+ def move_west
16
+ @east -= 1
17
+ end
18
+
19
+ def move_north
20
+ @north += 1
21
+ end
22
+
23
+ def move_south
24
+ @north -= 1
25
+ end
26
+
27
+ def move
28
+ send("move_#{@direction.downcase}")
29
+ end
30
+
31
+ def turn_left
32
+ turn(:left)
33
+ end
34
+
35
+ def turn_right
36
+ turn(:right)
37
+ end
38
+
39
+ def report
40
+ {
41
+ east: @east,
42
+ north: @north,
43
+ direction: @direction
44
+ }
45
+ end
46
+
47
+ def next_move
48
+ case @direction
49
+ when "NORTH"
50
+ [@east, @north + 1]
51
+ when "SOUTH"
52
+ [@east, @north - 1]
53
+ when "EAST"
54
+ [@east + 1, @north]
55
+ when "WEST"
56
+ [@east - 1, @north]
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def turn(turn_direction)
63
+ index = DIRECTIONS.index(@direction)
64
+ rotations = turn_direction == :right ? 1 : -1
65
+ @direction = DIRECTIONS.rotate(rotations)[index]
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,43 @@
1
+ module MyToyRobot
2
+ class Simulator
3
+ attr_reader :robot
4
+ def initialize(table)
5
+ @table = table
6
+ end
7
+
8
+ def place(east, north, facing)
9
+ return unless @table.valid_location?(east, north)
10
+ @robot = Robot.new(east, north, facing)
11
+ end
12
+
13
+ def move
14
+ return unless robot_placed?
15
+ return unless @table.valid_location?(*robot.next_move)
16
+ robot.move
17
+ end
18
+
19
+ def turn_left
20
+ return unless robot_placed?
21
+ robot.turn_left
22
+ end
23
+
24
+ def turn_right
25
+ return unless robot_placed?
26
+ robot.turn_right
27
+ end
28
+
29
+ def invalid(command)
30
+ puts "'#{command.strip}' is an invalid command"
31
+ end
32
+
33
+ def report
34
+ return unless robot_placed?
35
+ position = robot.report
36
+ puts "Robot is currently at (#{position[:east]}, #{position[:north]}) and it\'s facing #{position[:direction]}"
37
+ end
38
+
39
+ def robot_placed?
40
+ !robot.nil?
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,12 @@
1
+ module MyToyRobot
2
+ class Table
3
+ def initialize(width, length)
4
+ @width = width
5
+ @length = length
6
+ end
7
+
8
+ def valid_location?(east, north)
9
+ east >= 0 && east < @width && north >= 0 && north < @length
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module MyToyRobot
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "my_toy_robot/version"
2
+ require_relative "my_toy_robot/robot"
3
+ require_relative "my_toy_robot/table"
4
+ require_relative "my_toy_robot/simulator"
5
+ require_relative "my_toy_robot/command"
6
+ require_relative "my_toy_robot/cli"
@@ -0,0 +1,19 @@
1
+ require_relative 'lib/my_toy_robot/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "my_toy_robot"
5
+ spec.version = MyToyRobot::VERSION
6
+ spec.authors = ["Harrison Malone"]
7
+ spec.email = ["harrison.malone2@gmail.com"]
8
+ spec.summary = "Finally finished reading Toy Robot!"
9
+ spec.description = "Followed the book step by step and reproduced the functionality"
10
+ spec.homepage = "https://github.com/harrisonmalone/my_toy_robot"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
14
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ end
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: my_toy_robot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Harrison Malone
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Followed the book step by step and reproduced the functionality
14
+ email:
15
+ - harrison.malone2@gmail.com
16
+ executables:
17
+ - my_toy_robot
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - commands.txt
32
+ - exe/my_toy_robot
33
+ - lib/my_toy_robot.rb
34
+ - lib/my_toy_robot/cli.rb
35
+ - lib/my_toy_robot/command.rb
36
+ - lib/my_toy_robot/robot.rb
37
+ - lib/my_toy_robot/simulator.rb
38
+ - lib/my_toy_robot/table.rb
39
+ - lib/my_toy_robot/version.rb
40
+ - my_toy_robot.gemspec
41
+ homepage: https://github.com/harrisonmalone/my_toy_robot
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.1.2
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Finally finished reading Toy Robot!
64
+ test_files: []