diff --git a/TECHDEBT.md b/TECHDEBT.md index 555cbb8..efa397a 100644 --- a/TECHDEBT.md +++ b/TECHDEBT.md @@ -1,18 +1,4 @@ # TODO -- [ ] Reorder Responsibilities - - [ ] long class - - [ ] feature envy - - [ ] inappropriate intimacy - - [ ] data class - - [ ] message chain - -# Mikado -- [ ] Change var to val (immutable) - - [x] Rover.turnLeft use copy() on state - - [x] Rover.turnRight use copy() on state - - [x] Rover.move use copy() on state - - [x] Rover.constructor use copy() on state - - [x] Change RoverState to Data Class # RPP - [ ] Refine Abstractions @@ -35,7 +21,13 @@ - [x] Complexity - [x] long method - [x] duplicated code - - +- [x] Reorder Responsibilities + - [x] long class + - [x] feature envy + - [x] inappropriate intimacy + - [x] data class + - [x] message chain + # Mikado - [x] Clean up constants and private fields - [x] Move move() into Heading.move(Position) @@ -48,3 +40,9 @@ - [x] state.heading in Rover.constructor to enum - [x] Change heading Char in RoverState to Heading enum - [x] Create Heading enum +- [x] Change var to val (immutable) + - [x] Rover.turnLeft use copy() on state + - [x] Rover.turnRight use copy() on state + - [x] Rover.move use copy() on state + - [x] Rover.constructor use copy() on state + - [x] Change RoverState to Data Class diff --git a/src/main/kotlin/org/example/Rover.kt b/src/main/kotlin/org/example/Rover.kt index 4a81f94..79ba25b 100644 --- a/src/main/kotlin/org/example/Rover.kt +++ b/src/main/kotlin/org/example/Rover.kt @@ -17,27 +17,16 @@ class Rover { fun go(instructions: String) { for (instruction in instructions) { - when (instruction) { - COMMAND_TURN_LEFT -> turnLeft() - COMMAND_TURN_RIGHT -> turnRight() - COMMAND_MOVE -> move() - } + state = + when (instruction) { + COMMAND_TURN_LEFT -> state.turnLeft() + COMMAND_TURN_RIGHT -> state.turnRight() + COMMAND_MOVE -> state.move() + else -> state + } } } - private fun move() { - val (updatedX, updatedY) = state.heading.move(state.positionX, state.positionY) - state = state.copy(positionX = updatedX, positionY = updatedY) - } - - private fun turnRight() { - state = state.copy(heading = state.heading.turnRight()) - } - - private fun turnLeft() { - state = state.copy(heading = state.heading.turnLeft()) - } - val position: String get() = "${state.positionX} ${state.positionY} ${state.heading}" diff --git a/src/main/kotlin/org/example/RoverState.kt b/src/main/kotlin/org/example/RoverState.kt index ba4fe98..18f63a5 100644 --- a/src/main/kotlin/org/example/RoverState.kt +++ b/src/main/kotlin/org/example/RoverState.kt @@ -4,4 +4,13 @@ data class RoverState( val positionX: Int = 0, val positionY: Int = 0, var heading: Heading = Heading.NORTH, -) +) { + fun turnLeft(): RoverState = copy(heading = heading.turnLeft()) + + fun turnRight(): RoverState = copy(heading = heading.turnRight()) + + fun move(): RoverState { + val (newX, newY) = heading.move(positionX, positionY) + return copy(positionX = newX, positionY = newY) + } +}