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

[숫자 야구 게임] 박희원 미션 제출합니다. #212

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

HeewonP825
Copy link

개요

  • 우테코 프리코스 1주차 미션인 숫자 야구 게임 구현입니다.

작업 결과

스크린샷 2023-10-25 오후 9 22 14
스크린샷 2023-10-25 오후 9 23 53
스크린샷 2023-10-25 오후 9 24 20

작업 사항

  • main()

    • 숫자 야구의 시작을 알림
    • playGame() 함수 호출
    • 게임 종료 여부 구현
  • playGame()

    • checkGuess() 함수를 호출하여 ComputerNumbers와 userGuess 비교 작업 수행
    • 3 스트라이크의 경우 게임 종료
  • generateComputerNumbers()

    • 제시된 Randoms API를 사용해 랜덤으로 컴퓨터의 정답 할당
  • getUserInput()

    • 유저의 수를 입력받음
    • 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException을 발생시킨 후 애플리케이션은 종료 처리
  • checkGuess()

    • UserGuess[I]와 ComputerNumbers[I]를 비교하여 스트라이크와 볼 판정

기타

  • camp.nextstep.edu.missionutils에서 제공하는 RandomsConsole API를 사용했습니다.
  • kotlin-baseball-6 저장소를 Fork & Clone해 작업했습니다.

Copy link

@stopkite stopkite left a comment

Choose a reason for hiding this comment

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

바키! 1주차 과제하시느라 고생많으셨습니다.
디스코드에 남겨주신 것처럼 다음에는 클래스로 짜는 도전을 해보시면 좋을 것 같아요 :) !!
코드보니까 바키는 충분히 가능하실 것 같아요 ㅎㅎ
남은 3주도 같이 파이팅해보아요!

Comment on lines +14 to +21
if (input == "2") {
break
} else if (input == "1") {
playGame()
}
else {
throw IllegalArgumentException("잘못된 입력입니다.")
}
Copy link

@stopkite stopkite Oct 25, 2023

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
} 

이런식으로요 :)

Comment on lines +61 to +63
if (input == null || input.length != 3 || !input.all { it in '1'..'9' } || input.toSet().size != 3) {
throw IllegalArgumentException("잘못된 입력입니다. 서로 다른 3자리 숫자를 입력하세요.")
}
Copy link

@stopkite stopkite Oct 25, 2023

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스트라이크") {

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}스트라이크"

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) {

Choose a reason for hiding this comment

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

3이 어떤 의미의 숫자인지 이해 할 수 있도록 별도의 상수로 만들어 사용하면 좋을 것 같습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants