Skip to content

Commit

Permalink
refactor: 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
gusah009 committed Feb 15, 2024
1 parent 03abc49 commit c8cea8e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 6 additions & 8 deletions kotlin-baseball/src/main/kotlin/baseball/model/Numbers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Number> = input.map { Number(it.digitToInt()) }

data class Numbers(val numbers: List<Number>) {
companion object {
fun generateRandom() =
Numbers(
mutableListOf<Int>().apply {
mutableListOf<Number>().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()
)
}

Expand All @@ -30,15 +28,15 @@ 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)
}

fun getStrike(other: Numbers) =
(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) }

Expand Down
3 changes: 2 additions & 1 deletion kotlin-baseball/src/main/kotlin/baseball/view/View.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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())
Expand Down

0 comments on commit c8cea8e

Please sign in to comment.