R: Reorder responsibilities
This commit is contained in:
parent
c5ce275612
commit
2c372a8f51
3 changed files with 30 additions and 34 deletions
28
TECHDEBT.md
28
TECHDEBT.md
|
|
@ -1,18 +1,4 @@
|
||||||
# TODO
|
# 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
|
# RPP
|
||||||
- [ ] Refine Abstractions
|
- [ ] Refine Abstractions
|
||||||
|
|
@ -35,7 +21,13 @@
|
||||||
- [x] Complexity
|
- [x] Complexity
|
||||||
- [x] long method
|
- [x] long method
|
||||||
- [x] duplicated code
|
- [x] duplicated code
|
||||||
-
|
- [x] Reorder Responsibilities
|
||||||
|
- [x] long class
|
||||||
|
- [x] feature envy
|
||||||
|
- [x] inappropriate intimacy
|
||||||
|
- [x] data class
|
||||||
|
- [x] message chain
|
||||||
|
|
||||||
# Mikado
|
# Mikado
|
||||||
- [x] Clean up constants and private fields
|
- [x] Clean up constants and private fields
|
||||||
- [x] Move move() into Heading.move(Position)
|
- [x] Move move() into Heading.move(Position)
|
||||||
|
|
@ -48,3 +40,9 @@
|
||||||
- [x] state.heading in Rover.constructor to enum
|
- [x] state.heading in Rover.constructor to enum
|
||||||
- [x] Change heading Char in RoverState to Heading enum
|
- [x] Change heading Char in RoverState to Heading enum
|
||||||
- [x] Create 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
|
||||||
|
|
|
||||||
|
|
@ -17,27 +17,16 @@ class Rover {
|
||||||
|
|
||||||
fun go(instructions: String) {
|
fun go(instructions: String) {
|
||||||
for (instruction in instructions) {
|
for (instruction in instructions) {
|
||||||
when (instruction) {
|
state =
|
||||||
COMMAND_TURN_LEFT -> turnLeft()
|
when (instruction) {
|
||||||
COMMAND_TURN_RIGHT -> turnRight()
|
COMMAND_TURN_LEFT -> state.turnLeft()
|
||||||
COMMAND_MOVE -> move()
|
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
|
val position: String
|
||||||
get() = "${state.positionX} ${state.positionY} ${state.heading}"
|
get() = "${state.positionX} ${state.positionY} ${state.heading}"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,4 +4,13 @@ data class RoverState(
|
||||||
val positionX: Int = 0,
|
val positionX: Int = 0,
|
||||||
val positionY: Int = 0,
|
val positionY: Int = 0,
|
||||||
var heading: Heading = Heading.NORTH,
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue