Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step5 #960

Open
wants to merge 6 commits into
base: leehaeina
Choose a base branch
from
Open

Step5 #960

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ tasks {
}
ktlint {
verbose.set(true)
outputToConsole.set(true)
}
}
20 changes: 20 additions & 0 deletions src/main/kotlin/RacingGameController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package step3.racingcar.controller

import view.InputView
import view.RacingGameInput
import view.ResultView

class RacingGameController {
private val service: RacingGameService = RacingGameService()
private val resultView: ResultView = ResultView()

fun main() {
startGame(InputView.executeInputScreen())
}

private fun startGame(input: RacingGameInput) {
service.race(input).let {
resultView.outputScreen(it)
}
}
}
20 changes: 20 additions & 0 deletions src/main/kotlin/domain/Car.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package domain

class Car(
val name: String,
var distance: Int = 0
) {
fun move(number: Int): Int {
return if (number > 5) drive()
else stop()
}

private fun drive(): Int {
distance++
return distance
}

private fun stop(): Int {
return distance
}
}
11 changes: 11 additions & 0 deletions src/main/kotlin/domain/Winner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package domain

class Winner(
private val cars: List<Car>
) {
fun getWinner(): String {
val firstRecord = cars.maxOf { it.distance }
val winners = cars.filter { firstRecord == it.distance }.map { it.name }
return winners.joinToString { "," }
}
}
51 changes: 51 additions & 0 deletions src/main/kotlin/service/RacingGameService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package step3.racingcar.controller

import domain.Car
import domain.Winner
import view.RacingGameInput

class RacingGameService {
private val randomNumberGenerator: RandomNumberGenerator = RandomNumberGenerator(1, 9)
fun race(input: RacingGameInput): Pair<String, String> {
val candidate = input.carNames.split(",").map {
Car(it)
}
var trackHistory = ""
for (i: Int in 0..input.tryCount) {
trackHistory += lap(candidate)
}
return trackHistory to Winner(candidate).getWinner()
}

private fun lap(candidate: List<Car>): String {
var trackHistory = ""
candidate.forEach {
trackHistory += drawTrack(
it.name,
it.move(randomNumberGenerator.getRandomNumber())
) + "\n"
}
return trackHistory
}

private fun drawTrack(name: String, count: Int): String {
var result = ""
result += "$name : "
for (i: Int in 0..count) {
result += "-"
}
result += "\n"
return result
}
}

interface NumberGenerator {
fun getRandomNumber(): Int
val RANGE_START: Int
val RANGE_END: Int
}

class RandomNumberGenerator(override val RANGE_START: Int, override val RANGE_END: Int) :
NumberGenerator {
override fun getRandomNumber() = (RANGE_START..RANGE_END).random()
}
17 changes: 17 additions & 0 deletions src/main/kotlin/view/InputView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package view

object InputView {
fun executeInputScreen(): RacingGameInput {
println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).")
val carNames: String = readln()
println("시도할 횟수는 몇 회인가요?")
val tryCount = readln().toInt()

return RacingGameInput(carNames, tryCount)
}
}

data class RacingGameInput(
val carNames: String,
val tryCount: Int
)
13 changes: 13 additions & 0 deletions src/main/kotlin/view/ResultView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package view

class ResultView {

fun outputScreen(result: Pair<String, String>) {
println(result.first)
printWinner(result.second)
}

private fun printWinner(winners: String) {
println("${winners}가 최종 우승했습니다.")
}
}