diff --git a/src/main/kotlin/racingcar/Application.kt b/src/main/kotlin/racingcar/Application.kt index df4bb99c..258d295d 100644 --- a/src/main/kotlin/racingcar/Application.kt +++ b/src/main/kotlin/racingcar/Application.kt @@ -1,7 +1,6 @@ package racingcar import camp.nextstep.edu.missionutils.Console -import camp.nextstep.edu.missionutils.Randoms fun main() { val cars = inputCars() diff --git a/src/main/kotlin/racingcar/Car.kt b/src/main/kotlin/racingcar/Car.kt new file mode 100644 index 00000000..c95940b7 --- /dev/null +++ b/src/main/kotlin/racingcar/Car.kt @@ -0,0 +1,16 @@ +package racingcar + +import camp.nextstep.edu.missionutils.Randoms + +class Car(val name: String) { + var position: Int = 0 + private set + + fun move() { + if (Randoms.pickNumberInRange(0, 9) >= 4) { + position++ + } + } + + fun getPositionMarker(): String = "-".repeat(position) +} diff --git a/src/main/kotlin/racingcar/RacingGame.kt b/src/main/kotlin/racingcar/RacingGame.kt new file mode 100644 index 00000000..45bcf97a --- /dev/null +++ b/src/main/kotlin/racingcar/RacingGame.kt @@ -0,0 +1,24 @@ +package racingcar + +class RacingGame(private val cars: List, private val tryCount: Int) { + + fun start() { + repeat(tryCount) { + cars.forEach { it.move() } + printCurrentPositions() + } + } + + private fun printCurrentPositions() { + cars.forEach { car -> + println("${car.name} : ${car.getPositionMarker()}") + } + println() + } + + fun getWinners(): String { + val maxPosition = cars.maxOf { it.position } + return cars.filter { it.position == maxPosition } + .joinToString(", ") { it.name } + } +}