From c8cea8e0d1820748a76fb433dec827a3efa18c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=ED=95=B8=EB=AA=A8?= Date: Thu, 15 Feb 2024 19:57:26 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/controller/BaseballController.kt | 5 +---- .../src/main/kotlin/baseball/model/Numbers.kt | 14 ++++++-------- .../src/main/kotlin/baseball/view/View.kt | 3 ++- 3 files changed, 9 insertions(+), 13 deletions(-) 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())