R: Data clump: Position

This commit is contained in:
Paul Hameteman 2025-10-15 00:13:58 +02:00
commit 61a4d417b8
4 changed files with 29 additions and 14 deletions

View file

@ -6,11 +6,13 @@
- [ ] middle man
# Mikado
- [x] Remove deltaX, deltaY in Heading
- [x] NORTH, etc -> Vector(0, 1)
- [x] move -> return Pair but using vector
- [x] Add Vector to enum class Heading
- [x] Create Vector data class
- [x] Remove posX, posY from RoverState
- [x] use position in Rover.position
- [x] Create toString in Position
- [x] use position in Rover constructor()
- [x] use position in move()
- [x] Add position to RoverState
- [x] Create Position data class
# RPP
- [ ] Design Patterns
@ -53,3 +55,8 @@
- [x] Rover.move use copy() on state
- [x] Rover.constructor use copy() on state
- [x] Change RoverState to Data Class
- [x] Remove deltaX, deltaY in Heading
- [x] NORTH, etc -> Vector(0, 1)
- [x] move -> return Pair but using vector
- [x] Add Vector to enum class Heading
- [x] Create Vector data class

View file

@ -0,0 +1,8 @@
package org.example
data class Position(
val x: Int = 0,
val y: Int = 0,
) {
override fun toString(): String = "$x $y"
}

View file

@ -4,11 +4,12 @@ class Rover {
constructor(commands: String) {
val command = commands.split(' ')
if (command.size >= ROVER_MINIMUM_NEEDED_COMMANDS) {
state =
state.copy(
positionX = command[ROVER_STARTING_POSITION_X].toInt(),
positionY = command[ROVER_STARTING_POSITION_Y].toInt(),
val position =
Position(
x = command[ROVER_STARTING_POSITION_X].toInt(),
y = command[ROVER_STARTING_POSITION_Y].toInt(),
)
state = state.copy(position = position)
Heading.from(command[ROVER_FACING_DIRECTION][ROVER_COMMANDLIST_DIRECTION])?.let {
state = state.copy(heading = it)
}
@ -28,7 +29,7 @@ class Rover {
}
val position: String
get() = "${state.positionX} ${state.positionY} ${state.heading}"
get() = "${state.position} ${state.heading}"
fun pos(): String = position

View file

@ -1,8 +1,7 @@
package org.example
data class RoverState(
val positionX: Int = 0,
val positionY: Int = 0,
val position: Position = Position(),
var heading: Heading = Heading.NORTH,
) {
fun turnLeft(): RoverState = copy(heading = heading.turnLeft())
@ -10,7 +9,7 @@ data class RoverState(
fun turnRight(): RoverState = copy(heading = heading.turnRight())
fun move(): RoverState {
val (newX, newY) = heading.move(positionX, positionY)
return copy(positionX = newX, positionY = newY)
val (newX, newY) = heading.move(position.x, position.y)
return copy(position = Position(newX, newY))
}
}