R: Finish refine abstractions (add moveBy to position class and use in RS)

This commit is contained in:
Paul Hameteman 2025-10-15 00:31:01 +02:00
commit af40a996fe
4 changed files with 15 additions and 11 deletions

View file

@ -1,14 +1,14 @@
# TODO
- [ ] Refine Abstractions
- [ ] long parameter list
- [ ] data clump
- [ ] primitive obsession
- [ ] middle man
- [x] Refine Abstractions
- [x] long parameter list
- [x] data clump
- [x] primitive obsession
- [x] middle man
# Mikado
- [x] Replace x, y with Position in Heading.move()
- [x] RoverState.move use position
- [x] Return position in move() instead of Pair
- [x] remove move() from heading
- [x] use position.moveBy in RoverState.move
- [x] add moveBy to Position class
# RPP
- [ ] Design Patterns
@ -63,3 +63,7 @@
- [x] use position in move()
- [x] Add position to RoverState
- [x] Create Position data class
- [x] Replace x, y with Position in Heading.move()
- [x] RoverState.move use position
- [x] Return position in move() instead of Pair

View file

@ -23,8 +23,6 @@ enum class Heading(val symbol: Char, val vector: Vector) {
NORTH -> EAST
}
fun move(position: Position): Position = position.copy(x = position.x + vector.deltaX, position.y + vector.deltaY)
override fun toString(): String = symbol.toString()
companion object {

View file

@ -4,5 +4,7 @@ data class Position(
val x: Int = 0,
val y: Int = 0,
) {
fun moveBy(vector: Vector): Position = copy(x = x + vector.deltaX, y = y + vector.deltaY)
override fun toString(): String = "$x $y"
}

View file

@ -8,5 +8,5 @@ data class RoverState(
fun turnRight(): RoverState = copy(heading = heading.turnRight())
fun move(): RoverState = copy(position = heading.move(position))
fun move(): RoverState = copy(position = position.moveBy(heading.vector))
}