lemmibot 0.2.0 → 0.2.1
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.travis.yml +4 -0
- data/README.md +7 -0
- data/lemmibot.gemspec +13 -11
- data/lib/lemmibot/bot.rb +52 -45
- data/lib/lemmibot/command_interface.rb +19 -8
- data/lib/lemmibot/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d125d14ad12b0139f69af46b5051534ab07d8395
|
4
|
+
data.tar.gz: cd9242fa6928f0db371708a91aa157b8e2ba3ea9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88f65fc237a1b5bc6daf3ba49a6b482b8da998beca9af597467d7db745521646498cbc049ecf0a1ebd7f9e33c146eb77ed2c5ad1b01d5bab2634aff4239db464
|
7
|
+
data.tar.gz: 5abc057800f629faa4870d9b88ab96a078a70247bf64f7528b41893f90957a3a9a7d757fbe6babd25324d87433a7e38d40e4a00b54682badfa5b694852cdd3da
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# Lemmibot
|
2
|
+
[](https://badge.fury.io/rb/lemmibot)
|
3
|
+
[](https://travis-ci.org/ineffyble/lemmibot)
|
2
4
|
|
3
5
|
A toy robot with better self-preservation instincts than a real lemming.
|
4
6
|
|
@@ -8,6 +10,11 @@ Install the gem from [RubyGems](https://rubygems.org/):
|
|
8
10
|
|
9
11
|
$ gem install lemmibot
|
10
12
|
|
13
|
+
### Compatibility
|
14
|
+
|
15
|
+
Lemmibot is continuously tested against the latest minor versions of Ruby 2.0 onwards,
|
16
|
+
and should run on any system running `ruby >= 2.0.0`.
|
17
|
+
|
11
18
|
## Usage
|
12
19
|
|
13
20
|
Once installed, start an interactive bot interface:
|
data/lemmibot.gemspec
CHANGED
@@ -5,21 +5,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
5
|
require 'lemmibot/version'
|
6
6
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
|
-
spec.name
|
9
|
-
spec.version
|
10
|
-
spec.authors
|
11
|
-
spec.email
|
8
|
+
spec.name = 'lemmibot'
|
9
|
+
spec.version = Lemmibot::VERSION
|
10
|
+
spec.authors = ['Effy Elden']
|
11
|
+
spec.email = ['git@effy.is']
|
12
12
|
|
13
|
-
spec.summary
|
14
|
-
spec.homepage
|
15
|
-
spec.license
|
13
|
+
spec.summary = 'A toy robot with better self-preservation instincts than a lemming'
|
14
|
+
spec.homepage = 'https://github.com/ineffyble/lemmibot'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
|
17
|
-
spec.
|
17
|
+
spec.required_ruby_version = '>= 2.0.0'
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
20
|
f.match(%r{^(test|spec|features)/})
|
19
21
|
end
|
20
|
-
spec.bindir
|
21
|
-
spec.executables
|
22
|
-
spec.require_paths
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
23
25
|
|
24
26
|
spec.add_development_dependency 'bundler', '~> 1.15'
|
25
27
|
spec.add_development_dependency 'rake', '~> 10.0'
|
data/lib/lemmibot/bot.rb
CHANGED
@@ -1,56 +1,13 @@
|
|
1
1
|
module Lemmibot
|
2
|
-
# A simulated toy robot on a
|
2
|
+
# A simulated toy robot on a tabletop
|
3
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
4
|
def initialize
|
11
|
-
# Set the bot's position when instantiated
|
12
5
|
@pos_x = 0
|
13
6
|
@pos_y = 0
|
14
7
|
@direction = :north
|
15
8
|
@placed = false
|
16
9
|
end
|
17
10
|
|
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
11
|
def turn(relative_direction)
|
55
12
|
# Rotate the bot 90 degrees to face another direction
|
56
13
|
# TODO: Find a nicer way to find the new direction
|
@@ -79,12 +36,62 @@ module Lemmibot
|
|
79
36
|
end
|
80
37
|
|
81
38
|
def place(x, y, direction)
|
82
|
-
#
|
39
|
+
# Place the bot at a specified position, facing specified direction
|
83
40
|
return false unless set_position(:x, x) &&
|
84
41
|
set_position(:y, y) &&
|
85
42
|
set_direction(direction)
|
86
43
|
@placed = true
|
87
44
|
true
|
88
45
|
end
|
46
|
+
|
47
|
+
def report
|
48
|
+
# Return a hash of the bot's current location and direction
|
49
|
+
return false unless @placed
|
50
|
+
{
|
51
|
+
x: @pos_x,
|
52
|
+
y: @pos_y,
|
53
|
+
dir: @direction
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
DIRECTIONS = %i[west north east south].freeze
|
60
|
+
|
61
|
+
def valid_direction?(direction)
|
62
|
+
DIRECTIONS.include? direction
|
63
|
+
end
|
64
|
+
|
65
|
+
def set_direction(direction)
|
66
|
+
# Set the bot to be facing a specified valid direction
|
67
|
+
return false unless valid_direction? direction
|
68
|
+
@direction = direction
|
69
|
+
true
|
70
|
+
end
|
71
|
+
|
72
|
+
def valid_position?(position)
|
73
|
+
# Check if a position is on the tabletop
|
74
|
+
position >= 0 && position <= 4
|
75
|
+
end
|
76
|
+
|
77
|
+
def set_position(axis, position)
|
78
|
+
# Set the bot to a specified valid position on specified axis
|
79
|
+
return false unless valid_position? position
|
80
|
+
case axis
|
81
|
+
when :x then @pos_x = position
|
82
|
+
when :y then @pos_y = position
|
83
|
+
end
|
84
|
+
true
|
85
|
+
end
|
86
|
+
|
87
|
+
def change_position(axis, value)
|
88
|
+
# Alter the bot's position by given value on given axis
|
89
|
+
new_position = if axis == :x
|
90
|
+
@pos_x + value
|
91
|
+
else
|
92
|
+
@pos_y + value
|
93
|
+
end
|
94
|
+
set_position(axis, new_position)
|
95
|
+
end
|
89
96
|
end
|
90
97
|
end
|
@@ -10,15 +10,20 @@ module Lemmibot
|
|
10
10
|
loop do
|
11
11
|
command = gets
|
12
12
|
return unless command
|
13
|
-
|
13
|
+
begin
|
14
|
+
process_command(command.chomp)
|
15
|
+
rescue
|
16
|
+
# Malformed and failed commands should be ignored
|
17
|
+
end
|
14
18
|
end
|
15
19
|
end
|
16
20
|
|
21
|
+
private
|
22
|
+
|
17
23
|
def process_command(command)
|
24
|
+
# Process a command string
|
18
25
|
case command.upcase
|
19
|
-
when /PLACE/
|
20
|
-
args = place_arguments(command)
|
21
|
-
@bot.place(args[:x], args[:y], args[:dir])
|
26
|
+
when /PLACE/ then place(command)
|
22
27
|
when /MOVE/ then @bot.move
|
23
28
|
when /LEFT/ then @bot.turn(:left)
|
24
29
|
when /RIGHT/ then @bot.turn(:right)
|
@@ -26,8 +31,8 @@ module Lemmibot
|
|
26
31
|
end
|
27
32
|
end
|
28
33
|
|
29
|
-
# Parses arguments for a PLACE command and returns them as a hash
|
30
34
|
def place_arguments(command)
|
35
|
+
# Parse arguments for a PLACE command and return them as a hash
|
31
36
|
params_string = command.split(' ')[1]
|
32
37
|
params = params_string.split(',')
|
33
38
|
{
|
@@ -37,10 +42,16 @@ module Lemmibot
|
|
37
42
|
}
|
38
43
|
end
|
39
44
|
|
40
|
-
|
45
|
+
def place(command)
|
46
|
+
args = place_arguments(command)
|
47
|
+
@bot.place(args[:x], args[:y], args[:dir])
|
48
|
+
end
|
49
|
+
|
41
50
|
def report
|
42
|
-
|
43
|
-
|
51
|
+
# Output a report of the bot's current position and direction
|
52
|
+
report = @bot.report
|
53
|
+
return unless report
|
54
|
+
puts "#{report[:x]},#{report[:y]},#{report[:dir].upcase}"
|
44
55
|
end
|
45
56
|
end
|
46
57
|
end
|
data/lib/lemmibot/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lemmibot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Effy Elden
|
@@ -119,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
119
|
requirements:
|
120
120
|
- - ">="
|
121
121
|
- !ruby/object:Gem::Version
|
122
|
-
version:
|
122
|
+
version: 2.0.0
|
123
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
124
|
requirements:
|
125
125
|
- - ">="
|