Simulation of a vehicle on a planet according to the rules of Mars Rover Kata.
The application should:
- Read a .txt file containing:
- World map (grid)
- A set of commands that the rover will receive
- Print the rover's position on the screen after each command
- Rover Starting point: (x,y,direction):(0,0,N)
- Rover will accept the following instructions:
- L: Left - Turn rover 90deg to left
- R: Right - Turn rover 90deg to right
- F: Forward - Move rover to direction
- B: Backward - Move the rover in opposite direction
- Edge cases: If the rover continues to move after touching the edge of the grid, it will enter in the opposite coords
- Example: Grid 5x4, when rover touches (5,0) (0 based position) it will enter in the grid in (0,0)
- Grid could contain obstacles
- When rover touches obstacles, it can't move in direction of the obstacle.
- After each command string, the rover will send the following string to the central controller: [O:]::
- Where x is the x coord of the rover, y the y coord of the rover, Direction is the active direction of the rover
- If the last command of a set touches an obstacle, the rover will output the [O:] string at the beginning
I used Vite as bootstrapper, it helped me to have a nice and organised directory without too much effort. It also gave me a ready to use local dev environment, bundling and polyfilling
- Clone the repo: https://github.com/herecomesfed/mars-rover.git
- Install dependencies
- On your terminal use:
npm run dev - You can find more details on how to start the rover simulation directly from the app.
- HTML
- CSS
- Vanilla Javascript
- OOP
- TDD
This is the core of the application. It accept the following parameters:
const rover = new MarsRover(x:string, y: string, direction:string, mars:Mars Object, obstacles: array of Obstacles, instructions: array of instructions)
_changeDirection(instruction: string):Turns the rover based on the instruction._moveRover(instruction: string):Move the rover based on the instruction (F: direction + 1, B: direction - 1)._isThereObstacle():Checks if rover will touch an obstacle receiving the next command._updateRoverOutput():Will update the rover when a set of commands is finished.
_readInstructions(instruction: string):Read and perform a single instruction._startSimulation(callbackAfterSingleCommand: function, callbackAfterRowOfCommands: function, callbackAfterLoop: function):Read and perform all instructions, then provide an output string. Accepts three optional callback functions for each stage of the simulation (after each command, after a string of commands, after simulation).
getPosition():Returns the rover's position as (x, y).getDirection():Returns the rover's direction.getFullPosition():Returns the rover's full position as (x, y, direction).
Returns grid coords
const rover = new Mars(x:string, y: string)
Returns single obstacle coords
const rover = new Obstacle(x:string, y: string)
I built this app as an MVP. It is not perfect but it does the job. I learned a lot of thing doing this exercise and got better in logic and view separation.