Skip to content

Commit

Permalink
feat: add feature set
Browse files Browse the repository at this point in the history
  • Loading branch information
julia-sj-kr committed Oct 28, 2024
1 parent a0d9919 commit c25c491
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/main/kotlin/racingcar/Application.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package racingcar

import camp.nextstep.edu.missionutils.Console
import camp.nextstep.edu.missionutils.Randoms

fun main() {
val cars = inputCars()
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/racingcar/Car.kt
Original file line number Diff line number Diff line change
@@ -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)
}
24 changes: 24 additions & 0 deletions src/main/kotlin/racingcar/RacingGame.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package racingcar

class RacingGame(private val cars: List<Car>, 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 }
}
}

0 comments on commit c25c491

Please sign in to comment.