diff --git a/kotlin/robot-simulator/src/main/kotlin/Robot.kt b/kotlin/robot-simulator/src/main/kotlin/Robot.kt new file mode 100644 index 0000000..42858d5 --- /dev/null +++ b/kotlin/robot-simulator/src/main/kotlin/Robot.kt @@ -0,0 +1,42 @@ +class Robot(var gridPosition: GridPosition, var orientation: Orientation) { + constructor(): this(GridPosition(0,0), Orientation.NORTH) + + fun advance(): Unit { + gridPosition = when (orientation) { + Orientation.NORTH -> gridPosition.copy(y = gridPosition.y + 1) + Orientation.SOUTH -> gridPosition.copy(y = gridPosition.y - 1) + Orientation.EAST -> gridPosition.copy(x = gridPosition.x + 1) + Orientation.WEST -> gridPosition.copy(x = gridPosition.x - 1) + } + } + + fun turnRight(): Unit { + orientation = when (orientation){ + Orientation.NORTH -> Orientation.EAST + Orientation.EAST -> Orientation.SOUTH + Orientation.SOUTH -> Orientation.WEST + Orientation.WEST -> Orientation.NORTH + } + } + + fun turnLeft(): Unit { + orientation = when (orientation){ + Orientation.NORTH -> Orientation.WEST + Orientation.WEST -> Orientation.SOUTH + Orientation.SOUTH -> Orientation.EAST + Orientation.EAST -> Orientation.NORTH + } + } + + fun simulate(inpString: String): Unit { + require(inpString.all { (it == 'R') or (it == 'L') or (it == 'A') }) + for (i in inpString){ + when (i.toUpperCase()){ + 'R' -> this.turnRight() + 'L' -> this.turnLeft() + 'A' -> this.advance() + } + } + } + +} \ No newline at end of file