-
Notifications
You must be signed in to change notification settings - Fork 224
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
[숫자 야구 게임] 박희원 미션 제출합니다. #212
base: main
Are you sure you want to change the base?
Conversation
…erNumbers() & getUserInput()
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.
바키! 1주차 과제하시느라 고생많으셨습니다.
디스코드에 남겨주신 것처럼 다음에는 클래스로 짜는 도전을 해보시면 좋을 것 같아요 :) !!
코드보니까 바키는 충분히 가능하실 것 같아요 ㅎㅎ
남은 3주도 같이 파이팅해보아요!
if (input == "2") { | ||
break | ||
} else if (input == "1") { | ||
playGame() | ||
} | ||
else { | ||
throw IllegalArgumentException("잘못된 입력입니다.") | ||
} |
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.
이부분은 다음에 when 절로 표현해보는 것도 좋을 것 같아요!
when 절을 통해 else if문을 줄이고 더 간결하게 표현이 가능할 것 같아요!
또한, 추후에 만약 "3"이라는 조건이 추가된다면 불필요한 else if를 또 써야하는 상황이 올 수도 있으니까요!
when(input) {
1 -> playGame()
2 -> break
else -> throw
}
이런식으로요 :)
if (input == null || input.length != 3 || !input.all { it in '1'..'9' } || input.toSet().size != 3) { | ||
throw IllegalArgumentException("잘못된 입력입니다. 서로 다른 3자리 숫자를 입력하세요.") | ||
} |
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.
이 조건들은 함수 또는 변수로 따로 빼볼 수도 있을 것 같아요!
변수로 한 번 예시를 들어보자면
val isEmptyOrNull = if(input == null)
val isNotThreeLength = if(input.length != 3)
val isInRange = if(!input.all {it in '1'..'9'}
로 해서
if(isEmptyOrNull || isNotThreeLength || isNotInRange) { ... }
이런식으로 가독성이 좋게 작성해볼수도 있을 것 같아요 :) !!
val result = checkGuess(userGuess, computerNumbers) | ||
println("결과: $result") | ||
|
||
if (result == "3스트라이크") { |
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.
게임 종료 조건을 result == "3스트라이크"으로 설정하면 해당 문구를 변경시 해당 코드를 수정해야 한다는 문제가 발생할 수 있습니다.
별도의 변수로 선언하시거나 출력 문구를 만드는 함수를 별도로 작성하시면 좋을 것 같습니다.
|
||
return when { | ||
strikes == 3 -> "3스트라이크" | ||
strikes > 0 || balls > 0 -> "${balls}볼 ${strikes}스트라이크" |
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.
strike가 0 이고 ball이 1인 경우 "1볼"이 아니라 "1볼 0스트라이크" 가 출력이 될 것 같은데
이렇게 작성해도 ball이나 strike가 0인 경우 1가지만 출력이 가능한가요?
|
||
fun generateComputerNumbers(): List<Int> { // 컴퓨터 랜덤 변수 설정 | ||
val numbers = mutableListOf<Int>() | ||
while (numbers.size < 3) { |
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.
3이 어떤 의미의 숫자인지 이해 할 수 있도록 별도의 상수로 만들어 사용하면 좋을 것 같습니다.
개요
작업 결과
작업 사항
main()
playGame()
generateComputerNumbers()
getUserInput()
IllegalArgumentException
을 발생시킨 후 애플리케이션은 종료 처리checkGuess()
기타
camp.nextstep.edu.missionutils
에서 제공하는Randoms
및Console
API를 사용했습니다.