tres_bot 0.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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tres_bot.gemspec
4
+ gemspec
5
+
6
+ gem 'circular_list'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tres Acton
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ # TresBot
2
+
3
+ A basic table top robot
4
+
5
+ ## Installation
6
+
7
+ Install from RubyGems.org
8
+
9
+ $ gem install tres_bot
10
+
11
+ Compile it yourself
12
+
13
+ $ gem build tres_bot.gemspec
14
+ $ gem install tres_bot-1.0.0
15
+
16
+ ## Usage
17
+
18
+ # TresBot.start
19
+ # command
20
+
21
+ ## Commands
22
+ The first command must follow the format:
23
+ PLACE 1,1,NORTH
24
+
25
+ Subsequent commands:
26
+ Left (turns left)
27
+ Right (turns right)
28
+ Move (moves one space in the direction the bot is facing)
29
+ Report (reports current position)
30
+
31
+ ## Run Tests
32
+
33
+ cd x/tres_bot
34
+ rake test
35
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
@@ -0,0 +1,99 @@
1
+ require "tres_bot/version"
2
+ require 'circular_list'
3
+
4
+ module TresBot
5
+
6
+
7
+ class Robot
8
+ attr_accessor :current_position
9
+ attr_accessor :facing
10
+ attr_accessor :is_on_the_table
11
+
12
+ def initialize
13
+ self.current_position = [0,0]
14
+ self.facing = 'NORTH'
15
+ self.is_on_the_table = false
16
+ end
17
+
18
+ def place(position)
19
+ self.is_on_the_table = true
20
+ self.current_position = [position[0],position[1]]
21
+ self.facing = position[2].upcase
22
+ end
23
+
24
+ def right
25
+ c = ::CircularList::List.new(['NORTH','EAST','SOUTH','WEST'])
26
+ self.facing = c.fetch_next(c.index(self.facing))
27
+ end
28
+
29
+ def left
30
+ c = ::CircularList::List.new(['NORTH','EAST','SOUTH','WEST'])
31
+ self.facing = c.fetch_previous(c.index(self.facing))
32
+ end
33
+
34
+ def compass(dir)
35
+ dir = dir.downcase
36
+ axis = ['north','south'].include?(dir) ? 'y':'x'
37
+ operator = ['north','east'].include?(dir) ? '+' : '-'
38
+ [axis,operator]
39
+ end
40
+
41
+ def move
42
+ calibration = self.compass(self.facing)
43
+ axis = calibration[0]
44
+ operator = calibration[1]
45
+ if axis == 'x'
46
+ unless (self.current_position[0] == 0 && operator == '-') || (self.current_position[0] == 4 && operator == '+')
47
+ self.current_position[0]=self.current_position[0].to_i.send(operator,1)
48
+ end
49
+ else
50
+ unless (self.current_position[1] == 0 && operator == '-') || (self.current_position[1] == 4 && operator == '+')
51
+ self.current_position[1]=self.current_position[1].to_i.send(operator,1)
52
+ end
53
+ end
54
+ end
55
+
56
+ def report
57
+ rep = "#{self.current_position.join(', ')}, #{self.facing}"
58
+ puts rep
59
+ rep
60
+ end
61
+
62
+ end
63
+
64
+ def self.place_command_valid?(command)
65
+ if command[1].nil?
66
+ return false
67
+ else
68
+ command[1].downcase.gsub(/\s+/, "").match(/[0-4]+[,]+[0-9]+[,]+[a-z]{4,5}/) ? true : false
69
+ end
70
+ end
71
+
72
+ def self.execute_command(robot,command)
73
+ @robot = robot
74
+ command = command.split(' ')
75
+
76
+ if command[0] != nil
77
+ if command[0].downcase == 'place'
78
+ if place_command_valid?(command)
79
+ full_position = command[1].split(',')
80
+ @robot.place(full_position)
81
+ else
82
+ puts "Your place command is invalid. Please try again."
83
+ end
84
+ elsif ['move', 'report','left','right'].include? command[0].downcase
85
+ @robot.is_on_the_table ? @robot.send(command[0].downcase) : (puts "Put me on the table!")
86
+ end
87
+ end
88
+ end
89
+
90
+ def self.start
91
+ puts 'Press Ctrl-C to exit'
92
+ @robot = Robot.new
93
+
94
+ until false == true
95
+ command = gets
96
+ execute_command(@robot,command)
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ module TresBot
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,62 @@
1
+ require 'test/unit'
2
+ require 'tres_bot'
3
+
4
+ class BotTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @robot = TresBot::Robot.new
8
+ end
9
+
10
+ def test_place
11
+ @robot.place([1,1,"NORTH"])
12
+ assert_equal(@robot.report, "1, 1, NORTH")
13
+ end
14
+
15
+ def test_left
16
+ @robot.place([1,1,"NORTH"])
17
+ @robot.left
18
+ assert_equal(@robot.report, "1, 1, WEST")
19
+ end
20
+
21
+ def test_right
22
+ @robot.place([1,1,"NORTH"])
23
+ @robot.right
24
+ assert_equal(@robot.report, "1, 1, EAST")
25
+ end
26
+
27
+ def test_move
28
+ @robot.place([1,1,"NORTH"])
29
+ @robot.move
30
+ @robot.report
31
+ assert_equal(@robot.report, "1, 2, NORTH")
32
+ end
33
+
34
+ def test_x_axis_end_boundary
35
+ @robot.place([4,1,"EAST"])
36
+ @robot.move
37
+ @robot.report
38
+ assert_equal(@robot.report, "4, 1, EAST")
39
+ end
40
+
41
+ def test_x_axis_start_boundary
42
+ @robot.place([0,1,"WEST"])
43
+ @robot.move
44
+ @robot.report
45
+ assert_equal(@robot.report, "0, 1, WEST")
46
+ end
47
+
48
+ def test_y_axis_end_boundary
49
+ @robot.place([3,4,"NORTH"])
50
+ @robot.move
51
+ @robot.report
52
+ assert_equal(@robot.report, "3, 4, NORTH")
53
+ end
54
+
55
+ def test_y_axis_start_boundary
56
+ @robot.place([3,0,"SOUTH"])
57
+ @robot.move
58
+ @robot.report
59
+ assert_equal(@robot.report, "3, 0, SOUTH")
60
+ end
61
+
62
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tres_bot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tres_bot"
8
+ spec.version = TresBot::VERSION
9
+ spec.authors = ["Tres Acton"]
10
+ spec.email = ["linx@castlemako.com"]
11
+ spec.description = %q{Tabletop Robot}
12
+ spec.summary = %q{Obeys simple commands: place,move,left,right,report}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "circular_list"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tres_bot
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Tres Acton
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2014-05-09 00:00:00 +10:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: circular_list
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 0
28
+ version: "0"
29
+ type: :runtime
30
+ version_requirements: *id001
31
+ - !ruby/object:Gem::Dependency
32
+ name: bundler
33
+ prerelease: false
34
+ requirement: &id002 !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ segments:
39
+ - 1
40
+ - 3
41
+ version: "1.3"
42
+ type: :development
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: rake
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :development
55
+ version_requirements: *id003
56
+ description: Tabletop Robot
57
+ email:
58
+ - linx@castlemako.com
59
+ executables: []
60
+
61
+ extensions: []
62
+
63
+ extra_rdoc_files: []
64
+
65
+ files:
66
+ - .gitignore
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - lib/tres_bot.rb
72
+ - lib/tres_bot/version.rb
73
+ - test/test_bot.rb
74
+ - tres_bot.gemspec
75
+ has_rdoc: true
76
+ homepage: ""
77
+ licenses:
78
+ - MIT
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.3.6
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: "Obeys simple commands: place,move,left,right,report"
105
+ test_files:
106
+ - test/test_bot.rb