loco_bot 1.0.0
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +393 -0
- data/Rakefile +9 -0
- data/bin/loco_bot_cli +10 -0
- data/lib/loco_bot/cli/command/base.rb +28 -0
- data/lib/loco_bot/cli/command/hodor.rb +14 -0
- data/lib/loco_bot/cli/command/left.rb +14 -0
- data/lib/loco_bot/cli/command/move.rb +14 -0
- data/lib/loco_bot/cli/command/place.rb +17 -0
- data/lib/loco_bot/cli/command/report.rb +18 -0
- data/lib/loco_bot/cli/command/right.rb +14 -0
- data/lib/loco_bot/cli/command.rb +34 -0
- data/lib/loco_bot/cli.rb +67 -0
- data/lib/loco_bot/robot/direction/east.rb +37 -0
- data/lib/loco_bot/robot/direction/north.rb +37 -0
- data/lib/loco_bot/robot/direction/south.rb +37 -0
- data/lib/loco_bot/robot/direction/west.rb +37 -0
- data/lib/loco_bot/robot/direction.rb +27 -0
- data/lib/loco_bot/robot.rb +102 -0
- data/lib/loco_bot/table.rb +94 -0
- data/lib/loco_bot/version.rb +4 -0
- data/lib/loco_bot.rb +8 -0
- data/loco_bot.gemspec +26 -0
- data/spec/loco_bot/cli/command/hodor_spec.rb +14 -0
- data/spec/loco_bot/cli/command/left_spec.rb +14 -0
- data/spec/loco_bot/cli/command/move_spec.rb +14 -0
- data/spec/loco_bot/cli/command/place_spec.rb +14 -0
- data/spec/loco_bot/cli/command/report_spec.rb +22 -0
- data/spec/loco_bot/cli/command/right_spec.rb +14 -0
- data/spec/loco_bot/cli/command_spec.rb +53 -0
- data/spec/loco_bot/cli_spec.rb +37 -0
- data/spec/loco_bot/robot/direction/east_spec.rb +25 -0
- data/spec/loco_bot/robot/direction/north_spec.rb +25 -0
- data/spec/loco_bot/robot/direction/south_spec.rb +25 -0
- data/spec/loco_bot/robot/direction/west_spec.rb +25 -0
- data/spec/loco_bot/robot/direction_spec.rb +35 -0
- data/spec/loco_bot/robot_spec.rb +247 -0
- data/spec/loco_bot/table_spec.rb +197 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/support/test_1.txt +3 -0
- data/spec/support/test_2.txt +3 -0
- data/spec/support/test_3.txt +6 -0
- data/spec/support/test_4.txt +28 -0
- metadata +183 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ef33dd6ab2797541d85647722519e50e1fcf5c56
|
4
|
+
data.tar.gz: 2695c38bbc217dd317ba7ab67cfc6681e4034be7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 713365f6dd107ae2ac6410e192d5f77d4da7fd60590f289b409967e29dad9a83987103a6be79800092d0d02eba6b27a15b1f28dec10ac205736b1b4b0db69526
|
7
|
+
data.tar.gz: 511a8c28a3cfcd05ceb5b925f15c4cebfa250ca6a7b219657403c80d829add94e1c070248112b6260641a6805a94f505aa4a49828696ba914941d776663de2ac
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Rafaël Gonzalez
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,393 @@
|
|
1
|
+
[](https://travis-ci.org/rafaelgonzalez/loco_bot)
|
2
|
+
[](https://gemnasium.com/rafaelgonzalez/loco_bot)
|
3
|
+
[](https://codeclimate.com/github/rafaelgonzalez/loco_bot)
|
4
|
+
[](https://codeclimate.com/github/rafaelgonzalez/loco_bot)
|
5
|
+
|
6
|
+
# LocoBot
|
7
|
+
|
8
|
+
Ruby gem of crazy robots and benevolent tables that keep watching over them.
|
9
|
+
|
10
|
+
Issue commands to control the robots and the tables will keep you from making them fall.
|
11
|
+
|
12
|
+
> You monster!
|
13
|
+
|
14
|
+
> *- GLadOS*
|
15
|
+
|
16
|
+
## Rules
|
17
|
+
|
18
|
+
- Robots can not move until placed on a table.
|
19
|
+
- Robots are free to roam around the surface of the table, but will be prevented from
|
20
|
+
- falling from the table
|
21
|
+
- crashing into other robots on the same table
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Via RubyGems:
|
26
|
+
|
27
|
+
$ gem install loco_bot
|
28
|
+
|
29
|
+
Or in a Gemfile:
|
30
|
+
|
31
|
+
gem 'loco_bot'
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
### Command line interface
|
36
|
+
|
37
|
+
From the CLI, you can place a robot on a table of 5x5 and play around with it.
|
38
|
+
|
39
|
+
This means you only have a unique instance of `Robot` and `Table` available through this interface.
|
40
|
+
|
41
|
+
To use multiple robots and tables, see the API usage further below.
|
42
|
+
|
43
|
+
#### Manual input
|
44
|
+
|
45
|
+
Just run the binary and start typing commands:
|
46
|
+
|
47
|
+
$ loco_bot_cli
|
48
|
+
|
49
|
+
Available commands are:
|
50
|
+
|
51
|
+
- **PLACE X,Y,F**
|
52
|
+
|
53
|
+
Puts the robot on the table in position X,Y and facing NORTH, SOUTH, EAST or WEST.
|
54
|
+
|
55
|
+
- **MOVE**
|
56
|
+
|
57
|
+
Moves the robot one unit forward in the direction it is currently facing.
|
58
|
+
|
59
|
+
- **LEFT** and **RIGHT**
|
60
|
+
|
61
|
+
Rotates the robot 90 degrees in the specified direction without changing its position.
|
62
|
+
|
63
|
+
- **REPORT**
|
64
|
+
|
65
|
+
Announces the X,Y and F of the robot.
|
66
|
+
|
67
|
+
- **HODOR**
|
68
|
+
|
69
|
+
Outputs a friendly greating.
|
70
|
+
|
71
|
+
Example:
|
72
|
+
|
73
|
+
$ loco_bot_cli
|
74
|
+
PLACE 1,2,SOUTH
|
75
|
+
MOVE
|
76
|
+
LEFT
|
77
|
+
MOVE
|
78
|
+
MOVE
|
79
|
+
REPORT
|
80
|
+
0,4,EAST
|
81
|
+
|
82
|
+
#### Input from files
|
83
|
+
|
84
|
+
You can also use files to issue commands, like so:
|
85
|
+
|
86
|
+
$ loco_bot_cli < ./spec/support/test_1.txt
|
87
|
+
|
88
|
+
### API
|
89
|
+
|
90
|
+
Full documentation is available [here](http://rubydoc.info/github/rafaelgonzalez/locobot/master/frames).
|
91
|
+
|
92
|
+
#### LocoBot::Table
|
93
|
+
|
94
|
+
##### #new
|
95
|
+
|
96
|
+
You can specify the dimensions of the `Table`. Default dimensions are 5x5.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
table = LocoBot::Table.new
|
100
|
+
table.width # => 5
|
101
|
+
table.height # => 5
|
102
|
+
|
103
|
+
table = LocoBot::Table.new(20, 25)
|
104
|
+
table.width # => 20
|
105
|
+
table.height # => 25
|
106
|
+
```
|
107
|
+
|
108
|
+
##### #place_robot
|
109
|
+
|
110
|
+
Place the given `Robot` on the `Table`.
|
111
|
+
|
112
|
+
This method works the same way as `Robot#place`, only a `Robot` is passed as first argument instead of a `Table`.
|
113
|
+
|
114
|
+
Placing a `Robot` on a `Table` will:
|
115
|
+
|
116
|
+
- update the `Table`'s list of `Robot`s
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
robot = LocoBot::Robot.new
|
120
|
+
table = LocoBot::Table.new(10, 20)
|
121
|
+
|
122
|
+
table.robots # => []
|
123
|
+
|
124
|
+
table.place_robot(robot, 2, 3, LocoBot::Robot::Direction::North) # => true
|
125
|
+
|
126
|
+
table.robots # => [#<LocoBot::Robot:0x007f9212e7ad5>]
|
127
|
+
```
|
128
|
+
|
129
|
+
- update the `Robot`'s positions
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
robot = LocoBot::Robot.new
|
133
|
+
table = LocoBot::Table.new(10, 20)
|
134
|
+
|
135
|
+
robot.x # => nil
|
136
|
+
robot.y # => nil
|
137
|
+
robot.direction # => nil
|
138
|
+
robot.table # => nil
|
139
|
+
|
140
|
+
table.place_robot(robot, 2, 3, LocoBot::Robot::Direction::North) # => true
|
141
|
+
|
142
|
+
robot.x # => 2
|
143
|
+
robot.y # => 3
|
144
|
+
robot.direction # => LocoBot::Robot::Direction::North
|
145
|
+
robot.table # => #<LocoBot::Table:0x007f9212e62480 @width=10, @height=20>
|
146
|
+
```
|
147
|
+
|
148
|
+
A `Robot` can not be placed on a spot outside the `Table`.
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
robot = LocoBot::Robot.new
|
152
|
+
table = LocoBot::Table.new(10, 20)
|
153
|
+
|
154
|
+
table.place_robot(robot, 100, 12, LocoBot::Robot::Direction::North) # => false
|
155
|
+
|
156
|
+
robot.table # => nil
|
157
|
+
table.robots # => []
|
158
|
+
```
|
159
|
+
|
160
|
+
A `Robot` can not be placed on a spot of the `Table` where there is another `Robot`.
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
robot = LocoBot::Robot.new
|
164
|
+
table = LocoBot::Table.new(10, 20)
|
165
|
+
|
166
|
+
table.place_robot(LocoBot::Robot.new, 2, 3, LocoBot::Robot::Direction::North) # => true
|
167
|
+
|
168
|
+
table.place_robot(robot, 2, 3, LocoBot::Robot::Direction::East) # => false
|
169
|
+
|
170
|
+
robot.table # => nil
|
171
|
+
table.robots.count # => 1
|
172
|
+
```
|
173
|
+
|
174
|
+
##### #remove_robot
|
175
|
+
|
176
|
+
Removes the given `Robot` from the `Table`.
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
robot = LocoBot::Robot.new
|
180
|
+
table = LocoBot::Table.new(10, 20)
|
181
|
+
table.place_robot(robot, 2, 3, LocoBot::Robot::Direction::North) # => true
|
182
|
+
|
183
|
+
table.robots # => [#<LocoBot::Robot:0x007f9212e7ad5>]
|
184
|
+
robot.table # => #<LocoBot::Table:0x007f9212e62480 @width=10, @height=20>
|
185
|
+
|
186
|
+
table.remove_remove(robot)
|
187
|
+
|
188
|
+
table.robots # => []
|
189
|
+
robot.table # => nil
|
190
|
+
```
|
191
|
+
|
192
|
+
#### LocoBot::Robot
|
193
|
+
|
194
|
+
All the following methods, except for `#report` and `#hodor`, return a `Boolean`.
|
195
|
+
|
196
|
+
If they return `true`, the operation as succeded and the state of the Robot has changed. They will return `false` otherwise.
|
197
|
+
|
198
|
+
##### #place
|
199
|
+
|
200
|
+
Place the `Robot` on the given `Table`.
|
201
|
+
|
202
|
+
This method works the same way as `Table#place_robot`, only a `Table` is passed as first argument instead of a `Robot`.
|
203
|
+
|
204
|
+
Placing a `Robot` on a `Table` will:
|
205
|
+
|
206
|
+
- update the `Table`'s list of `Robot`s
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
robot = LocoBot::Robot.new
|
210
|
+
table = LocoBot::Table.new(10, 20)
|
211
|
+
|
212
|
+
table.robots # => []
|
213
|
+
|
214
|
+
robot.place(table, 2, 3, LocoBot::Robot::Direction::North) # => true
|
215
|
+
|
216
|
+
table.robots # => [#<LocoBot::Robot:0x007f9212e7ad5>]
|
217
|
+
```
|
218
|
+
|
219
|
+
- update the `Robot`'s positions
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
robot = LocoBot::Robot.new
|
223
|
+
table = LocoBot::Table.new(10, 20)
|
224
|
+
|
225
|
+
robot.x # => nil
|
226
|
+
robot.y # => nil
|
227
|
+
robot.direction # => nil
|
228
|
+
robot.table # => nil
|
229
|
+
|
230
|
+
robot.place(table, 2, 3, LocoBot::Robot::Direction::North) # => true
|
231
|
+
|
232
|
+
robot.x # => 2
|
233
|
+
robot.y # => 3
|
234
|
+
robot.direction # => LocoBot::Robot::Direction::North
|
235
|
+
robot.table # => #<LocoBot::Table:0x007f9212e62480 @width=10, @height=20>
|
236
|
+
```
|
237
|
+
|
238
|
+
A `Robot` can not be placed on a spot outside the `Table`.
|
239
|
+
|
240
|
+
```ruby
|
241
|
+
robot = LocoBot::Robot.new
|
242
|
+
table = LocoBot::Table.new(10, 20)
|
243
|
+
|
244
|
+
robot.place(table, 100, 12, LocoBot::Robot::Direction::North) # => false
|
245
|
+
|
246
|
+
robot.table # => nil
|
247
|
+
table.robots # => []
|
248
|
+
```
|
249
|
+
|
250
|
+
A `Robot` can not be placed on a spot of the `Table` where there is another `Robot`.
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
robot = LocoBot::Robot.new
|
254
|
+
table = LocoBot::Table.new(10, 20)
|
255
|
+
|
256
|
+
LocoBot::Robot.new.place(table, 2, 3, LocoBot::Robot::Direction::North) # => true
|
257
|
+
|
258
|
+
robot.place(table, 2, 3, LocoBot::Robot::Direction::East) # => false
|
259
|
+
|
260
|
+
robot.table # => nil
|
261
|
+
table.robots.count # => 1
|
262
|
+
```
|
263
|
+
|
264
|
+
##### #remove
|
265
|
+
|
266
|
+
Removes the `Robot` from its current `Table`.
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
robot = LocoBot::Robot.new
|
270
|
+
table = LocoBot::Table.new(10, 20)
|
271
|
+
robot.place(table, 2, 3, LocoBot::Robot::Direction::North) # => true
|
272
|
+
|
273
|
+
table.robots # => [#<LocoBot::Robot:0x007f9212e7ad5>]
|
274
|
+
robot.table # => #<LocoBot::Table:0x007f9212e62480 @width=10, @height=20>
|
275
|
+
|
276
|
+
robot.remove
|
277
|
+
|
278
|
+
table.robots # => []
|
279
|
+
robot.table # => nil
|
280
|
+
```
|
281
|
+
|
282
|
+
##### #turn_left, #turn_right
|
283
|
+
|
284
|
+
Changes the `Robot`'s facing direction without changing its position.
|
285
|
+
|
286
|
+
```ruby
|
287
|
+
robot = LocoBot::Robot.new
|
288
|
+
table = LocoBot::Table.new
|
289
|
+
|
290
|
+
robot.place(table, 2, 3, LocoBot::Robot::Direction::West)
|
291
|
+
robot.direction # => LocoBot::Robot::Direction::West
|
292
|
+
|
293
|
+
robot.turn_right # => true
|
294
|
+
robot.direction # => LocoBot::Robot::Direction::North
|
295
|
+
|
296
|
+
robot.turn_left # => true
|
297
|
+
robot.direction # => LocoBot::Robot::Direction::West
|
298
|
+
```
|
299
|
+
|
300
|
+
##### #move
|
301
|
+
|
302
|
+
Moves the robot one unit forward in the direction it is currently facing.
|
303
|
+
|
304
|
+
```ruby
|
305
|
+
robot = LocoBot::Robot.new
|
306
|
+
table = LocoBot::Table.new
|
307
|
+
|
308
|
+
robot.place(table, 2, 3, LocoBot::Robot::Direction::West)
|
309
|
+
|
310
|
+
robot.move # => true
|
311
|
+
|
312
|
+
robot.x # => 1
|
313
|
+
robot.y # => 3
|
314
|
+
```
|
315
|
+
|
316
|
+
The robot will not move if it makes it fall out of the table.
|
317
|
+
|
318
|
+
```ruby
|
319
|
+
robot = LocoBot::Robot.new
|
320
|
+
table = LocoBot::Table.new
|
321
|
+
|
322
|
+
robot.place(table, 0, 0, LocoBot::Robot::Direction::South)
|
323
|
+
|
324
|
+
robot.move # => false
|
325
|
+
|
326
|
+
robot.x # => 0
|
327
|
+
robot.y # => 0
|
328
|
+
```
|
329
|
+
|
330
|
+
It will not move either if there is another robot on the destination spot.
|
331
|
+
|
332
|
+
```ruby
|
333
|
+
robot = LocoBot::Robot.new
|
334
|
+
table = LocoBot::Table.new
|
335
|
+
|
336
|
+
LocoBot::Robot.new.place(table, 0, 0, LocoBot::Robot::Direction::South)
|
337
|
+
|
338
|
+
robot.move # => false
|
339
|
+
|
340
|
+
robot.x # => 0
|
341
|
+
robot.y # => 0
|
342
|
+
```
|
343
|
+
|
344
|
+
##### #report
|
345
|
+
|
346
|
+
Returns a Hash describing the `Robot`'s position and facing direction.
|
347
|
+
|
348
|
+
```ruby
|
349
|
+
robot = LocoBot::Robot.new
|
350
|
+
table = LocoBot::Table.new
|
351
|
+
|
352
|
+
robot.place(table, 2, 3, LocoBot::Robot::Direction::West)
|
353
|
+
|
354
|
+
robot.report
|
355
|
+
# => {x: 2, y: 3, direction: LocoBot::Robot::Direction::West}
|
356
|
+
```
|
357
|
+
|
358
|
+
##### #hodor!
|
359
|
+
|
360
|
+
Outputs a friendly message.
|
361
|
+
|
362
|
+
```ruby
|
363
|
+
LocoBot::Robot.new.hodor!
|
364
|
+
# stdout => "HODOR HODOR !\n"
|
365
|
+
```
|
366
|
+
|
367
|
+
#### LocoBot::CLI
|
368
|
+
|
369
|
+
You can still access the command line interface through code.
|
370
|
+
|
371
|
+
```ruby
|
372
|
+
cli = LocoBot::CLI.new
|
373
|
+
|
374
|
+
cli.input!('PLACE 1,2,WEST')
|
375
|
+
|
376
|
+
cli.robot.x # => 1
|
377
|
+
cli.robot.y # => 2
|
378
|
+
cli.robot.direction # => LocoBot::Robot::Direction::West
|
379
|
+
```
|
380
|
+
|
381
|
+
## License
|
382
|
+
|
383
|
+
Copyright 2014 Rafaël Gonzalez
|
384
|
+
|
385
|
+
Released under the terms of the MIT licence. See the LICENSE file for more details.
|
386
|
+
|
387
|
+
## Contributing
|
388
|
+
|
389
|
+
1. Fork it ( https://github.com/rafaelgonzalez/loco_bot/fork )
|
390
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
391
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
392
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
393
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/loco_bot_cli
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module LocoBot
|
2
|
+
class CLI
|
3
|
+
module Command
|
4
|
+
# Parent class for Command classes.
|
5
|
+
class Base
|
6
|
+
|
7
|
+
# @!attribute [r] table
|
8
|
+
# @return [Robot] the Robot
|
9
|
+
attr_reader :robot
|
10
|
+
|
11
|
+
# @!attribute [r] table
|
12
|
+
# @return [Table] the Table
|
13
|
+
attr_reader :table
|
14
|
+
|
15
|
+
def initialize(robot, table)
|
16
|
+
@robot = robot
|
17
|
+
@table = table
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns the Command's label.
|
21
|
+
# @return [String] the Command's label
|
22
|
+
def self.label
|
23
|
+
self.name.split("::").last.upcase
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module LocoBot
|
2
|
+
class CLI
|
3
|
+
module Command
|
4
|
+
# Upon execution, calls #turn_left on the robot attribute.
|
5
|
+
class Left < Base
|
6
|
+
# Executes the command.
|
7
|
+
# @return [Boolean] true if the operation succeeded, false otherwise.
|
8
|
+
def execute
|
9
|
+
robot.turn_left
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module LocoBot
|
2
|
+
class CLI
|
3
|
+
module Command
|
4
|
+
# Upon execution, calls #move on the robot attribute.
|
5
|
+
class Move < Base
|
6
|
+
# Executes the command.
|
7
|
+
# @return [Boolean] true if the operation succeeded, false otherwise.
|
8
|
+
def execute
|
9
|
+
robot.move
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module LocoBot
|
2
|
+
class CLI
|
3
|
+
module Command
|
4
|
+
# Upon execution, calls #place on the robot attribute.
|
5
|
+
class Place < Base
|
6
|
+
# Executes the command.
|
7
|
+
# Calls #place on the robot attribute, passing the table attribute and the given arguments.
|
8
|
+
# @return [Boolean] true if the operation succeeded, false otherwise.
|
9
|
+
def execute(x, y, direction_name)
|
10
|
+
direction = Robot::Direction.from_name(direction_name)
|
11
|
+
|
12
|
+
robot.place(table, x.to_i, y.to_i, direction)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module LocoBot
|
2
|
+
class CLI
|
3
|
+
module Command
|
4
|
+
# Upon execution, outputs the robot position to STDOUT.
|
5
|
+
class Report < Base
|
6
|
+
# Executes the command.
|
7
|
+
# @return [void]
|
8
|
+
def execute
|
9
|
+
report = robot.report
|
10
|
+
|
11
|
+
unless report.empty?
|
12
|
+
puts "#{report[:x]},#{report[:y]},#{report[:direction].label}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module LocoBot
|
2
|
+
class CLI
|
3
|
+
module Command
|
4
|
+
# Upon execution, calls #turn_right on the robot attribute.
|
5
|
+
class Right < Base
|
6
|
+
# Executes the command.
|
7
|
+
# @return [Boolean] true if the operation succeeded, false otherwise.
|
8
|
+
def execute
|
9
|
+
robot.turn_right
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'loco_bot/cli/command/base'
|
2
|
+
require 'loco_bot/cli/command/hodor'
|
3
|
+
require 'loco_bot/cli/command/left'
|
4
|
+
require 'loco_bot/cli/command/move'
|
5
|
+
require 'loco_bot/cli/command/place'
|
6
|
+
require 'loco_bot/cli/command/report'
|
7
|
+
require 'loco_bot/cli/command/right'
|
8
|
+
|
9
|
+
module LocoBot
|
10
|
+
class CLI
|
11
|
+
# Top-level namespace for available commands.
|
12
|
+
module Command
|
13
|
+
|
14
|
+
# Returns the list of available commands.
|
15
|
+
# @return [Array<String>] the list of available commands
|
16
|
+
def self.list
|
17
|
+
list = constants.map {|const| const.to_s.upcase}
|
18
|
+
list.delete('BASE')
|
19
|
+
list
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns a Command class that as the same name as the given name.
|
23
|
+
# @param name [String] the Command name
|
24
|
+
# @return [Command] the Command
|
25
|
+
def self.class_from_name(name)
|
26
|
+
return nil if name.nil? or name.capitalize.to_s == 'Base'
|
27
|
+
|
28
|
+
if const_defined?(name.capitalize, false)
|
29
|
+
const_get(name.capitalize)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|