From 0ca418f0b56bd4750a46af1dc4534173a1356c9f Mon Sep 17 00:00:00 2001 From: Paul Hameteman Date: Tue, 14 Oct 2025 23:09:35 +0200 Subject: [PATCH] R: Rover.move use copy() & make positionX and positionY immutable --- TECHDEBT.md | 2 +- src/main/kotlin/org/example/Rover.kt | 3 +-- src/main/kotlin/org/example/RoverState.kt | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/TECHDEBT.md b/TECHDEBT.md index 7af802f..ed1a666 100644 --- a/TECHDEBT.md +++ b/TECHDEBT.md @@ -10,7 +10,7 @@ - [ ] Change var to val (immutable) - [ ] Rover.turnLeft 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] Change RoverState to Data Class diff --git a/src/main/kotlin/org/example/Rover.kt b/src/main/kotlin/org/example/Rover.kt index bcf5d7a..e88a8f7 100644 --- a/src/main/kotlin/org/example/Rover.kt +++ b/src/main/kotlin/org/example/Rover.kt @@ -25,8 +25,7 @@ class Rover { private fun move() { val (updatedX, updatedY) = state.heading.move(state.positionX, state.positionY) - state.positionX = updatedX - state.positionY = updatedY + state = state.copy(positionX = updatedX, positionY = updatedY) } private fun turnRight() { diff --git a/src/main/kotlin/org/example/RoverState.kt b/src/main/kotlin/org/example/RoverState.kt index f0af6ed..ba4fe98 100644 --- a/src/main/kotlin/org/example/RoverState.kt +++ b/src/main/kotlin/org/example/RoverState.kt @@ -1,7 +1,7 @@ package org.example data class RoverState( - var positionX: Int = 0, - var positionY: Int = 0, + val positionX: Int = 0, + val positionY: Int = 0, var heading: Heading = Heading.NORTH, )