R: Rover.move use copy() & make positionX and positionY immutable

This commit is contained in:
Paul Hameteman 2025-10-14 23:09:35 +02:00
commit 0ca418f0b5
3 changed files with 4 additions and 5 deletions

View file

@ -10,7 +10,7 @@
- [ ] Change var to val (immutable) - [ ] Change var to val (immutable)
- [ ] Rover.turnLeft use copy() on state - [ ] Rover.turnLeft use copy() on state
- [ ] Rover.turnRight use copy() on state - [ ] Rover.turnRight use copy() on state
- [ ] 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

View file

@ -25,8 +25,7 @@ class Rover {
private fun move() { private fun move() {
val (updatedX, updatedY) = state.heading.move(state.positionX, state.positionY) val (updatedX, updatedY) = state.heading.move(state.positionX, state.positionY)
state.positionX = updatedX state = state.copy(positionX = updatedX, positionY = updatedY)
state.positionY = updatedY
} }
private fun turnRight() { private fun turnRight() {

View file

@ -1,7 +1,7 @@
package org.example package org.example
data class RoverState( data class RoverState(
var positionX: Int = 0, val positionX: Int = 0,
var positionY: Int = 0, val positionY: Int = 0,
var heading: Heading = Heading.NORTH, var heading: Heading = Heading.NORTH,
) )