R: Reorder responsibilities

This commit is contained in:
Paul Hameteman 2025-10-14 23:22:28 +02:00
commit 2c372a8f51
3 changed files with 30 additions and 34 deletions

View file

@ -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

View file

@ -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}"

View file

@ -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)
}
}