- Introduction
- Prerequisites
- Installation
- Running Tests
- Running Robot Simulation
- Other available scripts
- Folder structure
- Problem Statement
- Solution Approach
A toy robot simulator written in Typescript
- The application is a simulation of a toy robot moving on a square table top, of dimensions 5 units x 5 units.
- There are no other obstructions on the table surface.
- The robot is free to roam around the surface of the table but must be prevented from falling to destruction.
- Any movement that would result in the robot falling from the table must be prevented, however further valid movement commands must still be allowed.
Before setting up and running the project, ensure you have node on your machine. Run the following command in terminal to verify
node --version
- Clone the repository to your local machine:
git clone https://github.com/ooanishoo/toy-robot-challenge.git
- Navigate to the project directory:
cd toy-robot-challenge
- Install the required dependencies:
npm install
To run all the tests, run the following command:
npm test
To run the toy robot simulation, run the following command:
npm run simulation
This will run example command files stored in src/tests/data
directory.
If you want to run a specific command file, run the following command:
npm start ./src/tests/data/<command-file>
Example:
npm start ./src/tests/data/simulation-1
Alternatively, you can create your command file and run it using the above command.
To compile Typescript files to Javascript files to dist
folder with tsc, run the following command:
npm run build
Here is an overview of the key folders and their contents:
βββ src
β βββ command
β β βββ command.spec.ts
β β βββ command.ts
β βββ direction
β β βββ direction.spec.ts
β β βββ direction.ts
β βββ robot
β β βββ robot.spec.ts
β β βββ robot.ts
β βββ rotation
β β βββ rotation.spec.ts
β β βββ rotation.ts
β βββ table
β β βββ table.spec.ts
β β βββ table.ts
β βββ tests
β β βββ data
β β β βββ simulation-1
β β β βββ simulation-2
β β β βββ simulation-3
β β β βββ simulation-4
β β βββ integration.spec.ts
β β βββ testCases.ts
β βββ types
β β βββ display.ts
β β βββ position.ts
β βββ utils
β β βββ __mocks__
β β β βββ fs.ts
β β βββ utils.spec.ts
β β βββ utils.ts
β βββ index.ts
βββ .eslintrc
βββ .gitignore
βββ jest.config.js
βββ package-lock.json
βββ package.json
βββ README.md
βββ simulation
βββ tsconfig.json
A robot can read commands of the following form from a text file
PLACE X,Y,FACE
MOVE
LEFT
RIGHT
REPORT
- Input is read from a text file.
- PLACE will put the toy robot on the table in positions X,Y and facing NORTH, SOUTH, EAST, or WEST
- The origin (0,0) is considered to be the SOUTH WEST most corner of the table
- 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 will discard all commands in the sequence until a valid PLACE command has been executed
- MOVE will move the toy robot 1 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 the standard output is sufficient
A table can be constructed by providing dimensions: width and height
const table = new Table(5,5);
CommandType represents the following types of commands that can be executed by the robot:
- PLACE
- MOVE
- LEFT
- RIGHT
- REPORT
Direction represents the types of directions that the robot can face:
- NORTH
- SOUTH
- WEST
- EAST