toy_robo_simulator 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +10 -0
- data/.ruby-version +1 -1
- data/.travis.yml +2 -2
- data/Gemfile +0 -1
- data/bin/robo-dev +7 -0
- data/lib/toy_robo_simulator/console.rb +12 -19
- data/lib/toy_robo_simulator/robo.rb +11 -17
- data/lib/toy_robo_simulator/validator.rb +37 -15
- data/lib/toy_robo_simulator/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36a233f2485ba520549d1f92e495ec5ca18ef050
|
4
|
+
data.tar.gz: 8742f7dad1462f904c0d423b477609152fdce748
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edc2a79ad6e63d0cd297c47fdd6da200f644a9e1ce6d027addea658669069af9720c14b8c6e84dfa4ef93888a40b6e320a5cf61fa8602660e21bb2a83a3ec5e9
|
7
|
+
data.tar.gz: 40ddf777aab1f1cb04b08ba474c8b54f8658200ece6d2d0cbe5c9588b17257261695672d01a01d8b66b734eb844e918de2f724e450b185d23a9817007a9f850f
|
data/.rubocop.yml
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.3.1
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/bin/robo-dev
ADDED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'readline'
|
2
|
+
|
1
3
|
module ToyRoboSimulator
|
2
4
|
# The console is responsible for initaing an interactive command line interface for
|
3
5
|
# users to access the program.
|
@@ -9,18 +11,15 @@ module ToyRoboSimulator
|
|
9
11
|
# Initializes a CLI that includes a Robo instance.
|
10
12
|
def initialize
|
11
13
|
puts MESSAGE
|
12
|
-
@n = 0
|
13
14
|
@robo = Robo.new
|
14
|
-
print "#{format('%02d', @n)} > "
|
15
15
|
end
|
16
16
|
|
17
17
|
# Starts watching for user input.
|
18
18
|
def watch
|
19
|
-
|
20
|
-
while
|
21
|
-
run(
|
22
|
-
|
23
|
-
command = STDIN.gets.chomp
|
19
|
+
n = 0
|
20
|
+
while cmd = Readline.readline("#{format('%02d', n)}> ", true)
|
21
|
+
run(cmd)
|
22
|
+
n += 1
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
@@ -48,30 +47,24 @@ module ToyRoboSimulator
|
|
48
47
|
# Directly sends the command with arguments to the Robo instance.
|
49
48
|
def process(action, args)
|
50
49
|
case action
|
51
|
-
when 'exit'
|
52
|
-
|
50
|
+
when 'exit'
|
51
|
+
exit_program
|
52
|
+
when 'help'
|
53
|
+
puts HELP
|
53
54
|
else
|
54
|
-
|
55
|
+
robo.send(action.to_sym, *args)
|
55
56
|
end
|
56
57
|
rescue ArgumentError => e
|
57
|
-
|
58
|
+
puts TIP if e.message.include? 'wrong number of arguments'
|
58
59
|
end
|
59
60
|
|
60
61
|
def to_args(command)
|
61
62
|
command.split(' ').map { |n| n.split(',') }.flatten.map(&:chomp).map(&:downcase)
|
62
63
|
end
|
63
64
|
|
64
|
-
def tip
|
65
|
-
puts TIP
|
66
|
-
end
|
67
|
-
|
68
65
|
def exit_program
|
69
66
|
puts "\nThank You!\n\n"
|
70
67
|
exit
|
71
68
|
end
|
72
|
-
|
73
|
-
def help
|
74
|
-
puts HELP
|
75
|
-
end
|
76
69
|
end
|
77
70
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module ToyRoboSimulator
|
2
|
-
#
|
3
|
-
# The available actions are place, move, left, right, and report.
|
2
|
+
# Robo is the character of this program. It serves to simulate the movement of a robot on a table.
|
3
|
+
# The available actions are place, move, left, right, and report.
|
4
|
+
# Each action is validated before execution
|
4
5
|
# and will respond with the result.
|
5
6
|
class Robo
|
6
7
|
include Validator
|
@@ -31,11 +32,9 @@ module ToyRoboSimulator
|
|
31
32
|
# robo.y # => nil
|
32
33
|
# ```
|
33
34
|
def place(x, y, orientation)
|
34
|
-
|
35
|
-
warning && return if @errors.any?
|
36
|
-
@x = x.to_i
|
37
|
-
@y = y.to_i
|
35
|
+
@x, @y = x, y
|
38
36
|
@orientation = orientation.downcase.to_sym
|
37
|
+
return unless valid_placement?
|
39
38
|
puts 'It is placed.'
|
40
39
|
end
|
41
40
|
|
@@ -57,9 +56,7 @@ module ToyRoboSimulator
|
|
57
56
|
# robo.x # => 5
|
58
57
|
# ```
|
59
58
|
def move
|
60
|
-
|
61
|
-
validate_movement
|
62
|
-
warning && return if @errors.any?
|
59
|
+
return unless placed? && moveable?
|
63
60
|
move_forward(1)
|
64
61
|
puts 'It moves forward.'
|
65
62
|
end
|
@@ -77,16 +74,14 @@ module ToyRoboSimulator
|
|
77
74
|
# robo.orientation # => :north
|
78
75
|
# ```
|
79
76
|
def left
|
80
|
-
|
81
|
-
warning && return if @errors.any?
|
77
|
+
return unless placed?
|
82
78
|
turn(:left)
|
83
79
|
puts 'It turns left.'
|
84
80
|
end
|
85
81
|
|
86
82
|
# Turns right. See `#left`.
|
87
83
|
def right
|
88
|
-
|
89
|
-
warning && return if @errors.any?
|
84
|
+
return unless placed?
|
90
85
|
turn(:right)
|
91
86
|
puts 'It turns right'
|
92
87
|
end
|
@@ -102,8 +97,7 @@ module ToyRoboSimulator
|
|
102
97
|
# robo.report # => 'Robo is now at (3,3) facing EAST'
|
103
98
|
# ```
|
104
99
|
def report
|
105
|
-
|
106
|
-
warning && return if @errors.any?
|
100
|
+
return unless placed?
|
107
101
|
puts "Robo is now at (#{@x},#{@y}) facing #{@orientation.to_s.upcase}"
|
108
102
|
end
|
109
103
|
|
@@ -115,8 +109,8 @@ module ToyRoboSimulator
|
|
115
109
|
end
|
116
110
|
|
117
111
|
def turn(direction)
|
118
|
-
i
|
119
|
-
index
|
112
|
+
i = direction == :left ? 1 : -1
|
113
|
+
index = ORIENTATIONS.index(@orientation) + i
|
120
114
|
@orientation = ORIENTATIONS[index] || :north
|
121
115
|
end
|
122
116
|
|
@@ -3,29 +3,51 @@ module ToyRoboSimulator
|
|
3
3
|
# takes arguments and attributes from a Robo.
|
4
4
|
module Validator
|
5
5
|
# Ensures input values of Robo#place action is valid in type and range.
|
6
|
-
# Returns false if it is not valid.
|
7
|
-
def
|
8
|
-
|
9
|
-
@errors
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
# Returns false if it is not valid, otherwise returns true.
|
7
|
+
def valid_placement?
|
8
|
+
validate_placement
|
9
|
+
if @errors.any?
|
10
|
+
@errors.each { |message| puts message }
|
11
|
+
@x, @y, @orientation = nil, nil, nil
|
12
|
+
false
|
13
|
+
else
|
14
|
+
@x, @y = @x.to_i, @y.to_i
|
15
|
+
true
|
16
|
+
end
|
13
17
|
end
|
14
18
|
|
15
19
|
# Prevents Robo#move from moving out of the table.
|
16
|
-
# Returns false if the Robo is at the edge of the table.
|
17
|
-
def
|
18
|
-
|
20
|
+
# Returns false if the Robo is at the edge of the table, otherwise returns true.
|
21
|
+
def moveable?
|
22
|
+
if edge?
|
23
|
+
puts 'The Robo is at edge. No further move is allowed.'
|
24
|
+
else
|
25
|
+
true
|
26
|
+
end
|
19
27
|
end
|
20
28
|
|
21
29
|
# Ensures the Robo is placed before any other actions are performed.
|
22
|
-
# Returns false if no attributes are set for the Robo.
|
23
|
-
def
|
24
|
-
|
30
|
+
# Returns false if no attributes are set for the Robo, otherwise returns true.
|
31
|
+
def placed?
|
32
|
+
if @x && @y && @orientation
|
33
|
+
true
|
34
|
+
else
|
35
|
+
puts 'The Robo is not placed yet. Use PLACE command first.'
|
36
|
+
false
|
37
|
+
end
|
25
38
|
end
|
26
39
|
|
27
40
|
private
|
28
41
|
|
42
|
+
def validate_placement
|
43
|
+
@errors = []
|
44
|
+
@errors << 'X must be a number' unless int?(@x)
|
45
|
+
@errors << 'Y must be a number' unless int?(@y)
|
46
|
+
@errors << "X must be between 0 and #{@table.x}" unless in_range?(@x, @table.x)
|
47
|
+
@errors << "Y must be between 0 and #{@table.y}" unless in_range?(@y, @table.y)
|
48
|
+
@errors << 'It can only face NORTH, SOUTH, EAST, or WEST' unless orientation_valid?(@orientation)
|
49
|
+
end
|
50
|
+
|
29
51
|
def int?(value)
|
30
52
|
value.to_s == value.to_i.to_s
|
31
53
|
end
|
@@ -45,9 +67,9 @@ module ToyRoboSimulator
|
|
45
67
|
when :east
|
46
68
|
@x == @table.x
|
47
69
|
when :south
|
48
|
-
@y
|
70
|
+
@y.zero?
|
49
71
|
when :west
|
50
|
-
@x
|
72
|
+
@x.zero?
|
51
73
|
end
|
52
74
|
end
|
53
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toy_robo_simulator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -87,6 +87,7 @@ files:
|
|
87
87
|
- Rakefile
|
88
88
|
- bin/console
|
89
89
|
- bin/robo
|
90
|
+
- bin/robo-dev
|
90
91
|
- bin/setup
|
91
92
|
- lib/toy_robo_simulator.rb
|
92
93
|
- lib/toy_robo_simulator/console.rb
|
@@ -116,9 +117,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
117
|
version: '0'
|
117
118
|
requirements: []
|
118
119
|
rubyforge_project:
|
119
|
-
rubygems_version: 2.
|
120
|
+
rubygems_version: 2.5.1
|
120
121
|
signing_key:
|
121
122
|
specification_version: 4
|
122
123
|
summary: A toy robot simulator
|
123
124
|
test_files: []
|
124
|
-
has_rdoc:
|