toy-robot-simulator 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +79 -0
- data/lib/toy_robot_controller.rb +13 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3ed853290dbb977c1c2b342805da404cc67a2a6100f87595b575619aa31f647
|
4
|
+
data.tar.gz: ab21ed69da38c44c7bd494a88ba07c00be6fc2146782e77a4ab1d94c45450096
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f7080dcaf8309609f5dd61ae4e75428abdbba355d0d0a6a1bb228c7d653eb8c0fa0fdc90cb094ac59494b149e0276c02ff8c6ab1b929800a2e154639b3c4356
|
7
|
+
data.tar.gz: 19b7d35b3cc6c0c02297e2964d78e201d981dbc877a97b7d6923288b56eee02e98139eb720ef59cb58fbeb8544870b736a403c13426c3c92aa7d54037241ac51
|
data/README.md
CHANGED
@@ -0,0 +1,79 @@
|
|
1
|
+
|
2
|
+
# Toy Robot Simulator
|
3
|
+
_version: 0.0.1_
|
4
|
+
|
5
|
+
The Ruby gem let the toy robot move a table painlessly, easily, and most importantly, **it does not fall from the table**
|
6
|
+
|
7
|
+
- [Full gem documentation (version 0.0.1)](https://www.rubydoc.info/gems/toy-robot-simulator/0.0.1/)
|
8
|
+
# Installation
|
9
|
+
```
|
10
|
+
gem install toy-robot-simulator
|
11
|
+
```
|
12
|
+
```ruby
|
13
|
+
gem "toy-robot-simulator", "~> 0.0.1"
|
14
|
+
```
|
15
|
+
```
|
16
|
+
bundle install
|
17
|
+
```
|
18
|
+
# Usage
|
19
|
+
## ToyRobotController
|
20
|
+
The main component of this gem is which instantiate Robot, Table and process commands. In order to use this controller you need to do the following:
|
21
|
+
|
22
|
+
**1. Include the ToyRobotController**
|
23
|
+
```ruby
|
24
|
+
require 'toy_robot_controller'
|
25
|
+
```
|
26
|
+
**2. Initialize the controller**
|
27
|
+
```ruby
|
28
|
+
ToyRobotController.init(commands: Array, table_size : int)
|
29
|
+
```
|
30
|
+
`There is also a CommandLoader module available which can read the test data from file and return commands array which can be passed into init of our controller`
|
31
|
+
|
32
|
+
examples:
|
33
|
+
```ruby
|
34
|
+
ToyRobotController.init(['PLACE 0,0,NORTH', 'MOVE'])
|
35
|
+
```
|
36
|
+
**3. Execute the commands**
|
37
|
+
```ruby
|
38
|
+
ToyRobotController.execute_commands
|
39
|
+
```
|
40
|
+
**4. Report**
|
41
|
+
```ruby
|
42
|
+
ToyRobotController.report
|
43
|
+
```
|
44
|
+
|
45
|
+
|
46
|
+
## CommandSetLoader
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
require 'helpers/command_set_loader'
|
50
|
+
include CommandSetLoader
|
51
|
+
```
|
52
|
+
```ruby
|
53
|
+
commands = CommandSetLoader.read_commands('./test_data/test1.txt')
|
54
|
+
```
|
55
|
+
`We can use this helper method to generate the array of commands which can be used as commands with ToyRobotController`
|
56
|
+
```ruby
|
57
|
+
ToyRobotController.init(commands)
|
58
|
+
```
|
59
|
+
|
60
|
+
## Things to considered
|
61
|
+
|
62
|
+
**init** method of ToyRobotController expects array of commands where all the commands should be type of **string**
|
63
|
+
|
64
|
+
If you are using the **CommandSetLoader**, make sure that your file has each command on a new line such as:
|
65
|
+
```plain
|
66
|
+
PLACE 0,0,NORTH
|
67
|
+
MOVE
|
68
|
+
REPORT
|
69
|
+
```
|
70
|
+
|
71
|
+
## TODO
|
72
|
+
|
73
|
+
- Add configuration option
|
74
|
+
- Ability to change PLACE command sequence such as
|
75
|
+
- X,Y, NORTH PLACE
|
76
|
+
- NORTH X Y PLACE
|
77
|
+
- **CommandSetLoader** is reading file locally, would be a good idea to add the option to read the file remotely
|
78
|
+
- Add obstructions on the table
|
79
|
+
- Non square table
|
data/lib/toy_robot_controller.rb
CHANGED
@@ -4,12 +4,16 @@ require_relative 'toy_robot/robot'
|
|
4
4
|
require_relative 'surface/table'
|
5
5
|
require_relative 'helpers/command_parser_helper'
|
6
6
|
require_relative 'errors/command'
|
7
|
-
|
7
|
+
|
8
|
+
# ToyRobotController module which sets up the Robot, Table and execute commands provided
|
8
9
|
module ToyRobotController
|
9
10
|
class << self
|
10
11
|
attr_accessor :robot, :commands
|
11
12
|
include CommandParserHelper
|
12
13
|
|
14
|
+
# This method is expecting [Array<String>] of commands such as ['PLACE 0,0,NORTH', 'MOVE', 'REPORT']
|
15
|
+
# @param commands_stream [Array<String>]
|
16
|
+
# @param table_size [Integer]
|
13
17
|
def init(commands_stream, table_size = 5)
|
14
18
|
validate_command_stream(commands_stream)
|
15
19
|
|
@@ -20,10 +24,18 @@ module ToyRobotController
|
|
20
24
|
populate_commands
|
21
25
|
end
|
22
26
|
|
27
|
+
# This method return the coordinates of Robot on the table and the direction it is facing
|
23
28
|
def report(format = 'console')
|
24
29
|
@robot.report(format)
|
25
30
|
end
|
26
31
|
|
32
|
+
# Simply executes the commands array.
|
33
|
+
#
|
34
|
+
# considerations:
|
35
|
+
#
|
36
|
+
# @raise [Command::NoValidCommandsFound] if the commands array length is 0
|
37
|
+
#
|
38
|
+
# @raise [Command::PlaceCommandNotFound] if the commands array does not contain a valid PLACE command
|
27
39
|
def execute_commands
|
28
40
|
raise Command::NoValidCommandsFound if @commands.size.zero?
|
29
41
|
raise Command::PlaceCommandNotFound if @commands.select { |c| c.type == 'PLACE' }.size.zero?
|