lemmibot 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: 6b106002c756748ac7852fdb8b8b616d2dd4f72f
4
+ data.tar.gz: dbd17187abff6a25faa338b4fd45fc3d6114688b
5
+ SHA512:
6
+ metadata.gz: 77ac2d3a783f676071c553ca15e1c2e3ae5948182260d31aeaa01dcb0c63bfaf064b6b8a89f870b4d52b23b5492162b8004cd673caae39285ce13e68ee41e1fc
7
+ data.tar.gz: fea7929b621aca80379f15ea6fde93f035574edf5ef23a5a5ce6ae7ed078be4468d9dce76f651bb799e8c22f62eb45f7a2a323e1ddae2f121257bdb12bd571ff
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ Metrics/BlockLength:
2
+ Exclude:
3
+ - 'spec/**/*.rb'
4
+ # Do not prefix writer method names with set_
5
+ Style/AccessorMethodName:
6
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in lemmibot.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Effy Elden
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,41 @@
1
+ # Lemmibot
2
+
3
+ A toy robot simulator with more self-preservation instincts than a real lemming.
4
+
5
+ ## Installation
6
+
7
+ Install the gem from [RubyGems](https://rubygems.org/):
8
+
9
+ $ gem install lemmibot
10
+
11
+ ## Usage
12
+
13
+ Once installed, start an interactive bot interface:
14
+
15
+ $ lemmibot
16
+
17
+ Congratulations! You've now created your very own Lemmibot.
18
+
19
+ The bot will accept the following commands:
20
+
21
+ * `PLACE X,Y,F` (e.g. `PLACE 2,1,NORTH`) - Places the bot at the specified coordinates on the table top, facing the specified position.
22
+ * `MOVE` - Moves the bot one unit in the currently facing direction.
23
+ * `LEFT` - Rotates the bot 90 degrees left, changing the faced direction but not moving the bot.
24
+ * `RIGHT` - Rotates the bot 90 degrees right, changing the faced direction but not moving the bot.
25
+ * `REPORT` - Outputs a report of the bot's current position and direction, in the format `0,1,NORTH`.
26
+
27
+ The bot must be placed before it will accept any other commands.
28
+
29
+ Any erroneous commands (including unrecognised commands and malformed commands) will be silently ignored.
30
+
31
+ Any commands that would result in this bot's untimely demise
32
+ (e.g. placing it off the edge of the table, or attempting to move the bot off the edge of the table)
33
+ will be silently ignored.
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ineffyble/lemmibot.
38
+
39
+ ## License
40
+
41
+ Lemmibot is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new(:cop)
7
+
8
+ task default: %i[spec cop]
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
data/exe/lemmibot ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'lemmibot'
4
+
5
+ interface = Lemmibot::CommandInterface.new
6
+ interface.start
data/lemmibot.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'lemmibot/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'lemmibot'
9
+ spec.version = Lemmibot::VERSION
10
+ spec.authors = ['Effy Elden']
11
+ spec.email = ['git@effy.is']
12
+
13
+ spec.summary = 'A toy robot simulator'
14
+ spec.homepage = 'https://github.com/ineffyble/lemmibot'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.15'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'rubocop', '~> 0.49'
28
+ end
@@ -0,0 +1,90 @@
1
+ module Lemmibot
2
+ # A simulated toy robot on a table top
3
+ class Bot
4
+ DIRECTIONS = %i[west north east south].freeze
5
+ attr_reader :pos_x
6
+ attr_reader :pos_y
7
+ attr_reader :direction
8
+ attr_reader :placed
9
+
10
+ def initialize
11
+ # Set the bot's position when instantiated
12
+ @pos_x = 0
13
+ @pos_y = 0
14
+ @direction = :north
15
+ @placed = false
16
+ end
17
+
18
+ def valid_direction?(direction)
19
+ DIRECTIONS.include? direction
20
+ end
21
+
22
+ def set_direction(direction)
23
+ # Set the bot to be facing a specified valid direction
24
+ return false unless valid_direction? direction
25
+ @direction = direction
26
+ true
27
+ end
28
+
29
+ def valid_position?(position)
30
+ position >= 0 && position <= 4
31
+ end
32
+
33
+ def set_position(axis, position)
34
+ # Set the bot to a specified valid position on specified axis
35
+ return false unless valid_position? position
36
+ if axis == :x
37
+ @pos_x = position
38
+ else
39
+ @pos_y = position
40
+ end
41
+ true
42
+ end
43
+
44
+ def change_position(axis, value)
45
+ # Alter the bot's position on the table top
46
+ new_position = if axis == :x
47
+ @pos_x + value
48
+ else
49
+ @pos_y + value
50
+ end
51
+ set_position(axis, new_position)
52
+ end
53
+
54
+ def turn(relative_direction)
55
+ # Rotate the bot 90 degrees to face another direction
56
+ # TODO: Find a nicer way to find the new direction
57
+ return false unless @placed
58
+ change = if relative_direction == :left
59
+ -1
60
+ else
61
+ 1
62
+ end
63
+ next_direction_index = DIRECTIONS.index(@direction) + change
64
+ next_direction_index = 0 if next_direction_index > DIRECTIONS.count - 1
65
+ new_direction = DIRECTIONS[next_direction_index]
66
+ set_direction(new_direction)
67
+ end
68
+
69
+ def move
70
+ # Move the bot one unit in the direction it is facing
71
+ # TODO: Find a more elegant solution for this
72
+ return false unless @placed
73
+ case @direction
74
+ when :north then return change_position(:y, 1)
75
+ when :south then return change_position(:y, -1)
76
+ when :east then return change_position(:x, 1)
77
+ when :west then return change_position(:x, -1)
78
+ end
79
+ end
80
+
81
+ def place(x, y, direction)
82
+ # Places the bot at a specified position, facing specified direction
83
+ return false unless set_position(:x, x) &&
84
+ set_position(:y, y) &&
85
+ set_direction(direction)
86
+ @placed = true
87
+ true
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,45 @@
1
+ module Lemmibot
2
+ # An interface for issuing commands to a Lemmibot via CLI/STDIN
3
+ class CommandInterface
4
+ def initialize
5
+ @bot = Lemmibot::Bot.new
6
+ end
7
+
8
+ def start
9
+ puts 'Lemmibot is awaiting your commands'
10
+ loop do
11
+ command = gets
12
+ return unless command
13
+ process_command(command.chomp)
14
+ end
15
+ end
16
+
17
+ def process_command(command)
18
+ case command.upcase
19
+ when /PLACE/
20
+ args = place_arguments(command)
21
+ @bot.place(args[:x], args[:y], args[:dir])
22
+ when /MOVE/ then @bot.move
23
+ when /LEFT/ then @bot.turn(:left)
24
+ when /RIGHT/ then @bot.turn(:right)
25
+ when /REPORT/ then report
26
+ end
27
+ end
28
+
29
+ # Parses arguments for a PLACE command and returns them as a hash
30
+ def place_arguments(command)
31
+ params_string = command.split(' ')[1]
32
+ params = params_string.split(',')
33
+ {
34
+ x: params[0].to_i,
35
+ y: params[1].to_i,
36
+ dir: params[2].downcase.to_sym
37
+ }
38
+ end
39
+
40
+ # Outputs a report of the bot's current position and direction
41
+ def report
42
+ puts "#{@bot.pos_x},#{@bot.pos_y},#{@bot.direction.upcase}"
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Lemmibot
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/lemmibot.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'lemmibot/version'
2
+ require 'lemmibot/bot'
3
+ require 'lemmibot/command_interface'
4
+
5
+ # A toy robot simulator
6
+ module Lemmibot
7
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lemmibot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Effy Elden
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-18 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.49'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.49'
69
+ description:
70
+ email:
71
+ - git@effy.is
72
+ executables:
73
+ - lemmibot
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/setup
86
+ - exe/lemmibot
87
+ - lemmibot.gemspec
88
+ - lib/lemmibot.rb
89
+ - lib/lemmibot/bot.rb
90
+ - lib/lemmibot/command_interface.rb
91
+ - lib/lemmibot/version.rb
92
+ homepage: https://github.com/ineffyble/lemmibot
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.12
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A toy robot simulator
116
+ test_files: []