Java Tutorial Through Katas: Mars Rover
A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.
This article is part of the series Java Tutorial Through Katas.
The article assumes that the reader already has experience with Java, that he is familiar with the basics of unit tests and that he knows how to run them from his favorite IDE (mine is IntelliJ IDEA).
Tests that prove that the solution is correct are displayed below. Recommended way to solve this kata is to use test-driven development approach (write the implementation for the first test, confirm that it passes and move to the next). Once all of the tests pass, the kata can be considered solved. For more information about best practices, please read the Test Driven Development (TDD): Best Practices Using Java Examples.
One possible solution is provided below the tests. Try to solve the kata by yourself first.
Mars Rover
Develop an api that moves a rover around on a grid.
Rules:
- You are given the initial starting point (x,y) of a rover and the direction (N,S,E,W) it is facing.
- The rover receives a character array of commands.
- Implement commands that move the rover forward/backward (f,b).
- Implement commands that turn the rover left/right (l,r).
- Implement wrapping from one edge of the grid to another. (planets are spheres after all)
- Implement obstacle detection before each move to a new square. If a given sequence of commands encounters an obstacle, the rover moves up to the last possible point and reports the obstacle.
Tests
Following is a set of unit tests that can be used to solve this kata in the TDD fashion.
package com.technologyconversations.kata.marsrover; import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import static org.assertj.core.api.Assertions.*; /* Source: http://dallashackclub.com/rover Develop an api that moves a rover around on a grid. * You are given the initial starting point (x,y) of a rover and the direction (N,S,E,W) it is facing. * - The rover receives a character array of commands. * - Implement commands that move the rover forward/backward (f,b). * - Implement commands that turn the rover left/right (l,r). * - Implement wrapping from one edge of the grid to another. (planets are spheres after all) * - Implement obstacle detection before each move to a new square. * If a given sequence of commands encounters an obstacle, the rover moves up to the last possible point and reports the obstacle. */ public class RoverSpec { private Rover rover; private Coordinates roverCoordinates; private final Direction direction = Direction.NORTH; private Point x; private Point y; private List<Obstacle> obstacles; @Before public void beforeRoverTest() { x = new Point(1, 9); y = new Point(2, 9); obstacles = new ArrayList<Obstacle>(); roverCoordinates = new Coordinates(x, y, direction, obstacles); rover = new Rover(roverCoordinates); } @Test public void newInstanceShouldSetRoverCoordinatesAndDirection() { assertThat(rover.getCoordinates()).isEqualToComparingFieldByField(roverCoordinates); } @Test public void receiveSingleCommandShouldMoveForwardWhenCommandIsF() throws Exception { int expected = y.getLocation() + 1; rover.receiveSingleCommand('F'); assertThat(rover.getCoordinates().getY().getLocation()).isEqualTo(expected); } @Test public void receiveSingleCommandShouldMoveBackwardWhenCommandIsB() throws Exception { int expected = y.getLocation() - 1; rover.receiveSingleCommand('B'); assertThat(rover.getCoordinates().getY().getLocation()).isEqualTo(expected); } @Test public void receiveSingleCommandShouldTurnLeftWhenCommandIsL() throws Exception { rover.receiveSingleCommand('L'); assertThat(rover.getCoordinates().getDirection()).isEqualTo(Direction.WEST); } @Test public void receiveSingleCommandShouldTurnRightWhenCommandIsR() throws Exception { rover.receiveSingleCommand('R'); assertThat(rover.getCoordinates().getDirection()).isEqualTo(Direction.EAST); } @Test public void receiveSingleCommandShouldIgnoreCase() throws Exception { rover.receiveSingleCommand('r'); assertThat(rover.getCoordinates().getDirection()).isEqualTo(Direction.EAST); } @Test(expected = Exception.class) public void receiveSingleCommandShouldThrowExceptionWhenCommandIsUnknown() throws Exception { rover.receiveSingleCommand('X'); } @Test public void receiveCommandsShouldBeAbleToReceiveMultipleCommands() throws Exception { int expected = x.getLocation() + 1; rover.receiveCommands("RFR"); assertThat(rover.getCoordinates().getX().getLocation()).isEqualTo(expected); assertThat(rover.getCoordinates().getDirection()).isEqualTo(Direction.SOUTH); } @Test public void receiveCommandShouldWhatFromOneEdgeOfTheGridToAnother() throws Exception { int expected = x.getMaxLocation() + x.getLocation() - 2; rover.receiveCommands("LFFF"); assertThat(rover.getCoordinates().getX().getLocation()).isEqualTo(expected); } @Test public void receiveCommandsShouldStopWhenObstacleIsFound() throws Exception { int expected = x.getLocation() + 1; rover.getCoordinates().setObstacles(Arrays.asList(new Obstacle(expected + 1, y.getLocation()))); rover.getCoordinates().setDirection(Direction.EAST); rover.receiveCommands("FFFRF"); assertThat(rover.getCoordinates().getX().getLocation()).isEqualTo(expected); assertThat(rover.getCoordinates().getDirection()).isEqualTo(Direction.EAST); } @Test public void positionShouldReturnXYAndDirection() throws Exception { rover.receiveCommands("LFFFRFF"); assertThat(rover.getPosition()).isEqualTo("8 X 4 N"); } @Test public void positionShouldReturnNokWhenObstacleIsFound() throws Exception { rover.getCoordinates().setObstacles(Arrays.asList(new Obstacle(x.getLocation() + 1, y.getLocation()))); rover.getCoordinates().setDirection(Direction.EAST); rover.receiveCommands("F"); assertThat(rover.getPosition()).endsWith(" NOK"); } }
One possible solution is following.
package com.technologyconversations.kata.marsrover; /* Method receiveCommands should be used to transmit commands to the rover. */ public class Rover { private Coordinates coordinates; public void setCoordinates(Coordinates value) { coordinates = value; } public Coordinates getCoordinates() { return coordinates; } public Rover(Coordinates coordinatesValue) { setCoordinates(coordinatesValue); } public void receiveCommands(String commands) throws Exception { for (char command : commands.toCharArray()) { if (!receiveSingleCommand(command)) { break; } } } public boolean receiveSingleCommand(char command) throws Exception { switch(Character.toUpperCase(command)) { case 'F': return getCoordinates().moveForward(); case 'B': return getCoordinates().moveBackward(); case 'L': getCoordinates().changeDirectionLeft(); return true; case 'R': getCoordinates().changeDirectionRight(); return true; default: throw new Exception("Command " + command + " is unknown."); } } public String getPosition() { return getCoordinates().toString(); } }
Full source is located in the GitHub repo [https://github.com/vfarcic/mars-rover-kata-java). Above code presents only the code of the main class. There are several other classes/objects with their corresponding specification. Besides tests and implementation, repository includes build.gradle that can be used, among other things, to download AssertJ dependencies and run tests README.md contains short instructions how to set up the project.
What was your solution? Post it as a comment so that we can compare different ways to solve this kata.
Reference: | Java Tutorial Through Katas: Mars Rover from our JCG partner Viktor Farcic at the Technology conversations blog. |