Skip to content

Commit

Permalink
Feat: 게임 종료 후 우승자 출력 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
mediumorange committed Nov 1, 2023
1 parent fe4874c commit 1d421b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/kotlin/racingcar/controller/GameController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GameController {
createRaceNumber()
println()
playRace()
gameResult()
}

fun createCarList() {
Expand All @@ -32,4 +33,8 @@ class GameController {
println(InformationMessage.EXECUTION_RESULT.message)
raceService.playRace()
}
fun gameResult() {
val winner = carService.getWinner()
println(InformationMessage.FINAL_WINNER.message + winner)
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/racingcar/global/InformationMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package racingcar.global
enum class InformationMessage(val message: String) {
INPUT_CARS_NAME("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"),
ATTEMPT_NUMBER("시도할 횟수는 몇 회인가요?"),
EXECUTION_RESULT("실행 결과")
EXECUTION_RESULT("실행 결과"),
FINAL_WINNER("최종 우승자 : ")
}
6 changes: 6 additions & 0 deletions src/main/kotlin/racingcar/service/CarService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ class CarService {
if(random >= 4) car.move()
}
}

fun getWinner(): String {
val largestNumber = Companion.cars.maxOf(Car::location)
return Companion.cars.filter { it.location == largestNumber }
.joinToString(separator = ", ") { car -> car.name }
}
}

0 comments on commit 1d421b9

Please sign in to comment.