NOTE: I used PHP7.4 because I have wamp with this version for now and can't upgrade it because some of application is setup there.
My approach was to ensure that I use object oriented programming principles. Objects were created for both the Plateau and Rover with constructors and getters and setters.
I solved the problem of turning left and right through cardinal directions by declaring them in a numbered array which I could then increment and decrement through using the array indexes.
Turning right from the last element or left from the first is controlled for and the array effectively loops around to allow continuous turning.
Movement is done through simply incrementing or decrementing the x and y values depending on the current direction. Movement is also restricted by the boundaries of the plateau and by any parked rovers.
- The backend challenge was completed using PHP7.4.
- I spent a bit of extra time for create test cases to make sure that everything is working as expected.
- I did not use a framework like symfony for the backend as this would have been overkill for a project with a limited scope and lifespan.
As per our requirement (created test cases), Assuming a top right grid input of x = 5 and y = 5 and two rovers with the following data:
Rover 1: X-Axis Start: 1 Y-Axis Start: 2 Direction: N Move Commands: LMLMLMLMM
Rover 2: X-Axis Start: 3 Y-Axis Start: 3 Direction: E Move Commands: MMRMMRMRRM
You could expect to see the two rovers end up in the following positions and directions:
Rover 1: 1 3 N
Rover 2: 5 1 E
A web server capable of running PHP
A modern web browser
- make a clone of this application.
- run 'composer install' command in project directory to download third party vendor directory.
- for test the requirement, run the test cases like using below steps.
Test are run with phpunit
- Open a terminal and navigate to the project root folder
- Run the following command:
vendor/bin/phpunit --colors=always tests/RoverTest.php
Make a test using command line
- Open a terminal and navigate to the project root folder
- Run the following command:
php app/app.php
- add the input and can see the output like attached screenshot above.
- Sandip Limbachiya