another_toy_robot 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/lib/toy_robot/client.rb +1 -1
- data/lib/toy_robot/command.rb +11 -5
- data/lib/toy_robot/direction.rb +11 -15
- data/lib/toy_robot/null_position.rb +18 -0
- data/lib/toy_robot/position.rb +3 -25
- data/lib/toy_robot/version.rb +1 -1
- data/lib/toy_robot.rb +6 -5
- data/toy_robot.gemspec +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8d1128303c93b1075d61df54fa152297c37f8d3
|
4
|
+
data.tar.gz: f7eb58a07a7333905b4655d9032c3fdd83d7aae8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7360190bc39b05b0ba27907ba6174cc0c683dfd9d4fd85436297568643f9634eddacdb5759d84450bbd2a96db3fc482d6bfb3a303cfc5361731ce862e56ed5d4
|
7
|
+
data.tar.gz: bf0b75a09de010a969ce596a2f839fb0802e4828f91c048554aa501c6bc13f18721149bc2f78099566b376656dfe02bd8ae381ab56dbba06e6446c66a65fd0a4
|
data/README.md
CHANGED
@@ -7,6 +7,9 @@ The application is a simulation of a toy robot moving on a 5 x 5 unit tabletop.
|
|
7
7
|
is an example of a well tested, object oriented design, employing the command design
|
8
8
|
pattern.
|
9
9
|
|
10
|
+
### Dependencies
|
11
|
+
Requires Ruby 2.3 or later.
|
12
|
+
|
10
13
|
### Installation
|
11
14
|
```
|
12
15
|
gem install another_toy_robot
|
@@ -26,13 +29,14 @@ Valid commands are:
|
|
26
29
|
|
27
30
|
| Command | Description
|
28
31
|
| ------------- | ---
|
29
|
-
| `place x,y,d` | Places robot at position `x`, `y`, facing cardinal direction `d`. E.g. `place 1,2,
|
32
|
+
| `place x,y,d` | Places robot at position `x`, `y`, facing cardinal direction `d`. E.g. `place 1,2,north`.
|
30
33
|
| `left` | Rotates robot 90° counter-clockwise.
|
31
34
|
| `right` | Rotates robot 90° clockwise.
|
32
35
|
| `move` | Advances the robot one position in the direction it is currently facing.
|
33
36
|
| `report` | Prints the current location.
|
37
|
+
| `exit` | Closes the application.
|
34
38
|
|
35
|
-
Commands resulting in the robot moving to an out-of-bounds position (`x` or `y` being
|
39
|
+
Commands resulting in the robot moving to an out-of-bounds position (`x` or `y` being less than 0 or greater than 4) will be ignored.
|
36
40
|
|
37
41
|
## Specification
|
38
42
|
|
data/lib/toy_robot/client.rb
CHANGED
data/lib/toy_robot/command.rb
CHANGED
@@ -5,13 +5,21 @@ class PlaceCommand
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def execute
|
8
|
-
@robot.place
|
9
|
-
|
10
|
-
|
8
|
+
@robot.place Position.new(x_coord: x_coord,
|
9
|
+
y_coord: y_coord,
|
10
|
+
direction: direction)
|
11
11
|
end
|
12
12
|
|
13
13
|
private
|
14
14
|
|
15
|
+
def x_coord
|
16
|
+
@params[0].to_i
|
17
|
+
end
|
18
|
+
|
19
|
+
def y_coord
|
20
|
+
@params[1].to_i
|
21
|
+
end
|
22
|
+
|
15
23
|
def direction
|
16
24
|
case @params[2]
|
17
25
|
when "n", "north" then North.new
|
@@ -63,8 +71,6 @@ class ReportCommand
|
|
63
71
|
end
|
64
72
|
|
65
73
|
class InvalidCommand
|
66
|
-
def intialize; end
|
67
|
-
|
68
74
|
def execute
|
69
75
|
puts "Invalid command"
|
70
76
|
end
|
data/lib/toy_robot/direction.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
-
class
|
2
|
-
attr_reader :x_displacement, :y_displacement
|
1
|
+
class Direction
|
2
|
+
attr_reader :x_displacement, :y_displacement
|
3
3
|
|
4
|
+
def to_s
|
5
|
+
self.class.to_s.downcase
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class North < Direction
|
4
10
|
def initialize
|
5
11
|
@x_displacement = 0
|
6
12
|
@y_displacement = 1
|
7
|
-
@to_s = "north"
|
8
13
|
end
|
9
14
|
|
10
15
|
def left
|
@@ -16,13 +21,10 @@ class North
|
|
16
21
|
end
|
17
22
|
end
|
18
23
|
|
19
|
-
class East
|
20
|
-
attr_reader :x_displacement, :y_displacement, :to_s
|
21
|
-
|
24
|
+
class East < Direction
|
22
25
|
def initialize
|
23
26
|
@x_displacement = 1
|
24
27
|
@y_displacement = 0
|
25
|
-
@to_s = "east"
|
26
28
|
end
|
27
29
|
|
28
30
|
def left
|
@@ -34,13 +36,10 @@ class East
|
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
37
|
-
class South
|
38
|
-
attr_reader :x_displacement, :y_displacement, :to_s
|
39
|
-
|
39
|
+
class South < Direction
|
40
40
|
def initialize
|
41
41
|
@x_displacement = 0
|
42
42
|
@y_displacement = -1
|
43
|
-
@to_s = "south"
|
44
43
|
end
|
45
44
|
|
46
45
|
def left
|
@@ -52,13 +51,10 @@ class South
|
|
52
51
|
end
|
53
52
|
end
|
54
53
|
|
55
|
-
class West
|
56
|
-
attr_reader :x_displacement, :y_displacement, :to_s
|
57
|
-
|
54
|
+
class West < Direction
|
58
55
|
def initialize
|
59
56
|
@x_displacement = -1
|
60
57
|
@y_displacement = 0
|
61
|
-
@to_s = "west"
|
62
58
|
end
|
63
59
|
|
64
60
|
def left
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class NullPosition
|
2
|
+
attr_reader :x_coord, :y_coord, :direction, :to_s
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@x_coord = nil
|
6
|
+
@y_coord = nil
|
7
|
+
@direction = nil
|
8
|
+
@to_s = "No position"
|
9
|
+
end
|
10
|
+
|
11
|
+
def advance
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def turn(*)
|
16
|
+
self
|
17
|
+
end
|
18
|
+
end
|
data/lib/toy_robot/position.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
class
|
1
|
+
class Position
|
2
2
|
attr_reader :x_coord, :y_coord, :direction
|
3
3
|
|
4
4
|
def initialize(x_coord: 0, y_coord: 0, direction: North.new)
|
@@ -10,11 +10,11 @@ class RealPosition
|
|
10
10
|
def advance
|
11
11
|
x = @x_coord + @direction.x_displacement
|
12
12
|
y = @y_coord + @direction.y_displacement
|
13
|
-
|
13
|
+
Position.new x_coord: x, y_coord: y, direction: @direction
|
14
14
|
end
|
15
15
|
|
16
16
|
def turn(hand_side)
|
17
|
-
|
17
|
+
Position.new(x_coord: @x_coord,
|
18
18
|
y_coord: @y_coord,
|
19
19
|
direction: @direction.send(hand_side))
|
20
20
|
end
|
@@ -23,25 +23,3 @@ class RealPosition
|
|
23
23
|
"#{@x_coord}, #{@y_coord}, #{@direction}"
|
24
24
|
end
|
25
25
|
end
|
26
|
-
|
27
|
-
class NullPosition
|
28
|
-
attr_reader :x_coord, :y_coord, :direction
|
29
|
-
|
30
|
-
def initialize
|
31
|
-
@x_coord = nil
|
32
|
-
@y_coord = nil
|
33
|
-
@direction = nil
|
34
|
-
end
|
35
|
-
|
36
|
-
def advance
|
37
|
-
self
|
38
|
-
end
|
39
|
-
|
40
|
-
def turn(*)
|
41
|
-
self
|
42
|
-
end
|
43
|
-
|
44
|
-
def to_s
|
45
|
-
"No position"
|
46
|
-
end
|
47
|
-
end
|
data/lib/toy_robot/version.rb
CHANGED
data/lib/toy_robot.rb
CHANGED
@@ -3,24 +3,25 @@ require "toy_robot/client"
|
|
3
3
|
require "toy_robot/robot"
|
4
4
|
require "toy_robot/arena"
|
5
5
|
require "toy_robot/command"
|
6
|
-
require "toy_robot/position"
|
7
6
|
require "toy_robot/direction"
|
7
|
+
require "toy_robot/position"
|
8
|
+
require "toy_robot/null_position"
|
8
9
|
|
9
10
|
module ToyRobot
|
10
11
|
def self.main
|
11
12
|
client = Client.new
|
12
13
|
|
13
14
|
loop do
|
14
|
-
command
|
15
|
-
|
16
|
-
|
15
|
+
print "Input command: "
|
16
|
+
input = get_input
|
17
|
+
break if input == "exit"
|
18
|
+
client.parse input
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
20
22
|
private
|
21
23
|
|
22
24
|
def self.get_input
|
23
|
-
print "Input command: "
|
24
25
|
gets.downcase.strip
|
25
26
|
end
|
26
27
|
end
|
data/toy_robot.gemspec
CHANGED
@@ -6,7 +6,7 @@ require 'toy_robot/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "another_toy_robot"
|
8
8
|
spec.version = ToyRobot::VERSION
|
9
|
-
spec.date = "2016-11-
|
9
|
+
spec.date = "2016-11-30"
|
10
10
|
spec.authors = "Sheldon J. Johnson"
|
11
11
|
spec.email = "sheldon.j.johnson@outlook.com"
|
12
12
|
spec.homepage = "https://github.com/drzel/toy_robot"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: another_toy_robot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sheldon J. Johnson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: "The application is a simulation of a toy robot moving on a square tabletop.\nIt
|
14
14
|
is an example of a well tested, object oriented design, employing the \ncommand
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- lib/toy_robot/client.rb
|
35
35
|
- lib/toy_robot/command.rb
|
36
36
|
- lib/toy_robot/direction.rb
|
37
|
+
- lib/toy_robot/null_position.rb
|
37
38
|
- lib/toy_robot/position.rb
|
38
39
|
- lib/toy_robot/robot.rb
|
39
40
|
- lib/toy_robot/version.rb
|