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

[숫자 야구 게임] 짱구 미션 제출합니다. #4

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: 숫자 야구 1차 구현
02ggang9 committed Feb 13, 2024
commit 780083007835af1d2a11481879f3249d0c237588
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package KtInActionUtil
package KtInActionPractice

const val UNIT_LINE_SEPARATOR = "\n"

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package KtInActionUtil
package KtInActionPractice

/*
* 코틀린의 주요 특성
26 changes: 25 additions & 1 deletion kotlin-baseball/src/main/kotlin/baseball/Application.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package baseball

import camp.nextstep.edu.missionutils.Console
import camp.nextstep.edu.missionutils.Randoms
import step1.*

fun main() {
print("ho")
announceBaseballGameStartMessage()
var randomNumbers = pickRandomNumbers()

while (true) {
announceEnterNumbersMessage()
val readLine = Console.readLine()
if (!isUserInputValid(readLine)) {
throw IllegalArgumentException()
}

announceBaseballResult(readLine, randomNumbers)
if (readLine.isThreeStrike(readLine, randomNumbers)) {
announceThreeStrike()
announceBaseballRestartMessage()
if (Console.readLine() == "2") break
randomNumbers = pickRandomNumbers()
}
}
}

fun String.isThreeStrike(numbers: String, randomNumbers: String): Boolean =
numbers[0] == randomNumbers[0] && numbers[1] == randomNumbers[1] && numbers[2] == randomNumbers[2]
52 changes: 52 additions & 0 deletions kotlin-baseball/src/main/kotlin/step1/BaseBallGame.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package step1

import camp.nextstep.edu.missionutils.Randoms

fun announceBaseballGameStartMessage() = println("숫자 야구 게임을 시작합니다.")

fun announceEnterNumbersMessage() = print("숫자를 입력해주세요 : ")

fun announceThreeStrike() = println("3개의 숫자를 모두 맞히셨습니다! 게임 종료")

fun announceBaseballRestartMessage() = println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.")

fun announceBaseballResult(userInputNumbers: String, randomNumbers: String) = println(calculateBaseballGameResult(userInputNumbers, randomNumbers))
Comment on lines +5 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

말씀대로 Util 역할을 하는 코틀린 파일에서 최상위 함수로 쓰면 더 좋을 것 같습니다. 지금은 BaseballGame 클래스에 너무 많은 역할이 들어있는 느낌이에용


fun pickRandomNumbers(): String {
val randomNumbers = mutableListOf<Int>()
while (randomNumbers.size < 3) {
val randomNumber = Randoms.pickNumberInRange(1, 9)
if (!randomNumbers.contains(randomNumber)) {
randomNumbers.add(randomNumber)
}
}
return randomNumbers.joinToString(separator = "")
}

fun isUserInputValid(userInputNumbers: String): Boolean = userInputNumbers.length == 3 && userInputNumbers.toIntOrNull() != null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용자가 0을 입력한 경우도 Invalid하다고 판단해줘야 하지 않을까요?



private fun calculateBaseballGameResult(userInputNumbers: String, randomNumbers: String): String {
val result = StringBuilder()
var strikeCount = 0
var ballCount = 0

for (i in randomNumbers.indices) {
if (userInputNumbers[i] == randomNumbers[i]) {
strikeCount++
} else if (userInputNumbers[i] in randomNumbers) {
ballCount++
}
}

if (ballCount > 0) {
result.append("${ballCount}볼 ")
}

if (strikeCount > 0) {
result.append("${strikeCount}스트라이크")
}

return if (ballCount == 0 && strikeCount == 0) "낫싱" else result.toString()

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package KtInActionPractice

import KtInActionUtil.*
import org.junit.jupiter.api.Test