R: Move move() into Heading.move(Position)

This commit is contained in:
Paul Hameteman 2025-10-14 22:52:06 +02:00
commit 84e78f18d3
3 changed files with 31 additions and 29 deletions

View file

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

View file

@ -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<Int, Int> = Pair(x + deltaX, y + deltaY)
override fun toString(): String = symbol.toString()
companion object {

View file

@ -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() {