R: mikado (heading enum)
This commit is contained in:
parent
ab064c71f7
commit
61e293ee18
4 changed files with 42 additions and 20 deletions
15
TECHDEBT.md
15
TECHDEBT.md
|
|
@ -1,7 +1,20 @@
|
||||||
# TODO
|
# TODO
|
||||||
- [ ] Complexity
|
- [ ] Complexity
|
||||||
- [ ] long method
|
- [x] long method
|
||||||
- [ ] duplicated code
|
- [ ] duplicated code
|
||||||
|
-
|
||||||
|
# Mikado
|
||||||
|
- [ ] Clean up constants and private fields
|
||||||
|
- [ ] Move move() into Heading.move(Position)
|
||||||
|
- [ ] Move turnLeft() into Heading.turnLeft()
|
||||||
|
- [ ] Move turnRight() into Heading.turnRight()
|
||||||
|
- [x] Remove HEADING_* constants
|
||||||
|
- [x] in move() compare to heading enum
|
||||||
|
- [x] in turnRight() compare to heading enum
|
||||||
|
- [x] in turnLeft() compare to heading enum
|
||||||
|
- [x] state.heading in Rover.constructor to enum
|
||||||
|
- [x] Change heading Char in RoverState to Heading enum
|
||||||
|
- [x] Create Heading enum
|
||||||
|
|
||||||
# RPP
|
# RPP
|
||||||
- [ ] Reorder Responsibilities
|
- [ ] Reorder Responsibilities
|
||||||
|
|
|
||||||
14
src/main/kotlin/org/example/Heading.kt
Normal file
14
src/main/kotlin/org/example/Heading.kt
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package org.example
|
||||||
|
|
||||||
|
enum class Heading(val symbol: Char) {
|
||||||
|
NORTH('N'),
|
||||||
|
EAST('E'),
|
||||||
|
SOUTH('S'),
|
||||||
|
WEST('W');
|
||||||
|
|
||||||
|
override fun toString(): String = symbol.toString()
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun from(symbol: Char): Heading? = entries.firstOrNull { it.symbol == symbol }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@ class Rover {
|
||||||
if (command.size >= ROVER_MINIMUM_NEEDED_COMMANDS) {
|
if (command.size >= ROVER_MINIMUM_NEEDED_COMMANDS) {
|
||||||
state.positionX = command[ROVER_STARTING_POSITION_X].toInt()
|
state.positionX = command[ROVER_STARTING_POSITION_X].toInt()
|
||||||
state.positionY = command[ROVER_STARTING_POSITION_Y].toInt()
|
state.positionY = command[ROVER_STARTING_POSITION_Y].toInt()
|
||||||
state.heading = command[ROVER_FACING_DIRECTION][ROVER_COMMANDLIST_DIRECTION]
|
state.heading = Heading.from(command[ROVER_FACING_DIRECTION][ROVER_COMMANDLIST_DIRECTION]) ?: Heading.NORTH
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -22,28 +22,28 @@ class Rover {
|
||||||
|
|
||||||
private fun move() {
|
private fun move() {
|
||||||
when (state.heading) {
|
when (state.heading) {
|
||||||
HEADING_EAST -> state.positionX++
|
Heading.EAST -> state.positionX++
|
||||||
HEADING_SOUTH -> state.positionY--
|
Heading.SOUTH -> state.positionY--
|
||||||
HEADING_WEST -> state.positionX--
|
Heading.WEST -> state.positionX--
|
||||||
HEADING_NORTH -> state.positionY++
|
Heading.NORTH -> state.positionY++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun turnRight() {
|
private fun turnRight() {
|
||||||
when (state.heading) {
|
when (state.heading) {
|
||||||
HEADING_EAST -> state.heading = HEADING_SOUTH
|
Heading.EAST -> state.heading = Heading.SOUTH
|
||||||
HEADING_SOUTH -> state.heading = HEADING_WEST
|
Heading.SOUTH -> state.heading = Heading.WEST
|
||||||
HEADING_WEST -> state.heading = HEADING_NORTH
|
Heading.WEST -> state.heading = Heading.NORTH
|
||||||
HEADING_NORTH -> state.heading = HEADING_EAST
|
Heading.NORTH -> state.heading = Heading.EAST
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun turnLeft() {
|
private fun turnLeft() {
|
||||||
when (state.heading) {
|
when (state.heading) {
|
||||||
HEADING_EAST -> state.heading = HEADING_NORTH
|
Heading.EAST -> state.heading = Heading.NORTH
|
||||||
HEADING_NORTH -> state.heading = HEADING_WEST
|
Heading.NORTH -> state.heading = Heading.WEST
|
||||||
HEADING_WEST -> state.heading = HEADING_SOUTH
|
Heading.WEST -> state.heading = Heading.SOUTH
|
||||||
HEADING_SOUTH -> state.heading = HEADING_EAST
|
Heading.SOUTH -> state.heading = Heading.EAST
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,8 +64,3 @@ private const val ROVER_COMMANDLIST_DIRECTION = 0
|
||||||
private const val COMMAND_TURN_LEFT = 'L'
|
private const val COMMAND_TURN_LEFT = 'L'
|
||||||
private const val COMMAND_TURN_RIGHT = 'R'
|
private const val COMMAND_TURN_RIGHT = 'R'
|
||||||
private const val COMMAND_MOVE = 'M'
|
private const val COMMAND_MOVE = 'M'
|
||||||
|
|
||||||
internal const val HEADING_EAST = 'E'
|
|
||||||
internal const val HEADING_NORTH = 'N'
|
|
||||||
internal const val HEADING_WEST = 'W'
|
|
||||||
internal const val HEADING_SOUTH = 'S'
|
|
||||||
|
|
|
||||||
|
|
@ -3,5 +3,5 @@ package org.example
|
||||||
class RoverState {
|
class RoverState {
|
||||||
var positionX: Int = 0
|
var positionX: Int = 0
|
||||||
var positionY: Int = 0
|
var positionY: Int = 0
|
||||||
var heading: Char = HEADING_NORTH
|
var heading: Heading = Heading.NORTH
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue