-
Notifications
You must be signed in to change notification settings - Fork 0
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
02ggang9
wants to merge
8
commits into
main
Choose a base branch
from
02ggang9/baseball
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+813
−1
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e27ca20
feat: 코틀린 인 액션 파트 1 정리
02ggang9 2041c49
feat: 코틀린 인 액션 파트 2 정리
02ggang9 bd1783e
feat: 코틀린 인 액션 파트 3 정리
02ggang9 43ba740
chore: 파일 디렉토리 변경
02ggang9 7800830
feat: 숫자 야구 1차 구현
02ggang9 255fefc
fix: 1차 구현 분리
02ggang9 0782aa8
feat: 2차 구현 완료
02ggang9 58435c4
refactor: 코틀린이 제공하는 기능을 사용해 예외처리
02ggang9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: 숫자 야구 1차 구현
commit 780083007835af1d2a11481879f3249d0c237588
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...src/main/kotlin/KtInActionUtil/JointKt.kt → ...main/kotlin/KtInActionPractice/JointKt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package KtInActionUtil | ||
package KtInActionPractice | ||
|
||
const val UNIT_LINE_SEPARATOR = "\n" | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
.../kotlin/KtInActionUtil/KtInActionPart1.kt → ...lin/KtInActionPractice/KtInActionPart1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package KtInActionUtil | ||
package KtInActionPractice | ||
|
||
/* | ||
* 코틀린의 주요 특성 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
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 | ||
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. 사용자가 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() | ||
|
||
} |
1 change: 0 additions & 1 deletion
1
kotlin-baseball/src/test/kotlin/KtInActionPractice/KtInActionPart3.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package KtInActionPractice | ||
|
||
import KtInActionUtil.* | ||
import org.junit.jupiter.api.Test | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
말씀대로 Util 역할을 하는 코틀린 파일에서 최상위 함수로 쓰면 더 좋을 것 같습니다. 지금은
BaseballGame
클래스에 너무 많은 역할이 들어있는 느낌이에용