R: Heading turnLeft()

This commit is contained in:
Paul Hameteman 2025-10-14 22:27:53 +02:00
commit a56304bc77
3 changed files with 9 additions and 7 deletions

View file

@ -6,7 +6,7 @@
# Mikado # Mikado
- [ ] Clean up constants and private fields - [ ] Clean up constants and private fields
- [ ] Move move() into Heading.move(Position) - [ ] Move move() into Heading.move(Position)
- [ ] Move turnLeft() into Heading.turnLeft() - [x] Move turnLeft() into Heading.turnLeft()
- [x] Move turnRight() into Heading.turnRight() - [x] Move turnRight() into Heading.turnRight()
- [x] Remove HEADING_* constants - [x] Remove HEADING_* constants
- [x] in move() compare to heading enum - [x] in move() compare to heading enum

View file

@ -7,6 +7,13 @@ enum class Heading(val symbol: Char) {
WEST('W'), WEST('W'),
; ;
fun turnLeft() = when (this) {
EAST -> NORTH
NORTH -> WEST
WEST -> SOUTH
SOUTH -> EAST
}
fun turnRight() = when (this) { fun turnRight() = when (this) {
EAST -> SOUTH EAST -> SOUTH
SOUTH -> WEST SOUTH -> WEST

View file

@ -34,12 +34,7 @@ class Rover {
} }
private fun turnLeft() { private fun turnLeft() {
when (state.heading) { state.heading = state.heading.turnLeft()
Heading.EAST -> state.heading = Heading.NORTH
Heading.NORTH -> state.heading = Heading.WEST
Heading.WEST -> state.heading = Heading.SOUTH
Heading.SOUTH -> state.heading = Heading.EAST
}
} }
val position: String val position: String