Skip to content

ooanishoo/toy-robot-challenge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

39 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Toy Robot Code Challenge

toy-robot

Table of Contents:

  1. Introduction
  2. Prerequisites
  3. Installation
  4. Running Tests
  5. Running Robot Simulation
  6. Other available scripts
  7. Folder structure
  8. Problem Statement
  9. Solution Approach

Introduction

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.

Prerequisites

Before setting up and running the project, ensure you have node on your machine. Run the following command in terminal to verify

node --version

Installation

  1. Clone the repository to your local machine:
git clone https://github.com/ooanishoo/toy-robot-challenge.git
  1. Navigate to the project directory:
cd toy-robot-challenge
  1. Install the required dependencies:
npm install

Running Tests

To run all the tests, run the following command:

npm test

Running Robot Simulation

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.

Other available scripts

To compile Typescript files to Javascript files to dist folder with tsc, run the following command:

npm run build

Folder structure:

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

Problem Statement

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

Solution Approach

Table

A table can be constructed by providing dimensions: width and height

const table = new Table(5,5);

Command

CommandType represents the following types of commands that can be executed by the robot:

  • PLACE
  • MOVE
  • LEFT
  • RIGHT
  • REPORT

Direction

Direction represents the types of directions that the robot can face:

  • NORTH
  • SOUTH
  • WEST
  • EAST