-
Notifications
You must be signed in to change notification settings - Fork 2
손영빈// base: main( 필수 기능 구현) <- compare: dev(도전 기능 구현) #1
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
Open
ybin4548
wants to merge
3
commits into
yeongbin_main
Choose a base branch
from
yeongbin_dev
base: yeongbin_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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains hidden or 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,61 @@ | ||
| // | ||
| // play.swift | ||
| // baseballgame | ||
| // | ||
| // Created by 손영빈 on 1/13/26. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| // 게임 시작 함수 | ||
| func play() -> Int { | ||
|
|
||
| // let result = randomAnswer() | ||
| let result = randomAnswer2() | ||
| var totalCount = 0 | ||
| while true { | ||
| /* (Lv.2) 1. 정답 맞추기 + 힌트 받기 */ | ||
| print("숫자를 입력하세요: ", terminator: "\n") | ||
| guard let input = readLine(), | ||
| let num = Optional(input.compactMap{Int(String($0))}), //숫자 외의 다른 값이 들어왔는지 확인, map 대신 compactMap 사용 : map 사용시 nil로 처리 되기 때문에 처리가 바르게 일어나지않음(ex. 1ab입력시 num = [1,nil,nil] -> 중복으로 처리됨 | ||
| num.count == 3 | ||
| else { | ||
| print("올바르지 않은 입력값입니다.") | ||
| continue | ||
| } | ||
|
|
||
| if Set(num).count != 3 { //중복된 값이 있는지 확인 | ||
| print("올바르지 않은 입력값입니다.(중복)") | ||
| continue | ||
| } | ||
|
|
||
| totalCount += 1 //기록 저장용 Count + 1 | ||
|
|
||
| let inputChars = Array(input) //입력받은 값을 배열에 담음 | ||
|
|
||
| /* (Lv.2) 2. 힌트 생성, 게산 */ | ||
| var strike: Int = 0 | ||
| var ball: Int = 0 | ||
|
|
||
| for i in 0..<3 { | ||
| if result[i] == Int(String(inputChars[i]))! { //Int는 Character를 바로 받지 못함 -> String으로 감싸기 | ||
| strike += 1 | ||
| } else if result.contains(Int(String(inputChars[i]))!) { | ||
| ball += 1 | ||
| } | ||
| } | ||
| if strike == 3 { | ||
| print("정답입니다.") | ||
| return totalCount//정답일 경우, 게임 종료 -> (break를 사용하지 않고 return을 사용한 이유 -> 기록을 남기기 위해서) | ||
| }else if strike >= 1 && ball >= 1 { | ||
| print("\(strike)스트라이크 \(ball)볼") | ||
| }else if strike >= 1 { | ||
| print("\(strike)스트라이크") | ||
| }else if ball >= 1 { | ||
| print("\(ball)볼") | ||
| }else { | ||
| print("Nothing") | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or 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,57 +1 @@ | ||
| var answer: Set<Int> = [] | ||
| var result: [Int] = [] | ||
|
|
||
| /* (Lv.1) 1. 1~9 서로 다른 난수 3개 생성 */ | ||
| while answer.count < 3 { //정답 생성용(Set 사용) : 같은 수 반복 x | ||
| let num = Int.random(in: 1...9) | ||
| answer.insert(num) | ||
| } | ||
| result = Array(answer) // 정답 저장용 | ||
| print(answer) | ||
| print(result) | ||
|
|
||
|
|
||
|
|
||
| while true{ | ||
| /* (Lv.2) 1. 정답 맞추기 + 힌트 받기 */ | ||
| print("숫자를 입력하세요: ", terminator: "\n") | ||
| guard let input = readLine(), | ||
| let num = Optional(input.map{Int(String($0))}), //숫자 외의 다른 값이 들어왔는지 확인, map 대신 compactMap 사용 : map -> 이후 nil 처리 필요 | ||
| num.count == 3 | ||
| else { | ||
| print("올바르지 않은 입력값입니다.") | ||
| continue | ||
| } | ||
|
|
||
| if Set(num).count != 3 { //중복된 값이 있는지 확인 | ||
| print("올바르지 않은 입력값입니다.(중복)") | ||
| continue | ||
| } | ||
|
|
||
| let inputChars = Array(input) //입력받은 값을 배열에 담음 | ||
|
|
||
| /* (Lv.2) 2. 힌트 생성, 게산 */ | ||
| var strike: Int = 0 | ||
| var ball: Int = 0 | ||
|
|
||
| for i in 0..<3 { | ||
| if result[i] == Int(String(inputChars[i]))! { //Int는 Character를 바로 받지 못함 -> String으로 감싸기 | ||
| strike += 1 | ||
| } else if result.contains(Int(String(inputChars[i]))!) { | ||
| ball += 1 | ||
| } | ||
| } | ||
| if strike == 3{ | ||
| print("정답입니다.") | ||
| break //정답일 경우, 게임 종료 | ||
| }else if strike >= 1 && ball >= 1{ | ||
| print("\(strike)스트라이크 \(ball)볼") | ||
| }else if strike >= 1 { | ||
| print("\(strike)스트라이크") | ||
| }else if ball >= 1 { | ||
| print("\(ball)볼") | ||
| }else { | ||
| print("Nothing") | ||
| } | ||
|
|
||
| } | ||
| startGame() |
This file contains hidden or 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,39 @@ | ||
| // | ||
| // random.swift | ||
| // baseballgame | ||
| // | ||
| // Created by 손영빈 on 1/13/26. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| // 3자리 난수 생성 함수(1~9) -> Set 사용 -> 문제점 : 정답 생성용 Set, 정답 저장용 Array가 별도로 필요함. | ||
| func randomAnswer() -> [Int] { | ||
| var answer: Set<Int> = [] | ||
| var result: [Int] = [] | ||
|
|
||
| /* (Lv.1) 1. 1~9 서로 다른 난수 3개 생성 */ | ||
| while answer.count < 3 { //정답 생성용(Set 사용) : 같은 수 반복 x | ||
| let num = Int.random(in: 1...9) | ||
| answer.insert(num) | ||
| } | ||
| result = Array(answer) // 정답 저장용 | ||
| // print(answer) | ||
| // print(result) | ||
| return result | ||
| } | ||
| /* (Lv 3.) 1. 첫 자리(1~9), 나머지(0~9): Set 사용하지 않고 contains로 조건 사용 */ | ||
| func randomAnswer2() -> [Int] { | ||
| var result: [Int] = [] | ||
|
|
||
| let fst = Int.random(in: 1...9) | ||
| result.append(fst) | ||
| while result.count < 3{ | ||
| let rndnum = Int.random(in: 0...9) | ||
| if !result.contains(rndnum){ | ||
| result.append(rndnum) | ||
| } | ||
| } | ||
| // print(result) | ||
| return result | ||
| } |
This file contains hidden or 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,42 @@ | ||
| // | ||
| // start.swift | ||
| // baseballgame | ||
| // | ||
| // Created by 손영빈 on 1/13/26. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| // 프로그램 시작 함수 | ||
| func startGame() { | ||
| var scoreRecord: [Int] = [] | ||
| /* (Lv 4.) 1. 프로그램 시작 시 안내 문구 출력 */ | ||
| while true { | ||
| print(""" | ||
| 환영합니다! 원하시는 번호를 입력해주세요. | ||
| 1. 게임 시작하기 2. 게임 기록 보기 3. 종료하기 | ||
| """) | ||
| guard let input = readLine() | ||
| else { continue } | ||
|
|
||
| switch input { | ||
| /* (Lv 4.) 2. 게임 시작하기 선택 시 play로 연결 */ | ||
| case "1": | ||
| let score = play() //게임 후 결과를 Int로 반환받아서 score에 할당 | ||
| scoreRecord.append(score) // score을 Record에 저장 | ||
| /* (Lv 5.) 1. 게임 기록 보기 선택 시 시도 횟수 출력 */ | ||
| case "2": | ||
| print("게임 기록 보기") | ||
| for (idx, score) in scoreRecord.enumerated() { //고차함수 enumerated를 사용하여 idx: 인덱스 score: 점수 반환 후 출력 | ||
| print("\(idx + 1)번째 게임 : 시도 횟수 - \(score)") | ||
| } | ||
|
Collaborator
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. 배열의 인덱스 값을 이용하여 플레이 횟수를 유추하는 방법 너무 인상적입니다! |
||
| /* (Lv 6.) 1. 종료하기 선택 시 프로그램 종료(기록 초기화) */ | ||
| case "3": | ||
| print("게임을 종료합니다.") | ||
| return | ||
| /* (Lv 6.) 2. 이외 입력값에 대한 오류 출력 */ | ||
| default: | ||
| print ("올바른 숫자를 입력해주세요!") | ||
| } | ||
| } | ||
| } | ||
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.
20번 줄에서
guard let num = Optional(input.compactMap{ Int(String($0)) })코드로num: [Int]로 정의해주신 걸로 봤습니다!그래서
inputChars에 다시Array(input)으로 넣지 않아도inputChars = num을 바로 사용해주시거나 따로 새로운 정의 없이num을 그대로 사용하셔도 좋을 것 같습니당!그렇게하면 40번 줄 for문 내에서
inputChars를 타입 변환 없이 그대로 사용하실 수 있을 것 같아요ㅎㅎ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.
아이코, 그냥 휙 넘어갔던 부분이라 생각도 못하고있었던 것 같아요.
감사합니다 !