diff --git a/TECHDEBT.md b/TECHDEBT.md index a6bdd83..c4d43ae 100644 --- a/TECHDEBT.md +++ b/TECHDEBT.md @@ -1,28 +1,12 @@ # TODO -- [ ] Complexity - - [x] long method - - [ ] duplicated code - - -# Mikado -- [ ] Clean up constants and private fields - - [ ] Move move() into Heading.move(Position) - - [x] Move turnLeft() into Heading.turnLeft() - - [x] Move turnRight() into Heading.turnRight() - - [x] Remove HEADING_* constants - - [x] in move() compare to heading enum - - [x] in turnRight() compare to heading enum - - [x] in turnLeft() compare to heading enum - - [x] state.heading in Rover.constructor to enum - - [x] Change heading Char in RoverState to Heading enum - - [x] Create Heading enum - -# RPP - [ ] Reorder Responsibilities - [ ] long class - [ ] feature envy - [ ] inappropriate intimacy - [ ] data class - [ ] message chain + +# RPP - [ ] Refine Abstractions - [ ] long parameter list - [ ] data clump @@ -40,3 +24,19 @@ - [x] bad naming - [x] antipattern - [x] scopes +- [x] Complexity + - [x] long method + - [x] duplicated code + - +# Mikado +- [x] Clean up constants and private fields + - [x] Move move() into Heading.move(Position) + - [x] Move turnLeft() into Heading.turnLeft() + - [x] Move turnRight() into Heading.turnRight() + - [x] Remove HEADING_* constants + - [x] in move() compare to heading enum + - [x] in turnRight() compare to heading enum + - [x] in turnLeft() compare to heading enum + - [x] state.heading in Rover.constructor to enum + - [x] Change heading Char in RoverState to Heading enum + - [x] Create Heading enum diff --git a/src/main/kotlin/org/example/Heading.kt b/src/main/kotlin/org/example/Heading.kt index bc8df7d..0b36c19 100644 --- a/src/main/kotlin/org/example/Heading.kt +++ b/src/main/kotlin/org/example/Heading.kt @@ -1,10 +1,10 @@ package org.example -enum class Heading(val symbol: Char) { - NORTH('N'), - EAST('E'), - SOUTH('S'), - WEST('W'), +enum class Heading(val symbol: Char, val deltaX: Int, val deltaY: Int) { + NORTH('N', 0, 1), + EAST('E', 1, 0), + SOUTH('S', 0, -1), + WEST('W', -1, 0), ; fun turnLeft(): Heading = @@ -23,6 +23,11 @@ enum class Heading(val symbol: Char) { NORTH -> EAST } + fun move( + x: Int, + y: Int, + ): Pair = Pair(x + deltaX, y + deltaY) + override fun toString(): String = symbol.toString() companion object { diff --git a/src/main/kotlin/org/example/Rover.kt b/src/main/kotlin/org/example/Rover.kt index 3afa41b..3514468 100644 --- a/src/main/kotlin/org/example/Rover.kt +++ b/src/main/kotlin/org/example/Rover.kt @@ -21,12 +21,9 @@ class Rover { } private fun move() { - when (state.heading) { - Heading.EAST -> state.positionX++ - Heading.SOUTH -> state.positionY-- - Heading.WEST -> state.positionX-- - Heading.NORTH -> state.positionY++ - } + val (updatedX, updatedY) = state.heading.move(state.positionX, state.positionY) + state.positionX = updatedX + state.positionY = updatedY } private fun turnRight() {