Skip to content

김주희) 야구게임 과제 전체 문제 풀이#6

Open
coduhee wants to merge 23 commits into
mainfrom
juhee
Open

김주희) 야구게임 과제 전체 문제 풀이#6
coduhee wants to merge 23 commits into
mainfrom
juhee

Conversation

@coduhee
Copy link
Copy Markdown
Collaborator

@coduhee coduhee commented Jan 14, 2026

리팩토링할게 한참 남아보이긴하지만 일단 올려봅니다..
리뷰 남겨주시면 정말 감사드리겠습니다.

Comment on lines +20 to +31
switch readLine() {
case "1":
playGame()
case "2":
print("<게임 기록 보기>")
recordManager.showRecords() // showRecords 함수 호출
case "3":
print("< 숫자 야구 게임을 종료합니다. >")
exit(0) // 강제 종료 함수 출력
default:
print("올바른 숫자를 입력해주세요!")
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

readLine()은 옵셔널 값으로 알고있는데, 바인딩 안해주어도 switch문 잘 돌아가나용??

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

switch문에 readLine()을 넣으면 case에 해당하지않는 nil값은 바로 default문이 실행되어서 옵셔널 바인딩 + 값 비교를 동시에 해주는거라 더 깔끔하게 작성할 수 있습니당!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

아...!! 옵셔널 타입이라서 case Optional("1")처럼 되어야하지 않나...라고 헷갈렸었습니다ㅠㅠㅠ
맞네요 비교는 바로 되겠네요ㅋㅋㅋ default로 한번에 처리할 수 있다니 좋은 활용법인 것 같습니다! 설명 감사드려요!!☺️

Comment thread juhee/juhee/GameCenter.swift Outdated
Comment on lines +17 to +19
if arr[0] != arr[1]
&& arr[1] != arr[2]
&& arr[0] != arr[2]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

중복 체크 부분은 Set.count를 활용해봐도 좋을 것 같아요! arr를 Set 타입으로 바꾸면 중복 요소는 제거되니, 한번 참고해보시면 좋을 것 같습니당ㅎㅎ

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

왜 배열만 생각했을까요..ㅠㅠ Set의 존재를 까맣게 잊어버렸어요 감사합니다 예린님🤩 참고해서 더 간결하게 만들어보겠습니당!

Comment thread juhee/juhee/GameCenter.swift Outdated
Comment on lines +63 to +73
var b = Int.random(in: 0...9)
while a == b {
b = Int.random(in: 0...9) // a와 b가 다를때까지 b에 랜덤한 Int값 대입
}

var c = Int.random(in: 0...9)
while a == c || b == c {
c = Int.random(in: 0...9) // c가 a, b값과 다를때까지 c에 랜덤한 Int값 대입
}

let answer: Array = [a, b, c]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Array 타입에는 contains()라는 메서드가 있어요!
배열 안에 괄호 안의 요소가 포함되어있는지를 판단하여 Bool값을 리턴해주는 메서드예요.

요 메서드를 사용하면 코드의 반복성을 줄일 수 있지 않을까 생각됩니다.
한번 활용해보시는 것도 좋을 것 같아용!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

오앙 근데 위에서 써주셨던 Set.counts 이용해서 정답 생성하는걸로 리팩토링하는게 더 괜찮을거같아서 그쪽 방향으로 수정해봤습니당! 항상 많이 배워가는것같아 너무 감사드려용 예린님😆🫶

Comment thread juhee/juhee/BaseballGame.swift Outdated
Comment on lines +43 to +45
guard let input = readLine(),
let inputNumber = Int(input), // 숫자로 변환
gameCenter.checkInput(inputNumber) // 입력값 검사 함수 호출
Copy link
Copy Markdown

@Jaesung-Jung Jaesung-Jung Jan 15, 2026

Choose a reason for hiding this comment

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

guard let inputNumber = readLine().flatMap({ Int($0) }, gameCenter.checkInput(inputNumber) else {
  ...
}

Optional의 map으로 값이 nil이 아닌 케이스에서 변환 연산을 적용하는 방법

flatMap({ Int($0) })flatMap(Int.init) 으로 바꾸어도 됩니다.
지난 주차 과제에서 init을 map에 전달해서 쓰신 것 봤던 기억이..

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

헉 flatMap이 옵셔널을 제거하는 역할이 있는지 몰랐습니당.. 참고해서 수정하였습니다. 코멘트 정말 감사드립니다!! 🤩

GameCounter class 삭제 및 recordManager class에 addTrial메서드 생성 및 기타 코드 리팩토링
Updated the README to provide detailed project information, including purpose, rules, structure, class responsibilities, and refactoring points.
정답 생성 함수 배열의 contains를 이용한 방식으로 리팩토링
주석 추가
Array<Int> = [Int] 동의어
if return문 삭제해도 똑같은 의미
배열의 shuffle 이용해 풀이
게임에 쓰이는 입력수의 자리수를 gameNumber로 별도로 선언함
wholeNumberValue 이용하여 함수 간결화, 함수 네이밍 수정
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