From b42bacd4209a01515dc779dee658c69b32f45213 Mon Sep 17 00:00:00 2001 From: eunwoo song Date: Mon, 6 Nov 2023 01:54:21 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9A=B0=EC=8A=B9=EC=9E=90=EC=9D=98=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EC=9D=84=20=EC=B6=9C=EB=A0=A5=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- src/main/kotlin/racingcar/Cars.kt | 9 +++++++++ src/main/kotlin/racingcar/Main.kt | 17 ++++++++++++++--- src/main/kotlin/racingcar/OutputView.kt | 5 +++++ src/test/kotlin/racingcar/CarsTest.kt | 6 ++++++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 49a3646e03..965c56a1ff 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,5 @@ ## 기능목록 -- [ ] 자동차 이름 입력받는다 -- [ ] 우승자를 선정한다 +- [x] 자동차 이름 입력받는다 +- [x] 우승자를 선정한다 diff --git a/src/main/kotlin/racingcar/Cars.kt b/src/main/kotlin/racingcar/Cars.kt index adec065d09..1f872513c6 100644 --- a/src/main/kotlin/racingcar/Cars.kt +++ b/src/main/kotlin/racingcar/Cars.kt @@ -1,12 +1,21 @@ package racingcar class Cars(val cars: List, private val powerGenerator: PowerGenerator) { + init { + require(cars.isNotEmpty()) { "자동차는 최소 한 대 이상 존재해야 합니다." } + } + fun move(): Cars { val movedCars = mutableListOf() cars.forEach { movedCars.add(it.move(powerGenerator.generate())) } return Cars(movedCars, powerGenerator) } + fun findWinner(): List { + val maxPosition = cars.maxBy { it.position }.position + return cars.filter { it.position == maxPosition } + } + companion object { fun initialize(numberOfCars: Int, names: List, powerGenerator: PowerGenerator): Cars { val cars = mutableListOf() diff --git a/src/main/kotlin/racingcar/Main.kt b/src/main/kotlin/racingcar/Main.kt index b21814ff1b..b06b4089b5 100644 --- a/src/main/kotlin/racingcar/Main.kt +++ b/src/main/kotlin/racingcar/Main.kt @@ -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() @@ -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) } diff --git a/src/main/kotlin/racingcar/OutputView.kt b/src/main/kotlin/racingcar/OutputView.kt index 9ff5c624aa..2d1e9ee0df 100644 --- a/src/main/kotlin/racingcar/OutputView.kt +++ b/src/main/kotlin/racingcar/OutputView.kt @@ -12,4 +12,9 @@ object OutputView { } println() } + + fun printWinners(winners: List) { + val winnerNames = winners.joinToString(", ") { it.name } + println("$winnerNames 가 최종 우승했습니다.") + } } diff --git a/src/test/kotlin/racingcar/CarsTest.kt b/src/test/kotlin/racingcar/CarsTest.kt index a4d1a319df..2f961c4c03 100644 --- a/src/test/kotlin/racingcar/CarsTest.kt +++ b/src/test/kotlin/racingcar/CarsTest.kt @@ -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 { Cars.initialize(0, listOf("a", "b", "c"), FixedPowerGenerator(5)) } + } + @Test fun `자동차들은 특정 숫자 이상이 들어오면 움직인다`() { val cars = Cars.initialize(3, listOf("a", "b", "c"), FixedPowerGenerator(4))