From af40a996fe2583380f52579a34c742dd3e01e1d7 Mon Sep 17 00:00:00 2001 From: Paul Hameteman Date: Wed, 15 Oct 2025 00:31:01 +0200 Subject: [PATCH] R: Finish refine abstractions (add moveBy to position class and use in RS) --- TECHDEBT.md | 20 ++++++++++++-------- src/main/kotlin/org/example/Heading.kt | 2 -- src/main/kotlin/org/example/Position.kt | 2 ++ src/main/kotlin/org/example/RoverState.kt | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/TECHDEBT.md b/TECHDEBT.md index 7b355c9..3e8c4a6 100644 --- a/TECHDEBT.md +++ b/TECHDEBT.md @@ -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 + diff --git a/src/main/kotlin/org/example/Heading.kt b/src/main/kotlin/org/example/Heading.kt index 39eabe8..41525b5 100644 --- a/src/main/kotlin/org/example/Heading.kt +++ b/src/main/kotlin/org/example/Heading.kt @@ -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 { diff --git a/src/main/kotlin/org/example/Position.kt b/src/main/kotlin/org/example/Position.kt index 75cc7cc..e259c0c 100644 --- a/src/main/kotlin/org/example/Position.kt +++ b/src/main/kotlin/org/example/Position.kt @@ -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" } diff --git a/src/main/kotlin/org/example/RoverState.kt b/src/main/kotlin/org/example/RoverState.kt index 97b442e..5bb121c 100644 --- a/src/main/kotlin/org/example/RoverState.kt +++ b/src/main/kotlin/org/example/RoverState.kt @@ -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)) }