Skip to content

Commit

Permalink
feat: 우승자의 이름을 출력하는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
be-student committed Nov 5, 2023
1 parent 3c8cc6a commit 31f5a03
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

## 기능목록

- [ ] 자동차 이름 입력받는다
- [ ] 우승자를 선정한다
- [x] 자동차 이름 입력받는다
- [x] 우승자를 선정한다
9 changes: 9 additions & 0 deletions src/main/kotlin/racingcar/Cars.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package racingcar

class Cars(val cars: List<Car>, private val powerGenerator: PowerGenerator) {
init {
require(cars.isNotEmpty()) { "자동차는 최소 한 대 이상 존재해야 합니다." }
}

fun move(): Cars {
val movedCars = mutableListOf<Car>()
cars.forEach { movedCars.add(it.move(powerGenerator.generate())) }
return Cars(movedCars, powerGenerator)
}

fun findWinner(): List<Car> {
val maxPosition = cars.maxBy { it.position }.position
return cars.filter { it.position == maxPosition }
}

companion object {
fun initialize(numberOfCars: Int, names: List<String>, powerGenerator: PowerGenerator): Cars {
val cars = mutableListOf<Car>()
Expand Down
17 changes: 14 additions & 3 deletions src/main/kotlin/racingcar/Main.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package racingcar

fun main() {
val numberOfCars = InputView.inputNumberOfCars()
val namesOfCars = InputView.inputNameOfCars()
val cars = Cars.initialize(numberOfCars, namesOfCars, RandomPowerGenerator)
val cars = initializeCars()

val movedCars = moveCars(cars)
val winners = movedCars.findWinner()
OutputView.printWinners(winners)
}

private fun moveCars(cars: Cars): Cars {
val numberOfMoves = InputView.inputNumberOfMoves()

OutputView.printResultTitle()
Expand All @@ -14,4 +18,11 @@ fun main() {
movedCars = movedCars.move()
OutputView.printResult(movedCars.cars)
}
return movedCars
}

private fun initializeCars(): Cars {
val numberOfCars = InputView.inputNumberOfCars()
val namesOfCars = InputView.inputNameOfCars()
return Cars.initialize(numberOfCars, namesOfCars, RandomPowerGenerator)
}
5 changes: 5 additions & 0 deletions src/main/kotlin/racingcar/OutputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ object OutputView {
}
println()
}

fun printWinners(winners: List<Car>) {
val winnerNames = winners.joinToString(", ") { it.name }
println("$winnerNames 가 최종 우승했습니다.")
}
}
6 changes: 6 additions & 0 deletions src/test/kotlin/racingcar/CarsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package racingcar

import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

internal class CarsTest {

@Test
fun `자동차들은 1대 이상 있어야 한다`() {
assertThrows<IllegalArgumentException> { Cars.initialize(0, listOf("a", "b", "c"), FixedPowerGenerator(5)) }
}

@Test
fun `자동차들은 특정 숫자 이상이 들어오면 움직인다`() {
val cars = Cars.initialize(3, listOf("a", "b", "c"), FixedPowerGenerator(4))
Expand Down

0 comments on commit 31f5a03

Please sign in to comment.