Scala Tutorial Through Katas: Mars Rover (Medium)

A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.

This article is part of the series “Scala Tutorial Through Katas”. Articles are divided into easy, medium and hard. Beginners should start with easy ones and move towards more complicated once they feel more comfortable programming in Scala.

For the complete list of Scala katas and solutions please visit the index page

The article assumes that the reader is familiar with the basic usage of ScalaTest asserts and knows how to run them from his favorite Scala IDE (ours is IntelliJ IDEA with the Scala plugin).

Tests that prove that the solution is correct are displayed below. Recommended way to solve this kata is to 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.

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.

  • 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.

Following is a set of unit tests that can be used to solve this kata in the TDD fashion.

[UNIT TESTS]

class MarsRoverTest extends FlatSpec with Matchers {

  "Planet" can "define its size" in {
    val mars = Planet(5, 6)
    mars.coordinates.x.location should be (5)
    mars.coordinates.y.location should be (6)
  }

  "Rover" should "accept starting point (X,Y) of a rover and the direction (N,S,E,W) it is facing" in {
    val rover = Rover(12, 42, 'E')
    rover.coordinates.x.location should be (12)
    rover.coordinates.y.location should be (42)
    rover.direction should be ('E')
  }

  it should "be able to move forward (f)" in {
    val rover = Rover(12, 42, 'E')
    rover.sendCommands("f")
    rover.coordinates.x.location should be (13)
    rover.coordinates.y.location should be (42)
  }

  it should "be able to move backward (b)" in {
    val rover = Rover(12, 42, 'E')
    rover.sendCommands("b")
    rover.coordinates.x.location should be (11)
    rover.coordinates.y.location should be (42)
  }

  it should "be able to turn left (l)" in {
    val rover = Rover(12, 42, 'N')
    rover.sendCommands("l")
    rover.direction should be ('W')
  }

  it should "be able to receive a character array of commands" in {
    val rover = Rover(12, 42, 'E')
    rover.sendCommands("flf")
    rover.coordinates.x.location should be (13)
    rover.coordinates.y.location should be (43)
    rover.direction should be ('N')
  }

  it should "wrap from one edge of the grid to another" in {
    val rover = Rover(1, 1, 'E', Planet(3, 3))
    rover.sendCommands("fff")
    rover.coordinates.x.location should be (1)
    rover.sendCommands("rfff")
    rover.coordinates.y.location should be (1)
  }

  it should "report OK and array of commands if no obstacle was found" in {
    val rover = Rover(12, 42, 'E')
    rover.sendCommands("f") should be ("OK: f")
  }

  it should "report NOK and array of commands that lead to an obstacle" in {
    val rover = Rover(1, 1, 'N', Planet(10, 10, List(Coordinates(1, 3))))
    rover.sendCommands("ff") should be ("NOK: f")
  }

}

Test code can be found in the GitHub MarsRover.scala.

[ONE POSSIBLE SOLUTION]

class Rover(val coordinates: Coordinates, var direction: Char, val planet: Planet) {

  val compass = Seq('N', 'E', 'S', 'W')

  def sendCommands(commands: String): String = {
    def sendCommands(commands: List[Char], performed: String): String = {
      commands.head match {
        case 'f' => if(!move(1)) return performed
        case 'b' => if(!move(-1)) return performed
        case 'l' => turn(-1)
        case 'r' => turn(1)
      }
      if (commands.tail == Nil) performed + commands.head.toString
      else sendCommands(commands.tail, performed + commands.head.toString)
    }
    val performed = sendCommands(commands.toList, "")
    (if (performed == commands) "OK" else "NOK") + ": " + performed
  }

  private def move(stepDirection: Int): Boolean = {
    val newCoordinates = coordinates.step(stepDirection, direction, planet.coordinates)
    if (planet.hasObstacle(newCoordinates)) false
    else { coordinates.update(newCoordinates); true }
  }

  private def turn(turnDirection: Int) {
    direction = compass((4 + compass.indexOf(direction) + turnDirection) % 4)
  }

}
object Rover {
  def apply(initialX: Int, initialY: Int, direction: Char, planet: Planet = Planet()) = {
    new Rover(Coordinates(initialX, initialY), direction, planet)
  }
}

class Planet(val coordinates: Coordinates = Coordinates(100, 100), obstacles: List[Coordinates] = List()) {
  def hasObstacle(location: Coordinates): Boolean = {
    for (coordinates <- obstacles) {
      if (coordinates.x == location.x && coordinates.y == location.y) {
        return true
      }
    }
    false
  }
}
object Planet {
  def apply(maxX: Int = 100, maxY: Int = 100, obstacles: List[Coordinates] = List()) = {
    new Planet(Coordinates(maxX, maxY), obstacles)
  }
}

class Coordinates(val x: Point, val y: Point) {
  def step(stepDirection: Int, direction: Char, planetCoordinates: Coordinates): Coordinates = {
    var stepX, stepY = 0
    direction match {
      case 'N' => stepY = 1
      case 'S' => stepY = -1
      case 'E' => stepX = 1
      case 'W' => stepX = -1
    }
    Coordinates(
      x.step(stepDirection, stepX, planetCoordinates.x.location),
      y.step(stepDirection, stepY, planetCoordinates.y.location))
  }
  def update(coordinates: Coordinates) {
    x.location = coordinates.x.location
    y.location = coordinates.y.location
  }
}
object Coordinates {
  def apply(x: Int, y: Int) = new Coordinates(new Point(x), new Point(y))
}

class Point(var location: Int) {
  def step(direction: Int, step: Int, max: Int): Int = (max + location + step * direction) % max
  override def equals(that: Any): Boolean = this.hashCode() == that.hashCode()
  override def hashCode = location.hashCode
}

The solution code can be found in MarsRover.scala solution.

Alternative (and better) solution done by Nishruu can be found here.

What was your solution? Post it as a comment so that we can compare different ways to solve this kata.

About these ads

2 thoughts on “Scala Tutorial Through Katas: Mars Rover (Medium)

    • Let’s say that the planet grid is 5×5 and the rover is on the position 4×2 facing east. Command ‘f’ would take it to the position 5×2. Another command ‘f’ would end with the location 1×2. Since the grid is 5×5, rover cannot be on the position 6×2.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s