jg_toy_robot 0.0.1

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: 1c7167a1fac92ac29bb48c7367ebce28e40ce034
4
+ data.tar.gz: bf45a10103625bc963d48ad4f36c62bdb5b5cd94
5
+ SHA512:
6
+ metadata.gz: c894feca2cefe54e17f300cc38d752bdd08a3f9f6f6ef3d9f2319893426c7fd2c7c78d1d299743f169c177afc9cb041866061c3a87590485bf7a9b6b99aacdcb
7
+ data.tar.gz: 9005d48b19e4e192b055009f2bccfd9fed614411d710067a9c01affae3cd70805d2d23b13917d2964a0e63d032edd18ea44417449b688c2d5be1c272fb6fba95
@@ -0,0 +1,77 @@
1
+ class ToyRobot
2
+ # A class to represent the orientation of a toy robot
3
+ class Orientation
4
+
5
+ # change in x value following a move in this orientation
6
+ attr_reader :dx
7
+
8
+ # change in y value following a move in this orientation
9
+ attr_reader :dy
10
+
11
+ VECTORS = {
12
+ NORTH: [0,1],
13
+ SOUTH: [0,-1],
14
+ EAST: [1,0],
15
+ WEST: [-1,0]
16
+ }
17
+
18
+ # Create a new Orientation. Must supply the name of the orientation (NORTH, SOUTH, EAST, or WEST) or a 2 element array
19
+ # containing dx and dy values, that can be 1, 0 or -1.
20
+ def initialize(key)
21
+ if key.is_a? String
22
+ vector = VECTORS[key.upcase.strip.to_sym]
23
+ raise ArgumentError, "supplied argument of #{key} is not a valid Orientation name - valid Orientation names are #{VECTORS.keys.join(', ')}" unless vector
24
+ @dx, @dy = vector
25
+ elsif key.is_a? Array
26
+ raise ArgumentError, "supplied orientation array has length of #{key.length} - an orientation array must have a length of two" unless key.size == 2
27
+ raise ArgumentError, "supplied orientation array has a dx value of #{key[0]} - valid values are 1, 0 or -1" unless [1,0,-1].include?(key[0])
28
+ raise ArgumentError, "supplied orientation array has a dy value of #{key[1]} - valid values are 1, 0 or -1" unless [1,0,-1].include?(key[1])
29
+ raise ArgumentError, "supplied orientation array has both dx and dy values of 0 - one of the values must be non-zero" if key[0] == 0 && key[1] == 0
30
+ raise ArgumentError, "supplied orientation array has both dx and dy values as non-zero - one of the values must be zero" if key[0] != 0 && key[1] != 0
31
+ @dx, @dy = key
32
+ else
33
+ raise ArgumentError, "supplied argument is of type #{key.class.name}. An orientation name or array is required"
34
+ end
35
+ end
36
+
37
+
38
+ # return a new Orientation that is 90 degrees to the left of self
39
+ def left
40
+ turn(-1)
41
+ end
42
+
43
+
44
+ # return a new Orientation that is 90 degrees to the right of self
45
+ def right
46
+ turn(1)
47
+ end
48
+
49
+
50
+ # return the name of the Orientation (NORTH, SOUTH, EAST, or WEST)
51
+ def to_s
52
+ inverted[ [@dx, @dy] ].to_s
53
+ end
54
+
55
+
56
+ private
57
+
58
+ # unified turn function, because maths
59
+ def turn(direction)
60
+ dx = @dx == 0 ? direction * @dy : 0
61
+ dy = @dy == 0 ? direction * -1 * @dx : 0
62
+ ToyRobot::Orientation.new( [ dx, dy] )
63
+ end
64
+
65
+
66
+ # caching method to return a vector that allows lookup of an Orientation name from it's dx and dy values
67
+ def inverted
68
+ @inverted ||= VECTORS.invert
69
+ end
70
+
71
+
72
+
73
+
74
+ end # class Orientation
75
+ end # class ToyRobot
76
+
77
+ curl -u jg_syd https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials
data/lib/toy_robot.rb ADDED
@@ -0,0 +1,13 @@
1
+ dir = File.dirname(__FILE__)
2
+ puts "dir is #{dir}"
3
+ $LOAD_PATH.unshift dir unless $LOAD_PATH.include?(dir)
4
+
5
+ require 'toy_robot/orientation'
6
+
7
+
8
+ # Class that simulates a Toy Robot
9
+ # Also functions as a module and namespace for related classes
10
+ class ToyRobot
11
+
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jg_toy_robot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Gray
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple toy robot simulator gem
14
+ email: foo@bar.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/toy_robot.rb
20
+ - lib/toy_robot/orientation.rb
21
+ homepage: http://rubygems.org/gems/jg_toy_robot
22
+ licenses:
23
+ - GPL-3.0
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.5.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A toy robot
45
+ test_files: []