Created
June 14, 2018 19:27
-
-
Save dpolivaev/bec6aaf4f8020a9ea151c787cab7ad98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalatest.Matchers | |
case class Point(x: Int, y: Int) | |
sealed trait Direction | |
case object N extends Direction | |
case object S extends Direction | |
case object W extends Direction | |
case object E extends Direction | |
case class Rover(position: Point, direction: Direction) { | |
def explore(commands: List[Char]):Rover = commands match { | |
case head :: tail => (this match { | |
case Rover(_, N) => Rover(Point(0, position.y + 1), N) | |
case Rover(_, S) => Rover(Point(0, position.y-1), S) | |
case Rover(_, W) => Rover(Point(position.x - 1, 0), W) | |
case Rover(_, E) => Rover(Point(position.x + 1, 0), E) | |
}).explore(tail) | |
case Nil => this | |
} | |
} | |
class MarsRoverTest extends org.scalatest.FunSpec with Matchers { | |
describe("mars rover starting at point 0, 0") { | |
val initialPoint = Point(x = 0, y = 0) | |
def rover0_0(direction: Direction) = { | |
Rover(position = initialPoint, direction = direction) | |
} | |
it("remains at the same position with the same direction when calles wuth empty command list") { | |
val rover = rover0_0(N) | |
val emptyCommandList = List() | |
val newRover = rover.explore(emptyCommandList) | |
newRover shouldEqual rover | |
} | |
describe("given command forward") { | |
val goForward = List('f') | |
describe("once") { | |
it("moves 1 row up facing north") { | |
val rover = rover0_0(N) | |
val newRover = rover.explore(goForward) | |
newRover shouldEqual Rover(position = Point(x = 0, y = 1), direction = N) | |
} | |
it("moves 1 row down facing south") { | |
val rover = rover0_0(S) | |
val newRover = rover.explore(goForward) | |
newRover shouldEqual Rover(position = Point(x = 0, y = -1), direction = S) | |
} | |
it("moves 1 row left facing west") { | |
val rover = rover0_0(W) | |
val newRover = rover.explore(goForward) | |
newRover shouldEqual Rover(position = Point(x = -1, y = 0), direction = W) | |
} | |
it("moves 1 row right facing east") { | |
val rover = rover0_0(E) | |
val newRover = rover.explore(goForward) | |
newRover shouldEqual Rover(position = Point(x = 1, y = 0), direction = E) | |
} | |
} | |
describe("twice") { | |
val goForwardTwice = goForward ++ goForward | |
it("moves 2 rows up facing north") { | |
val rover = rover0_0(N) | |
val newRover = rover.explore(goForwardTwice) | |
newRover shouldEqual Rover(position = Point(x = 0, y = 2), direction = N) | |
} | |
it("moves 2 row down facing south") { | |
val rover = rover0_0(S) | |
val newRover = rover.explore(goForwardTwice) | |
newRover shouldEqual Rover(position = Point(x = 0, y = -2), direction = S) | |
} | |
it("moves 2 row left facing west") { | |
val rover = rover0_0(W) | |
val newRover = rover.explore(goForwardTwice) | |
newRover shouldEqual Rover(position = Point(x = -2, y = 0), direction = W) | |
} | |
it("moves 2 row right facing east") { | |
val rover = rover0_0(E) | |
val newRover = rover.explore(goForwardTwice) | |
newRover shouldEqual Rover(position = Point(x = 2, y = 0), direction = E) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment