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 - [ ] middle man
# Mikado # Mikado
- [x] Remove deltaX, deltaY in Heading - [x] Remove posX, posY from RoverState
- [x] NORTH, etc -> Vector(0, 1) - [x] use position in Rover.position
- [x] move -> return Pair but using vector - [x] Create toString in Position
- [x] Add Vector to enum class Heading - [x] use position in Rover constructor()
- [x] Create Vector data class - [x] use position in move()
- [x] Add position to RoverState
- [x] Create Position data class
# RPP # RPP
- [ ] Design Patterns - [ ] Design Patterns
@ -53,3 +55,8 @@
- [x] Rover.move use copy() on state - [x] Rover.move use copy() on state
- [x] Rover.constructor use copy() on state - [x] Rover.constructor use copy() on state
- [x] Change RoverState to Data Class - [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) { constructor(commands: String) {
val command = commands.split(' ') val command = commands.split(' ')
if (command.size >= ROVER_MINIMUM_NEEDED_COMMANDS) { if (command.size >= ROVER_MINIMUM_NEEDED_COMMANDS) {
state = val position =
state.copy( Position(
positionX = command[ROVER_STARTING_POSITION_X].toInt(), x = command[ROVER_STARTING_POSITION_X].toInt(),
positionY = command[ROVER_STARTING_POSITION_Y].toInt(), y = command[ROVER_STARTING_POSITION_Y].toInt(),
) )
state = state.copy(position = position)
Heading.from(command[ROVER_FACING_DIRECTION][ROVER_COMMANDLIST_DIRECTION])?.let { Heading.from(command[ROVER_FACING_DIRECTION][ROVER_COMMANDLIST_DIRECTION])?.let {
state = state.copy(heading = it) state = state.copy(heading = it)
} }
@ -28,7 +29,7 @@ class Rover {
} }
val position: String val position: String
get() = "${state.positionX} ${state.positionY} ${state.heading}" get() = "${state.position} ${state.heading}"
fun pos(): String = position fun pos(): String = position

View file

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