R: Move move() into Heading.move(Position)
This commit is contained in:
parent
4e3d9177af
commit
84e78f18d3
3 changed files with 31 additions and 29 deletions
|
|
@ -1,10 +1,10 @@
|
|||
package org.example
|
||||
|
||||
enum class Heading(val symbol: Char) {
|
||||
NORTH('N'),
|
||||
EAST('E'),
|
||||
SOUTH('S'),
|
||||
WEST('W'),
|
||||
enum class Heading(val symbol: Char, val deltaX: Int, val deltaY: Int) {
|
||||
NORTH('N', 0, 1),
|
||||
EAST('E', 1, 0),
|
||||
SOUTH('S', 0, -1),
|
||||
WEST('W', -1, 0),
|
||||
;
|
||||
|
||||
fun turnLeft(): Heading =
|
||||
|
|
@ -23,6 +23,11 @@ enum class Heading(val symbol: Char) {
|
|||
NORTH -> EAST
|
||||
}
|
||||
|
||||
fun move(
|
||||
x: Int,
|
||||
y: Int,
|
||||
): Pair<Int, Int> = Pair(x + deltaX, y + deltaY)
|
||||
|
||||
override fun toString(): String = symbol.toString()
|
||||
|
||||
companion object {
|
||||
|
|
|
|||
|
|
@ -21,12 +21,9 @@ class Rover {
|
|||
}
|
||||
|
||||
private fun move() {
|
||||
when (state.heading) {
|
||||
Heading.EAST -> state.positionX++
|
||||
Heading.SOUTH -> state.positionY--
|
||||
Heading.WEST -> state.positionX--
|
||||
Heading.NORTH -> state.positionY++
|
||||
}
|
||||
val (updatedX, updatedY) = state.heading.move(state.positionX, state.positionY)
|
||||
state.positionX = updatedX
|
||||
state.positionY = updatedY
|
||||
}
|
||||
|
||||
private fun turnRight() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue