diff --git a/kotlin-baseball/src/main/kotlin/baseball/controller/BaseballController.kt b/kotlin-baseball/src/main/kotlin/baseball/controller/BaseballController.kt index a938d4e..c7c68e0 100644 --- a/kotlin-baseball/src/main/kotlin/baseball/controller/BaseballController.kt +++ b/kotlin-baseball/src/main/kotlin/baseball/controller/BaseballController.kt @@ -22,8 +22,5 @@ class BaseballController( } while (view.getUserContinueGame() == UserContinueGame.RESTART) } - private fun StrikeBall.isGameContinue(): Boolean { - println("${this.strike} ${this.ball}") - return this.strike != NUMBER_LENGTH - } + private fun StrikeBall.isGameContinue() = this.strike != NUMBER_LENGTH } diff --git a/kotlin-baseball/src/main/kotlin/baseball/model/Numbers.kt b/kotlin-baseball/src/main/kotlin/baseball/model/Numbers.kt index a814b76..0723b6c 100644 --- a/kotlin-baseball/src/main/kotlin/baseball/model/Numbers.kt +++ b/kotlin-baseball/src/main/kotlin/baseball/model/Numbers.kt @@ -6,20 +6,18 @@ const val MIN_NUMBER_INCLUDE = 1 const val MAX_NUMBER_INCLUDE = 9 const val NUMBER_LENGTH = 3 -class Numbers(input: String) { - private val numbers: List = input.map { Number(it.digitToInt()) } - +data class Numbers(val numbers: List) { companion object { fun generateRandom() = Numbers( - mutableListOf().apply { + mutableListOf().apply { while (size != NUMBER_LENGTH) { - val pickNumber = Randoms.pickNumberInRange(MIN_NUMBER_INCLUDE, MAX_NUMBER_INCLUDE) + val pickNumber = Number(Randoms.pickNumberInRange(MIN_NUMBER_INCLUDE, MAX_NUMBER_INCLUDE)) if (pickNumber !in this) { this.add(pickNumber) } } - }.joinToString("") + }.toList() ) } @@ -30,7 +28,7 @@ class Numbers(input: String) { fun compare(other: Numbers): StrikeBall { val strike = getStrike(other) - val ball = getBall(other, strike) + val ball = getBall(other) return StrikeBall(strike, ball) } @@ -38,7 +36,7 @@ class Numbers(input: String) { (0 until NUMBER_LENGTH) .count { this.numbers[it] == other.numbers[it] } - fun getBall(other: Numbers, strike: Int) = + fun getBall(other: Numbers) = (0 until NUMBER_LENGTH) .count { i -> isBall(i, other) } diff --git a/kotlin-baseball/src/main/kotlin/baseball/view/View.kt b/kotlin-baseball/src/main/kotlin/baseball/view/View.kt index 8a14290..1915437 100644 --- a/kotlin-baseball/src/main/kotlin/baseball/view/View.kt +++ b/kotlin-baseball/src/main/kotlin/baseball/view/View.kt @@ -1,6 +1,7 @@ package baseball.view import baseball.model.NUMBER_LENGTH +import baseball.model.Number import baseball.model.Numbers import baseball.model.StrikeBall import baseball.model.UserContinueGame @@ -13,7 +14,7 @@ class View { fun getUserNumbers(): Numbers { print("숫자를 입력해주세요 : ") - return Numbers(Console.readLine()) + return Numbers(Console.readLine().map { Number(it.digitToInt()) }) } fun getUserContinueGame() = UserContinueGame.of(Console.readLine())