KatPadi's Point

TRS: Toy Robot Simulator

A few weeks ago, I created a basic Ruby gem implementation of the “Toy Robot Simulator” problem.

Toy Robot Simulator wuttt???

The scenario is that the application is a simulation of a toy robot moving on a square table of certain dimensions. The robot is free to roam around the surface but must not fall. (Yeah. Robot must not fall. =P)

There are 5 basic commands that the robot can understand:

  • PLACE X,Y,F
  • MOVE
  • LEFT
  • RIGHT
  • REPORT

Other simulation rules are as follows:

  • PLACE will put the toy robot on the table in position X,Y and facing NORTH, SOUTH, EAST or WEST.
    The origin (0,0) can be considered to be the SOUTH WEST most corner.
  • The first valid command to the robot is a PLACE command, after that, any sequence of commands may be issued, in any order, including another PLACE command.
  • The application should discard all commands in the sequence until a valid PLACE command has been executed.
  • MOVE will move the toy robot one unit forward in the direction it is currently facing.
  • LEFT and RIGHT will rotate the robot 90 degrees in the specified direction without changing the position of the robot.
  • REPORT will announce the X,Y and F of the robot. This can be in any form, but standard output is sufficient.

Mighty Thor!!!

The app that I came up with can be run interactively or via series of commands from a file input. I used Thor for the “interactive” mode. Thor removes the pain of having to handle command-line argument parsing! It’s like Rake on steroids. (Or Thor with the Mjolnir? OK. I am not making any sense.)

ANYWAY, so I used it to build the CLI mode of the Toy Robot Simulator.

Run interactively via command line input:

$ trs play

Specify table dimensions:

$ trs play -l=10 -w=10

Auto-report position after every command:

$ trs play -r=true

Commands while inside interactive mode:
PLACE - Specifies position of robot in X,Y,F format.
_i.e. : PLACE 1,1,SOUTH_
MOVE - Moves robot X unit forward in the direction it is currently facing
_i.e. : MOVE or MOVE 2_
LEFT - Rotates robot 90 degrees to the left
RIGHT - Rotates robot 90 degrees to the right
REPORT - Shouts the current position

You can define the dimensions of the table before you start. You can move the robot around, etc.

More on how to use it here: https://github.com/katpadi/trs/edit/master/README.md

P.S. I wish I had time to do some “visuals” but I leave the robot simulation imagination to you. (harhar!)

TRS repository: https://github.com/katpadi/trs

Leave a Reply

Your email address will not be published. Required fields are marked *