Import the Kotlin project from Dojo assignment

This commit is contained in:
Paul Hameteman 2025-10-14 20:42:45 +02:00
commit 98c91a107a
11 changed files with 245 additions and 22 deletions

View file

@ -1,3 +0,0 @@
fun main() {
println("Hello, Kotlin CI/CD world!")
}

43
src/main/kotlin/Rover.kt Normal file
View 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'
}

View file

@ -1,10 +0,0 @@
import kotlin.test.Test
import kotlin.test.assertEquals
class MainTest {
@Test
fun testExample() {
val result = 2 + 2
assertEquals(4, result)
}
}

View 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())
}
}