-
Notifications
You must be signed in to change notification settings - Fork 422
[Step5]자동차 경주(리팩터링) #936
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
base: choi-ys
Are you sure you want to change the base?
[Step5]자동차 경주(리팩터링) #936
Changes from all commits
a59f19b
b6ac70f
7a4fee4
9f3930b
9c82457
0effa6b
c6fd39d
10710ad
753cac7
def429b
ebca741
107cc52
352368f
754af09
b024f5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,20 @@ import step3.racingcar.domain.Cars | |
import step3.racingcar.domain.PlayInfo | ||
import step3.racingcar.service.RacingCarService | ||
import step3.racingcar.utils.CarGenerator | ||
import step3.racingcar.utils.RandomNumberGenerator.generateRandomNumberToCarByRound | ||
import step3.racingcar.utils.RandomNumberGenerator | ||
import step3.racingcar.view.InputView.Companion.inputJoinerCarsGuideMessagePrinter | ||
import step3.racingcar.view.InputView.Companion.inputRoundCountGuideMessagePrinter | ||
import step3.racingcar.view.ResultView | ||
|
||
class RacingCarController { | ||
private val racingCarService: RacingCarService = RacingCarService() | ||
private val racingCarService: RacingCarService = RacingCarService(RandomNumberGenerator()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Service 하위의 모든 객체에서 이제 생성되는 숫자에 대해 개발자가 제어할 수 있겠네요 :) 👍👍 |
||
|
||
fun gameStart() { | ||
val carNames = inputJoinerCarsGuideMessagePrinter() | ||
val totalRound = inputRoundCountGuideMessagePrinter() | ||
val cars = Cars.of(CarGenerator.generate(carNames)) | ||
generateRandomNumberToCarByRound(cars, totalRound) | ||
val playInfo = PlayInfo(cars, totalRound) | ||
racingCarService.play(playInfo) | ||
val playResult = racingCarService.play(playInfo) | ||
ResultView.printResult(playResult) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,16 @@ | ||
package step3.racingcar.domain | ||
|
||
private const val CAR_ID_DELIMITER = "-" | ||
private const val MOVE_CRITERIA = 4 | ||
private const val INITIAL_DISTANCE = 0 | ||
|
||
class Car(val name: String) { | ||
private var randomNumbers: RandomNumbers = RandomNumbers() | ||
var distance = 0 | ||
|
||
fun addRandomNumber(randomNumber: Int) = randomNumbers.add(randomNumber) | ||
|
||
fun race(currentRoundIndex: Int) { | ||
val randomNumberByCurrentRound: Int = randomNumbers[currentRoundIndex] | ||
if (isMove(randomNumberByCurrentRound)) { | ||
class Car(val name: String, var distance: Int = INITIAL_DISTANCE) { | ||
Comment on lines
5
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여전히 외부에서 distance 값을 변형시킬 수 있습니다 😥 아래 처럼 작성해볼 수 있어요! 참고 정도로만 봐주시면 좋겠습니다 🙂 class Car(
val name: String,
initialDistance: Int = 0,
) {
var distance: Int = initialDistance
private set
...
} |
||
fun race(randomNumber: Int) { | ||
if (isMove(randomNumber)) { | ||
distance++ | ||
} | ||
} | ||
|
||
fun isMaximumDistance(maxDistance: Int): Boolean = distance == maxDistance | ||
Comment on lines
12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 함수명은 내용과 사뭇 달라 보입니다. |
||
private fun isMove(randomNumber: Int): Boolean = randomNumber >= MOVE_CRITERIA | ||
fun copy(): Car = Car(name, distance) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
package step3.racingcar.domain | ||
|
||
class Cars private constructor(private val elements: List<Car>) { | ||
fun elements(): List<Car> = elements | ||
fun size(): Int = elements.size | ||
operator fun get(index: Int) = elements[index] | ||
|
||
operator fun get(index: Int): Car = elements[index] | ||
|
||
fun race(numberGenerator: NumberGenerator): RoundResult { | ||
val roundResult = RoundResult() | ||
elements.forEach { | ||
it.race(numberGenerator.value()) | ||
roundResult.add(it) | ||
} | ||
return roundResult | ||
} | ||
|
||
fun winnerNames(): List<String> { | ||
val maxDistance = findMaxDistance() | ||
return elements | ||
.filter { it.isMaximumDistance(maxDistance) } | ||
.map { it.name } | ||
} | ||
|
||
private fun findMaxDistance(): Int = elements.maxOf { it.distance } | ||
|
||
companion object { | ||
fun of(elements: List<Car>) = Cars(elements) | ||
fun of(elements: List<Car>): Cars = Cars(elements) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package step3.racingcar.domain | ||
|
||
interface NumberGenerator { | ||
fun value(): Int | ||
|
||
Comment on lines
+3
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kotlin에서 1개 함수만 있는 인터페이스를 람다로 사용하고 싶을때는, |
||
companion object { | ||
const val RANGE_START = 1 | ||
const val RANGE_END = 9 | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package step3.racingcar.domain | ||
|
||
class RoundResult { | ||
private val elements: MutableList<Car> = mutableListOf() | ||
|
||
operator fun get(index: Int): Car = elements[index] | ||
fun add(car: Car) = elements.add(car.copy()) | ||
fun size(): Int = elements.size | ||
} | ||
Comment on lines
+3
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 각 라운드 별 결과에서는 직접 Car를 가지지 않고, 라운드에 대한 정보만 가져보는 것은 어떨까요? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package step3.racingcar.domain | ||
|
||
class RoundResults private constructor(val totalRound: Int, val cars: Cars) { | ||
private val elements: MutableList<RoundResult> = mutableListOf() | ||
|
||
operator fun get(index: Int) = elements[index] | ||
fun add(roundResult: RoundResult) = elements.add(roundResult) | ||
|
||
companion object { | ||
fun of(playInfo: PlayInfo): RoundResults = RoundResults(playInfo.totalRound, playInfo.cars) | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
package step3.racingcar.service | ||
|
||
import step3.racingcar.domain.Car | ||
import step3.racingcar.domain.Cars | ||
import step3.racingcar.domain.NumberGenerator | ||
import step3.racingcar.domain.PlayInfo | ||
import step3.racingcar.domain.Winners | ||
import step3.racingcar.view.ResultView.Companion.printRoundResult | ||
import step3.racingcar.view.ResultView.Companion.printWinner | ||
import step3.racingcar.domain.RoundResult | ||
import step3.racingcar.domain.RoundResults | ||
|
||
class RacingCarService { | ||
fun play(playInfo: PlayInfo) { | ||
class RacingCarService(private val numberGenerator: NumberGenerator) { | ||
fun play(playInfo: PlayInfo): RoundResults { | ||
val roundResults = RoundResults.of(playInfo) | ||
repeat(playInfo.totalRound) { | ||
playEachRound(it, playInfo.cars) | ||
val playEachRoundAndReturn = playEachRound(playInfo.cars) | ||
roundResults.add(playEachRoundAndReturn) | ||
} | ||
printWinner(Winners.of(playInfo.cars)) | ||
return roundResults | ||
} | ||
|
||
private fun playEachRound(currentRoundIndex: Int, cars: Cars) { | ||
cars.elements().forEach { | ||
it.race(currentRoundIndex) | ||
} | ||
printRoundResult(currentRoundIndex, cars) | ||
fun playEachRound(cars: Cars): RoundResult = | ||
cars.race(numberGenerator) | ||
|
||
fun playEachRoundByCar(car: Car) { | ||
val randomNumber = numberGenerator.value() | ||
car.race(randomNumber) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,11 @@ | ||
package step3.racingcar.utils | ||
|
||
import step3.racingcar.domain.Car | ||
import step3.racingcar.domain.Cars | ||
import step3.racingcar.domain.NumberGenerator | ||
import step3.racingcar.domain.NumberGenerator.Companion.RANGE_END | ||
import step3.racingcar.domain.NumberGenerator.Companion.RANGE_START | ||
|
||
object RandomNumberGenerator { | ||
private const val RANGE_START = 1 | ||
private const val RANGE_END = 9 | ||
|
||
fun generateRandomNumberToCarByRound(cars: Cars, totalRound: Int) { | ||
cars.elements().forEach { | ||
generateRandomNumberToEachCar(it, totalRound) | ||
} | ||
} | ||
class RandomNumberGenerator : NumberGenerator { | ||
override fun value(): Int = generate() | ||
|
||
private fun generate(): Int = (RANGE_START..RANGE_END).random() | ||
|
||
private fun generateRandomNumberToEachCar(car: Car, totalRound: Int) { | ||
repeat(totalRound) { | ||
car.addRandomNumber(generate()) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
피드백 내용들을 정말 열심히 곱씹으시며 많은 고민을 하신 것들이 잘 느껴지는 대목이네요 😊👍👍