toy-robot
Description
toy-robot is a simulator of a toy robot that moves on a tabletop.
From RubyGems
$ gem install toybot
$ toy-robot
Note:
If you get an unknown command error after trying to execute toy-robot,
you can try to reset the command hash table by executing hash -r (bash) or
opening a new terminal window.
From the source
Requirements
If you don't already have, install Bundler from rubygems:
$ gem install bundler
Installation
$ bundle install
This will install the few required dependencies specified in toybot.gemspec
Run tests
You can check that everything works correctly by running the tests:
$ bundle exec rake
Usage
$ bundle exec exe/toy-robot
It is also possible to give a size to the tabletop:
$ bundle exec bin/toy-robot WIDTH HEIGHT
Error handling
- Toybot is quite permissive with the input, as long as it respects the number of arguments and uses the comma separator.
- Toybot is not very smart, although he is trying. Any invalid input will freak him out and he will display a little helper.
Implementation details
Toybot is made of several components:
Tabletop
The Tabletop class is a simple abstraction of a tabletop with a width and a height.
- By default, the size of the tabletop is infinite.
- The tabletop only has positive coordinates starting at x(0) and y(0).
- The tabletop cannot be smaller than 1 by 1.
where: lib/toybot/tabletop.rb
Direction
The Direction class abstracts the notion of cardinal directions.
- It can be rotated clockwise or counter clockwise by 90 degrees.
where: lib/toybot/direction.rb
Bot
Finally, our little robot.
- The bot needs to be initialized with a tabletop object.
- It can be initialized and run in one time with:
Toybot::Bot.start(tabletop)
- It internally implements all the possible actions.
- Action methods are called by passing a message to the bot object, deducted from the input.
where: lib/toybot/bot.rb
Specifications
toy-robot is a simulator of a toy robot that moves on a tabletop.
toy-robot reads instructions from STDIN, executing them one at a time until EOF is reached. (On a Linux or OSX system, type C-d to generate an EOF character).
valid commands
PLACE X,Y,FACING
Put the toy robot on the table in position X,Y and facing NORTH, SOUTH, EAST or WEST. If the robot is already placed, issuing another valid PLACE command will place the robot in the newly specified location.
MOVE
Moves the toy robot one unit forward in the direction it is currently facing.
LEFT
Rotates the robot 90 degrees to the left (i.e. counter-clockwise) without changing the position of the robot.
RIGHT
Rotates the robot 90 degrees to the right (i.e. clockwise) without changing the position of the robot.
REPORT
Announces the X,Y and F of the robot.