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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9697c5a9c1cd08ba5afadb9b1079a0f2f59f292
4
- data.tar.gz: f9645af6fd80dce7a433784a4779c08ee72c014b
3
+ metadata.gz: 36a233f2485ba520549d1f92e495ec5ca18ef050
4
+ data.tar.gz: 8742f7dad1462f904c0d423b477609152fdce748
5
5
  SHA512:
6
- metadata.gz: 460f8d9a3e697694c47589e52755480b1996ee64fc240a8d593f88de99499c84a055c0fe45e8ed9212af9576911013c33a7417cdc9ed3d3a347c7d44faaf87da
7
- data.tar.gz: c7d44efdea2331bfba1189a0e01272a4bf36a44eec99083183c73ae324ff4a3204978e74f0df86897e4950ade59f1ba9a55faf6183c321a8abc2e13c083d8704
6
+ metadata.gz: edc2a79ad6e63d0cd297c47fdd6da200f644a9e1ce6d027addea658669069af9720c14b8c6e84dfa4ef93888a40b6e320a5cf61fa8602660e21bb2a83a3ec5e9
7
+ data.tar.gz: 40ddf777aab1f1cb04b08ba474c8b54f8658200ece6d2d0cbe5c9588b17257261695672d01a01d8b66b734eb844e918de2f724e450b185d23a9817007a9f850f
data/.rubocop.yml CHANGED
@@ -2,3 +2,13 @@ AllCops:
2
2
  Exclude:
3
3
  - 'bin/**/*'
4
4
  - '*.gemspec'
5
+ DisplayCopNames: true
6
+
7
+ Style/FrozenStringLiteralComment:
8
+ Enabled: false
9
+
10
+ Style/MutableConstant:
11
+ Enabled: false
12
+
13
+ Metrics/LineLength:
14
+ Max: 100
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.2.3
1
+ 2.3.1
data/.travis.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.3
4
- before_install: gem install bundler -v 1.10.6
3
+ - 2.3.1
4
+ before_install: gem install bundler -v 1.13.2
data/Gemfile CHANGED
@@ -3,4 +3,3 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in toy_robo_simulator.gemspec
4
4
  gemspec
5
5
  gem 'codeclimate-test-reporter', group: :test, require: nil
6
- gem 'colorize', '0.7.7'
data/bin/robo-dev ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require 'toy_robo_simulator'
5
+
6
+ ToyRoboSimulator::Console.new.watch
7
+
@@ -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
- command = STDIN.gets.chomp
20
- while command
21
- run(command)
22
- print "#{format('%02d', @n += 1)} > "
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' then exit_program
52
- when 'help' then help
50
+ when 'exit'
51
+ exit_program
52
+ when 'help'
53
+ puts HELP
53
54
  else
54
- @robo.send(action.to_sym, *args)
55
+ robo.send(action.to_sym, *args)
55
56
  end
56
57
  rescue ArgumentError => e
57
- tip if e.message.include? 'wrong number of arguments'
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
- # Rubo is the character of this program. It serves to simulates the movement of a robot on a table.
3
- # The available actions are place, move, left, right, and report. Each action is validated before execution
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
- validate_placement(x, y, orientation)
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
- validate_if_placed
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
- validate_if_placed
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
- validate_if_placed
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
- validate_if_placed
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 = direction == :left ? 1 : -1
119
- index = ORIENTATIONS.index(@orientation) + i
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 validate_placement(x, y, orientation)
8
- @errors << 'X must be a number' unless int?(x)
9
- @errors << 'Y must be a number' unless int?(y)
10
- @errors << "X must be between 0 and #{@table.x}" unless in_range?(x, @table.x)
11
- @errors << "Y must be between 0 and #{@table.y}" unless in_range?(y, @table.y)
12
- @errors << 'Orientation should be either NORTH, SOUTH, EAST, or WEST' unless orientation_valid?(orientation)
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 validate_movement
18
- @errors << 'The Robo is at edge. No further move is allowed.' if edge?
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 validate_if_placed
24
- @errors << 'The Robo is not placed yet. Use PLACE command first.' unless @x && @y && @orientation
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 == 0
70
+ @y.zero?
49
71
  when :west
50
- @x == 0
72
+ @x.zero?
51
73
  end
52
74
  end
53
75
  end
@@ -1,3 +1,3 @@
1
1
  module ToyRoboSimulator
2
- VERSION = '1.0.3'
2
+ VERSION = '1.0.4'
3
3
  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.3
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-03-13 00:00:00.000000000 Z
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.4.5.1
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: