Import the Kotlin project from Dojo assignment
This commit is contained in:
parent
2276f60b2f
commit
98c91a107a
11 changed files with 245 additions and 22 deletions
|
|
@ -1,3 +0,0 @@
|
|||
fun main() {
|
||||
println("Hello, Kotlin CI/CD world!")
|
||||
}
|
||||
43
src/main/kotlin/Rover.kt
Normal file
43
src/main/kotlin/Rover.kt
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package org.example
|
||||
|
||||
class Rover {
|
||||
constructor(p: String) {
|
||||
val s = p.split(' ')
|
||||
if (s.size >= 3) {
|
||||
rs.xx = s[0].toInt()
|
||||
rs.yy = s[1].toInt()
|
||||
rs.dd = s[2][0]
|
||||
}
|
||||
}
|
||||
|
||||
fun go(cms: String) {
|
||||
for (c in cms) {
|
||||
when (c) {
|
||||
'L' -> { when (rs.dd) { 'E' -> rs.dd = 'N' 'N' -> rs.dd = 'W' 'W' -> rs.dd = 'S' 'S' -> rs.dd = 'E'}}
|
||||
'R' -> { when (rs.dd) { 'E' -> rs.dd = 'S' 'S' -> rs.dd = 'W' 'W' -> rs.dd = 'N' 'N' -> rs.dd = 'E'}}
|
||||
'M' -> { when (rs.dd) { 'E' -> rs.xx++ 'S' -> rs.yy-- 'W' -> rs.xx-- 'N' -> rs.yy++}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun g(z: Char) {
|
||||
go(z.toString())
|
||||
}
|
||||
|
||||
val xyd: String
|
||||
get() = "${rs.xx} ${rs.yy} ${rs.dd}"
|
||||
|
||||
fun pos(): String {
|
||||
return xyd
|
||||
}
|
||||
|
||||
constructor() : this("")
|
||||
|
||||
private var rs = RoverState()
|
||||
}
|
||||
|
||||
class RoverState {
|
||||
var xx: Int = 0
|
||||
var yy: Int = 0
|
||||
var dd: Char = 'N'
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class MainTest {
|
||||
@Test
|
||||
fun testExample() {
|
||||
val result = 2 + 2
|
||||
assertEquals(4, result)
|
||||
}
|
||||
}
|
||||
30
src/test/kotlin/MarsRoverTests.kt
Normal file
30
src/test/kotlin/MarsRoverTests.kt
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import org.example.Rover
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.CsvSource
|
||||
|
||||
class MarsRoverTests {
|
||||
@ParameterizedTest
|
||||
@CsvSource(
|
||||
"1 2 N, ' ', 1 2 N",
|
||||
"1 2 N, L, 1 2 W",
|
||||
"1 2 W, L, 1 2 S",
|
||||
"1 2 S, L, 1 2 E",
|
||||
"1 2 E, L, 1 2 N",
|
||||
"1 2 N, R, 1 2 E",
|
||||
"1 2 E, R, 1 2 S",
|
||||
"1 2 S, R, 1 2 W",
|
||||
"1 2 W, R, 1 2 N",
|
||||
"1 2 N, M, 1 3 N",
|
||||
"1 2 E, M, 2 2 E",
|
||||
"1 2 S, M, 1 1 S",
|
||||
"1 2 W, M, 0 2 W",
|
||||
"1 2 N, LMLMLMLMM, 1 3 N",
|
||||
"3 3 E, MMRMMRMRRM, 5 1 E"
|
||||
)
|
||||
fun `execute commands`(startingPosition: String, instructions: String, expectedOutput: String) {
|
||||
val rover = Rover(startingPosition)
|
||||
rover.go(instructions)
|
||||
assertEquals(expectedOutput, rover.pos())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue